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>§
sourcepub fn len(&self) -> usize
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());
sourcepub fn is_empty(&self) -> bool
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);
sourcepub fn to_integer<T>(&self) -> Result<T, Error>where
T: PrimInt,
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());
sourcepub unsafe fn to_integer_unchecked<T>(&self) -> Twhere
T: PrimInt,
pub unsafe fn to_integer_unchecked<T>(&self) -> Twhere
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 ofT
.
sourcepub fn to_bool_ber(&self) -> Result<bool, Error>
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());
sourcepub fn to_bool_der(&self) -> Result<bool, Error>
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());