Struct bsn1::Ber[][src]

pub struct Ber { /* fields omitted */ }
Expand description

Ber owns BerRef and represents a BER.

Implementations

impl Ber[src]

pub fn new(id: &IdRef, contents: &[u8]) -> Self[src]

Creates a new instance from id and contents with definite length.

Note that BER allows both definite and indefinite length, however, the length of the return value is always definite. (Generally speaking, the performance of definite length is better than that of indefinite length. Indefinite length is seldom used these days.)

Examples

use bsn1::{Ber, IdRef};

let id = IdRef::octet_string();
let _ber = Ber::new(id, &[]);

pub fn from_id_iterator<I>(id: &IdRef, contents: I) -> Self where
    I: Iterator + Clone,
    I::Item: AsRef<[u8]>, 
[src]

Creates a new instance from id and contents .

Examples

use bsn1::{Ber, IdRef};

let id = IdRef::sequence();

// Build instance using function 'from_id_iterator()'.
let contents: &[Ber] = &[Ber::utf8_string("foo"), Ber::integer(29)];
let ber = Ber::from_id_iterator(id, contents.iter());

// Build instance using function 'new()'.
let contents: Vec<u8> = contents.iter()
                        .map(|i| Vec::from(i.as_ref() as &[u8]))
                        .flatten().collect();
let expected = Ber::new(id, &contents);

assert_eq!(expected, ber);

pub fn boolean(val: bool) -> Self[src]

Returns a new instance representing boolean.

Examples

use bsn1::{contents, Ber, IdRef};

let val = true;
let ber = Ber::boolean(val);

assert_eq!(IdRef::boolean(), ber.id());
assert_eq!(val, contents::to_bool_ber(ber.contents()).unwrap());

pub fn integer(val: i128) -> Self[src]

Returns a new instance representing ingeger.

Examples

use bsn1::{contents, Ber, IdRef};

let val = 39;
let ber = Ber::integer(val);

assert_eq!(IdRef::integer(), ber.id());
assert_eq!(val, contents::to_integer(ber.contents()).unwrap());

pub fn utf8_string(val: &str) -> Self[src]

Returns a new instance representing utf8_string.

Examples

use bsn1::{Ber, IdRef};

let val = &"foo";
let ber = Ber::utf8_string(val);

assert_eq!(IdRef::utf8_string(), ber.id());
assert_eq!(val.as_bytes(), ber.contents());

pub fn octet_string(val: &[u8]) -> Self[src]

Returns a new instance representing octet_string.

Examples

use bsn1::{Ber, IdRef};

let val = &[1, 2, 3];
let ber = Ber::octet_string(val);

assert_eq!(IdRef::octet_string(), ber.id());
assert_eq!(val, ber.contents());

impl Ber[src]

pub fn into_vec(self) -> Vec<u8>[src]

Consumes self , returning Vec .

Examples

use bsn1::{Ber, IdRef};

let id = IdRef::octet_string();
let contents: &[u8] = &[0, 1, 2, 3, 4];

let ber = Ber::new(id, contents);
let v = ber.clone().into_vec();

assert_eq!(ber.as_ref() as &[u8], v.as_ref() as &[u8]);

Methods from Deref<Target = BerRef>

pub fn id(&self) -> &IdRef[src]

Provides a reference to IdRef of self .

Examples

use bsn1::{Ber, BerRef, IdRef};

let id = IdRef::octet_string();
let contents = &[1, 2, 3];

// 'Ber' implements 'Deref<Target=BerRef>.'
let ber = Ber::new(id, contents);
assert_eq!(id, ber.id());

pub fn length(&self) -> Length[src]

Returns Length of self .

Warnings

Length stands for ‘the length octets of the contents’ in BER. The total bytes is greater than the value.

Examples

use bsn1::{Ber, BerRef, IdRef, Length};

let id = IdRef::octet_string();
let contents = &[1, 2, 3];

// 'Ber' implements 'Deref<Target=BerRef>.'
let ber = Ber::new(id, contents);
assert_eq!(Length::Definite(contents.len()), ber.length());

pub fn contents(&self) -> &[u8][src]

Provides a reference to the ‘contents’ octets of self .

Examples

use bsn1::{Ber, BerRef, IdRef};

let id = IdRef::octet_string();
let contents = &[1, 2, 3];

// 'Ber' implements 'Deref<Target=BerRef>.'
let ber = Ber::new(id, contents);
assert_eq!(contents, ber.contents());

Trait Implementations

impl AsRef<[u8]> for Ber[src]

fn as_ref(&self) -> &[u8][src]

Performs the conversion.

impl AsRef<BerRef> for Ber[src]

fn as_ref(&self) -> &BerRef[src]

Performs the conversion.

impl Borrow<[u8]> for Ber[src]

fn borrow(&self) -> &[u8][src]

Immutably borrows from an owned value. Read more

impl Borrow<BerRef> for Ber[src]

fn borrow(&self) -> &BerRef[src]

Immutably borrows from an owned value. Read more

impl Clone for Ber[src]

fn clone(&self) -> Ber[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Ber[src]

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

Formats the value using the given formatter. Read more

impl Deref for Ber[src]

type Target = BerRef

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Dereferences the value.

impl From<&'_ BerRef> for Ber[src]

fn from(ber_ref: &BerRef) -> Self[src]

Performs the conversion.

impl From<&'_ DerRef> for Ber[src]

fn from(der: &DerRef) -> Self[src]

Performs the conversion.

impl From<Der> for Ber[src]

fn from(der: Der) -> Self[src]

Performs the conversion.

impl PartialEq<Ber> for Ber[src]

fn eq(&self, other: &Ber) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Ber) -> bool[src]

This method tests for !=.

impl TryFrom<&'_ [u8]> for Ber[src]

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>[src]

Parses bytes starting with octets of ‘ASN.1 BER’ and returns a new instance.

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

type Error = Error

The type returned in the event of a conversion error.

impl Eq for Ber[src]

impl StructuralEq for Ber[src]

impl StructuralPartialEq for Ber[src]

Auto Trait Implementations

impl RefUnwindSafe for Ber

impl Send for Ber

impl Sync for Ber

impl Unpin for Ber

impl UnwindSafe for Ber

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

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]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.