Enum Length

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

Length represents ASN.1 length.

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

§Caution

Length implements PartialEq and PartialOrd, but Eq nor Ord, because ‘Indefinite length’ is not comparable.

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§

Source§

impl Length

Source

pub fn parse<R>(readable: &mut R) -> Result<Self, Error>
where R: ?Sized + Read,

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

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

§Examples
use bsn1::Length;

// Serializes Definite(5).
let length = Length::Definite(5);
let mut serialized = Vec::from(&length.to_bytes()[..]);

// Parses the serialized bytes.
let deserialized = Length::parse(&mut &serialized[..]).unwrap();
assert_eq!(length, deserialized);

// The extra octets at the end does not affect the result.
serialized.push(0x03);
serialized.push(0x04);
let deserialized = Length::parse(&mut &serialized[..]).unwrap();
assert_eq!(length, deserialized);
Source

pub fn to_bytes(self) -> impl Deref<Target = [u8]>

Serializes self.

§Examples
use bsn1::Length;

// Serializes Definite(3), and deserializes it.
// The result is Definite(3).
let bytes = Length::Definite(3).to_bytes();
let deserialized = Length::parse(&mut &*bytes).unwrap();
assert_eq!(Length::Definite(3), deserialized);
Source

pub const fn len(self) -> usize

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);
Source

pub const fn definite(self) -> Option<usize>

Converts self to Option<usize>.

Returns None if self is Indefinite; otherwise, wraps the inner number in Some and returns it.

§Examples
use bsn1::Length;

let length = Length::Indefinite;
assert_eq!(length.definite(), None);

let length = Length::Definite(45);
assert_eq!(length.definite(), Some(45));
Source

pub const fn is_indefinite(self) -> bool

Returns true if self is Indefinite; otherwise false.

§Examples
use bsn1::Length;

let length = Length::Indefinite;
assert_eq!(length.is_indefinite(), true);

let length = Length::Definite(12);
assert_eq!(length.is_indefinite(), false);
Source

pub const fn is_definite(self) -> bool

Returns false if self is Indefinite; otherwise true.

§Examples
use bsn1::Length;

let length = Length::Indefinite;
assert_eq!(length.is_definite(), false);

let length = Length::Definite(12);
assert_eq!(length.is_definite(), true);

Trait Implementations§

Source§

impl Clone for Length

Source§

fn clone(&self) -> Length

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Length

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Length

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Length

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Length

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Length

Auto Trait Implementations§

§

impl Freeze for Length

§

impl RefUnwindSafe for Length

§

impl Send for Length

§

impl Sync for Length

§

impl Unpin for Length

§

impl UnwindSafe for Length

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.