pub enum Length {
    Indefinite,
    Definite(usize),
}
Expand description

Length represents ASN.1 length.

Note that Length represents the byte count of the contents in ASN.1. The total byte size of BER, DER, and CER is greater than that. (BER, DER, and CER are constituted of identifier, length, and contents.)

Variants

Indefinite

Represents ‘Indefinite’ length.

‘Indefinite’ is only for ‘BER’, and the contents must end with ‘EOC’ octets.

Definite(usize)

‘Definite’ is for ‘BER’, ‘DER’, and ‘CER’, and represents the byte count of the contents.

Implementations

Parses bytes starting with length octets and tries to creates a new instance.

This function ignores extra octet(s) at the end of bytes if any.

This method is same to TryFrom implementation.

Examples
use bsn1::Length;

let mut bytes = vec![0x05]; // represents Definite(5).
let len = Length::from_bytes(&bytes).unwrap();
assert_eq!(Length::Definite(5), len);

// Ignores the last extra octet 0x03.
bytes.push(0x03);
let len = Length::from_bytes(&bytes).unwrap();
assert_eq!(Length::Definite(5), len);

Serializes length .

Examples
use bsn1::Length;
use std::convert::TryFrom;

let length = Length::Definite(3);
let bytes = length.to_bytes();

let deserialized = Length::try_from(bytes.as_ref()).unwrap();
assert_eq!(length, deserialized);

Returns the byte count of the octets that self is serialized.

Examples
use bsn1::Length;

// The length of INDEFINITE is always 1.
assert_eq!(Length::Indefinite.len(), 1);

// The length is 1 if the value is less than or equals to 127.
assert_eq!(Length::Definite(0).len(), 1);
assert_eq!(Length::Definite(127).len(), 1);

// The length is 2 if the value is 128.
assert_eq!(Length::Definite(128).len(), 2);

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Parses bytes starting with length octets and tries to create a new instance.

This function ignores extra octet(s) at the end of bytes if any.

This function is same to from_bytes.

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.