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
sourceimpl Length
impl Length
sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
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);sourcepub fn to_bytes(self) -> impl Deref<Target = [u8]>
pub fn to_bytes(self) -> impl Deref<Target = [u8]>
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);sourcepub const fn len(self) -> usize
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);Trait Implementations
sourceimpl TryFrom<&[u8]> for Length
impl TryFrom<&[u8]> for Length
impl Copy for Length
impl Eq for Length
impl StructuralEq for Length
impl StructuralPartialEq for Length
Auto Trait Implementations
impl RefUnwindSafe for Length
impl Send for Length
impl Sync for Length
impl Unpin for Length
impl UnwindSafe for Length
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more