[−][src]Struct bsn1::BerBuilder
BerBuilder is a struct to build Ber effectively.
Examples
Definite length and empty contents.
use bsn1::{Ber, BerBuilder, IdRef, Length}; let id = IdRef::octet_string(); let expected = Ber::new(IdRef::octet_string(), &[]); // Because the contents is empty, do not need to call method 'extend_contents()'. let mut builder = BerBuilder::new(id, Length::Definite(0)); let ber = builder.finish(); assert_eq!(expected, ber);
Indefinite length and empty contents.
use bsn1::{Ber, BerBuilder, IdRef, Length}; let id = IdRef::octet_string(); let eoc = Ber::new(IdRef::eoc(), &[]); // Because the contents is empty, do not need to call method 'extend_contents()'. // Function 'finish()' will adds the last 'EOC.' let mut builder = BerBuilder::new(id, Length::Indefinite); let ber = builder.finish(); assert_eq!(id, ber.id()); assert_eq!(Length::Indefinite, ber.length()); assert_eq!(eoc.as_ref() as &[u8], ber.contents());
Definite length and not empty contents.
use bsn1::{Ber, BerBuilder, IdRef, Length}; let id = IdRef::octet_string(); let contents = &[0, 1, 2, 3, 4]; let expected = Ber::new(IdRef::octet_string(), contents); // Append 'contents' at once. { let length = Length::Definite(contents.len()); let mut builder = BerBuilder::new(id, length); unsafe { builder.extend_contents(contents) }; let ber = builder.finish(); assert_eq!(expected, ber); } // Split contents into 2 pieces and append them one by one. { let length = Length::Definite(contents.len()); let mut builder = BerBuilder::new(id, length); unsafe { builder.extend_contents(&contents[..2]); builder.extend_contents(&contents[2..]); } let ber = builder.finish(); assert_eq!(expected, ber); }
Indefinite length and not empty contents.
use bsn1::{Ber, BerBuilder, IdRef, Length}; let id = IdRef::octet_string(); let contents: &[u8] = &[0, 1, 2, 3, 4]; let eoc = Ber::new(IdRef::eoc(), &[]); // Append 'contents' at once. { let length = Length::Indefinite; let mut builder = BerBuilder::new(id, length); unsafe { builder.extend_contents(contents) }; let ber = builder.finish(); assert_eq!(id, ber.id()); assert_eq!(Length::Indefinite, ber.length()); let mut bytes = Vec::from(contents); bytes.extend(eoc.as_ref() as &[u8]); let bytes: &[u8] = bytes.as_ref(); assert_eq!(bytes, ber.contents()); } // Split contents into 2 pieces and append them one by one. { let length = Length::Indefinite; let mut builder = BerBuilder::new(id, length); unsafe { builder.extend_contents(&contents[..2]); builder.extend_contents(&contents[2..]); } let ber = builder.finish(); assert_eq!(id, ber.id()); assert_eq!(Length::Indefinite, ber.length()); let mut bytes = Vec::from(contents); bytes.extend(eoc.as_ref() as &[u8]); let bytes: &[u8] = bytes.as_ref(); assert_eq!(bytes, ber.contents()); }
Implementations
impl BerBuilder[src]
pub fn new(id: &IdRef, contents_len: Length) -> Self[src]
Creates a new instance to build Der with id and contents whose length equals to
contents_len .
Examples
See examples for the struct .
pub unsafe fn extend_contents<B>(&mut self, bytes: B) where
B: AsRef<[u8]>, [src]
B: AsRef<[u8]>,
Appends bytes to the end of the DER contents to be build.
Warnings
The user must not adds 'EOC' if Length::Indefinite was passed to the constructor
funciton new .
Function finish will adds the last 'EOC.'
(Indefinite length BER must include one and only one 'EOC' in the contents.)
Safety
The behavior is undefined if user appends 'EOC' to a Indefinite length builder instance.
Panics
Panics if Length::Definite was passed to the constructor new ,
and if the accumerated length of the 'contents' will exceed that value.
Examples
See examples for the struct .
pub fn finish(self) -> Ber[src]
Consumes self , building a new Ber instance.
If Length::Indefinite was passed to the constructor new , this method adds 'EOC'
to the end of contents of a building Ber .
Panics
Panics if Length::Definite was passed to the constructor new ,
and if the accumerated length of the 'contents' does not equal to that value.
Examples
See examples for the struct .
Auto Trait Implementations
impl RefUnwindSafe for BerBuilder
impl !Send for BerBuilder
impl !Sync for BerBuilder
impl Unpin for BerBuilder
impl UnwindSafe for BerBuilder
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,