Struct bsn1::Contents

source ·
pub struct Contents { /* private fields */ }
Expand description

Contents owns ContentsRef and represents contents octets of ASN.1.

The structure of Contents is similar to that of Vec<u8>.

The user can access the inner ContentsRef via the Deref and DerefMut implementations.

Methods from Deref<Target = ContentsRef>§

source

pub fn len(&self) -> usize

Returns the byte count of the inner slice.

§Example
use bsn1::ContentsRef;

let bytes = &[0, 1, 2, 3, 4];
let contents = <&ContentsRef>::from(bytes);

assert_eq!(contents.len(), bytes.len());
source

pub fn is_empty(&self) -> bool

Returns true if the inner slice is empty, or false.

§Example
use bsn1::ContentsRef;

let bytes = &[];
let contents = <&ContentsRef>::from(bytes);
assert_eq!(contents.is_empty(), true);

let bytes = &[0, 1, 2, 3, 4];
let contents = <&ContentsRef>::from(bytes);
assert_eq!(contents.is_empty(), false);
source

pub fn to_integer<T>(&self) -> Result<T, Error>
where T: PrimInt,

Parses self as the ASN.1 contents of integer.

Type T should be a built-in primitive integer type (e.g., u8, i32, isize, i128…)

§Examples
use bsn1::{Contents, ContentsRef};

let contents = Contents::from(17_i32);
assert_eq!(Ok(17_i32), contents.to_integer::<i32>());

// Overflow to convert i32::MAX into i16.
let contents = Contents::from(i32::MAX);
assert!(contents.to_integer::<i16>().is_err());

// Cannot convert a negatibe value into unsigned type.
let contents = Contents::from(-5_i32);
assert!(contents.to_integer::<u32>().is_err());
source

pub unsafe fn to_integer_unchecked<T>(&self) -> T
where T: PrimInt,

Parses self as a contents of ASN.1 integer without any check.

Type T should be a built-in primitive integer type (e.g., u8, i32, isize, u128, …)

§Safety

The behaviour is undefined in the following cases.

  • self is not formatted as the contents of ASN.1 integer.
  • The value is greater than the max value of T, or less than the min value of T.
source

pub fn to_bool_ber(&self) -> Result<bool, Error>

Parses self as the contents of BER bool.

§Warnings

The rule of BER bool is different from that of DER and CER. BER regards 0x00 as False, and any octet except for 0x00 as True.

See also to_bool_der.

§Examples
use bsn1::ContentsRef;

let true_contents = <&ContentsRef>::from(true);
assert_eq!(Ok(true), true_contents.to_bool_ber());

let false_contents = <&ContentsRef>::from(false);
assert_eq!(Ok(false), false_contents.to_bool_ber());
source

pub fn to_bool_der(&self) -> Result<bool, Error>

Parses self as the contents of DER bool.

§Warnings

The rule of BER bool is different from that of DER and CER. DER regards 0xFF as True, and 0x00 as False.

See also to_bool_ber.

§Examples
use bsn1::ContentsRef;

let true_contents = <&ContentsRef>::from(true);
assert_eq!(Ok(true), true_contents.to_bool_der());

let false_contents = <&ContentsRef>::from(false);
assert_eq!(Ok(false), false_contents.to_bool_der());

Trait Implementations§

source§

impl AsMut<[u8]> for Contents

source§

fn as_mut(&mut self) -> &mut [u8]

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsMut<ContentsRef> for Contents

source§

fn as_mut(&mut self) -> &mut ContentsRef

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsRef<[u8]> for Contents

source§

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

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<ContentsRef> for Contents

source§

fn as_ref(&self) -> &ContentsRef

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<ContentsRef> for Contents

source§

fn borrow(&self) -> &ContentsRef

Immutably borrows from an owned value. Read more
source§

impl Clone for Contents

source§

fn clone(&self) -> Contents

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 Contents

source§

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

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

impl Deref for Contents

§

type Target = ContentsRef

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl DerefMut for Contents

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl From<&[u8]> for Contents

source§

fn from(bytes: &[u8]) -> Self

Converts to this type from the input type.
source§

impl<const N: usize> From<&[u8; N]> for Contents

source§

fn from(bytes: &[u8; N]) -> Self

Converts to this type from the input type.
source§

impl From<&ContentsRef> for Contents

source§

fn from(value: &ContentsRef) -> Self

Converts to this type from the input type.
source§

impl<'a> From<&'a str> for Contents

source§

fn from(value: &'a str) -> Self

Converts to this type from the input type.
source§

impl From<String> for Contents

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

impl From<Vec<u8>> for Contents

source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
source§

impl From<bool> for Contents

source§

fn from(val: bool) -> Self

The encoding rule of boolean is different between BER and DER. The return value is valid both as BER and DER.

source§

impl From<i128> for Contents

source§

fn from(val: i128) -> Self

Converts to this type from the input type.
source§

impl From<i16> for Contents

source§

fn from(val: i16) -> Self

Converts to this type from the input type.
source§

impl From<i32> for Contents

source§

fn from(val: i32) -> Self

Converts to this type from the input type.
source§

impl From<i64> for Contents

source§

fn from(val: i64) -> Self

Converts to this type from the input type.
source§

impl From<i8> for Contents

source§

fn from(val: i8) -> Self

Converts to this type from the input type.
source§

impl From<isize> for Contents

source§

fn from(val: isize) -> Self

Converts to this type from the input type.
source§

impl From<u128> for Contents

source§

fn from(val: u128) -> Self

Converts to this type from the input type.
source§

impl From<u16> for Contents

source§

fn from(val: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for Contents

source§

fn from(val: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Contents

source§

fn from(val: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for Contents

source§

fn from(val: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for Contents

source§

fn from(val: usize) -> Self

Converts to this type from the input type.
source§

impl Hash for Contents

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<T> PartialEq<T> for Contents
where T: Borrow<ContentsRef>,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Contents

Auto Trait Implementations§

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> 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,

§

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>,

§

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>,

§

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.