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
impl ContentsRef
sourcepub const fn len(&self) -> usize
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());
sourcepub const fn is_empty(&self) -> bool
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);
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 const fn to_bool_ber(&self) -> Result<bool, Error>
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());
sourcepub const fn to_bool_der(&self) -> Result<bool, Error>
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
impl AsMut<[u8]> for ContentsRef
source§impl AsMut<ContentsRef> for Contents
impl AsMut<ContentsRef> for Contents
source§fn as_mut(&mut self) -> &mut ContentsRef
fn as_mut(&mut self) -> &mut ContentsRef
source§impl AsRef<[u8]> for ContentsRef
impl AsRef<[u8]> for ContentsRef
source§impl AsRef<ContentsRef> for Contents
impl AsRef<ContentsRef> for Contents
source§fn as_ref(&self) -> &ContentsRef
fn as_ref(&self) -> &ContentsRef
source§impl Borrow<ContentsRef> for Contents
impl Borrow<ContentsRef> for Contents
source§fn borrow(&self) -> &ContentsRef
fn borrow(&self) -> &ContentsRef
source§impl Debug for ContentsRef
impl Debug for ContentsRef
source§impl<'a> From<&'a [u8]> for &'a ContentsRef
impl<'a> From<&'a [u8]> for &'a ContentsRef
source§impl From<&ContentsRef> for Contents
impl From<&ContentsRef> for Contents
source§fn from(value: &ContentsRef) -> Self
fn from(value: &ContentsRef) -> Self
source§impl<'a> From<&'a mut [u8]> for &'a mut ContentsRef
impl<'a> From<&'a mut [u8]> for &'a mut ContentsRef
source§impl<'a> From<&'a str> for &'a ContentsRef
impl<'a> From<&'a str> for &'a ContentsRef
source§impl From<bool> for &'static ContentsRef
impl From<bool> for &'static ContentsRef
source§impl Hash for ContentsRef
impl Hash for ContentsRef
source§impl<T> Index<T> for ContentsRefwhere
T: SliceIndex<[u8]>,
impl<T> Index<T> for ContentsRefwhere
T: SliceIndex<[u8]>,
source§impl<T> IndexMut<T> for ContentsRefwhere
T: SliceIndex<[u8]>,
impl<T> IndexMut<T> for ContentsRefwhere
T: SliceIndex<[u8]>,
source§impl<T> PartialEq<T> for ContentsRefwhere
T: Borrow<ContentsRef>,
impl<T> PartialEq<T> for ContentsRefwhere
T: Borrow<ContentsRef>,
source§impl PartialEq for ContentsRef
impl PartialEq for ContentsRef
source§fn eq(&self, other: &ContentsRef) -> bool
fn eq(&self, other: &ContentsRef) -> bool
self
and other
values to be equal, and is used
by ==
.