Struct bsn1::ContentsRef

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

ContentsRef is a wrapper of [u8] and represents ‘ASN.1 contents’.

The user can access the inner slice via the implementation of AsRef or AsMut.

This struct is Unsized, and the user will usually use a reference.

Implementations§

source§

impl ContentsRef

source

pub const 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 const 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 const 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 const 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 ContentsRef

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 ContentsRef

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 Debug for ContentsRef

source§

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

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

impl<'a> From<&'a [u8]> for &'a ContentsRef

source§

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

Converts to this type from the input type.
source§

impl<'a, const N: usize> From<&'a [u8; N]> for &'a ContentsRef

source§

fn from(bytes: &'a [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 mut [u8]> for &'a mut ContentsRef

source§

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

Converts to this type from the input type.
source§

impl<'a, const N: usize> From<&'a mut [u8; N]> for &'a mut ContentsRef

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl From<bool> for &'static ContentsRef

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 Hash for ContentsRef

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
source§

impl<T> Index<T> for ContentsRef
where T: SliceIndex<[u8]>,

§

type Output = <T as SliceIndex<[u8]>>::Output

The returned type after indexing.
source§

fn index(&self, index: T) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IndexMut<T> for ContentsRef
where T: SliceIndex<[u8]>,

source§

fn index_mut(&mut self, index: T) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<T> PartialEq<T> for ContentsRef
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 PartialEq for ContentsRef

source§

fn eq(&self, other: &ContentsRef) -> 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 ToOwned for ContentsRef

§

type Owned = Contents

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> Self::Owned

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · source§

fn clone_into(&self, target: &mut Self::Owned)

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

impl Eq for ContentsRef

source§

impl StructuralPartialEq for ContentsRef

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