Struct bsn1::DerRef[][src]

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

DerRef is a wrapper of [u8] and represents DER.

This struct is ‘Unsized’, and user usually uses a reference to the instance.

Implementations

impl DerRef[src]

pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self[src]

Provides a reference from bytes without any sanitization.

bytes must not include any extra octet.

If it is sure that bytes starts with DER octets, but if some extra octet(s) may added after that, use from_bytes_starts_with_unchecked instead. If it is not sure whether bytes starts with DER octets or not, use TryFrom implementation.

Safety

The behavior is undefined if bytes is not formatted as a DER.

Examples

use bsn1::{Der, DerRef, IdRef};

let der = Der::new(IdRef::octet_string(), &[]);
let der_ref = unsafe { DerRef::from_bytes_unchecked(der.as_ref()) };
assert_eq!(der.as_ref() as &DerRef, der_ref);

pub unsafe fn from_bytes_starts_with_unchecked(bytes: &[u8]) -> &Self[src]

Provides a reference from bytes that starts with a DER.

bytes may include some extra octet(s) at the end.

If it is not sure whether bytes starts with DER octets or not, use TryFrom implementation.

Safety

The behavior is undefined if bytes does not start with ‘ASN.1 DER’ octets.

Examples

use bsn1::{Der, DerRef, IdRef};

let der = Der::new(IdRef::octet_string(), &[]);
let mut bytes = Vec::from(der.as_ref() as &[u8]);
bytes.extend(&[1, 2, 3]);

let der_ref = unsafe { DerRef::from_bytes_starts_with_unchecked(bytes.as_ref()) };
assert_eq!(der.as_ref() as &DerRef, der_ref);

impl DerRef[src]

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

Returns a reference to IdRef of self .

Examples

use bsn1::{Der, IdRef};

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

// 'DER' implements 'Deref<Target=DerRef>'
let der = Der::new(id, contents);
assert_eq!(id, der.id());

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

Returns Length to represent the length of contents.

Note that DER does not allow indefinite Length. The return value must be Length::Definite .

Warnings

Length stands for the length octets in DER. The total bytes is greater than the value.

Examples

use bsn1::{Der, IdRef, Length};

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

// 'DER' implements 'Deref<Target=DerRef>'
let der = Der::new(id, contents);
assert_eq!(Length::Definite(contents.len()), der.length());

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

Returns a reference to the contents octets of self .

Examples

use bsn1::{Der, IdRef};

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

// 'DER' implements 'Deref<Target=DerRef>'
let der = Der::new(id, contents);
assert_eq!(contents, der.contents());

Trait Implementations

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

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

Performs the conversion.

impl AsRef<DerRef> for Der[src]

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

Performs the conversion.

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

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

Immutably borrows from an owned value. Read more

impl Borrow<DerRef> for Der[src]

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

Immutably borrows from an owned value. Read more

impl Debug for DerRef[src]

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

Formats the value using the given formatter. Read more

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

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

Performs the conversion.

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

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

Performs the conversion.

impl<'a> From<&'a DerRef> for &'a BerRef[src]

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

Performs the conversion.

impl PartialEq<DerRef> for DerRef[src]

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

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

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

This method tests for !=.

impl ToOwned for DerRef[src]

type Owned = Der

The resulting type after obtaining ownership.

fn to_owned(&self) -> Self::Owned[src]

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

fn clone_into(&self, target: &mut Self::Owned)[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<'a> TryFrom<&'a [u8]> for &'a DerRef[src]

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

Parses bytes starting with octets of ‘ASN.1 DER’ and returns a reference to DerRef .

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

Warnings

ASN.1 does not allow some universal identifier for DER, however, this function will accept such an identifier. For example, ‘Octet String’ must be primitive in DER, but this function returns Ok for constructed Octet String DER.

type Error = Error

The type returned in the event of a conversion error.

impl Eq for DerRef[src]

impl StructuralEq for DerRef[src]

impl StructuralPartialEq for DerRef[src]

Auto Trait Implementations

impl RefUnwindSafe for DerRef

impl Send for DerRef

impl !Sized for DerRef

impl Sync for DerRef

impl Unpin for DerRef

impl UnwindSafe for DerRef

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