Struct bsn1::Id

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

Id owns IdRef and represents ‘Identifier’.

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

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

Implementations§

source§

impl Id

source

pub fn new(class: ClassTag, pc: PCTag, number: TagNumber) -> Self

Creates a new instance from class , pc , and number.

§Warnings

ASN.1 reserves some universal identifier numbers and they should not be used, however, this function ignores the rule. For example, number 15 (0x0f) is reserved for now, but this function accepts such a number without any error.

§Examples
use bsn1::{Id, IdRef};

let idref = IdRef::integer();
let id = Id::new(idref.class(), idref.pc(), idref.number().unwrap());
assert_eq!(idref, &id);
source

pub fn parse<R>(readable: &mut R) -> Result<Self, Error>
where R: ?Sized + Read,

Parses readable starting with identifier and tries to build a new instance.

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

§Performance

This function is not so efficient compared with IdRef::parse. If you have a slice of serialized bytes, use IdRef::parse and then call ToOwned::to_owned instead.

§Warnings

ASN.1 reserves some universal identifier numbers and they should not be used, however, this function ignores the rule. For example, number 15 (0x0f) is reserved for now, but this functions returns Ok.

§Examples
use bsn1::{Id, IdRef};

// Serialize an 'identifier' representing 'utf8-string'
let id = IdRef::utf8_string();
let mut serialized = Vec::from(id.as_ref());

// Deserialize it.
let deserialized = Id::parse(&mut &serialized[..]).unwrap();
assert_eq!(id, &deserialized);

// Extra octets at the end does not affect the result.
serialized.push(0);
serialized.push(1);
let deserialized = Id::parse(&mut &serialized[..]).unwrap();
assert_eq!(id, &deserialized);

// We can use `IdRef::parse` instead. It is more efficient.
let deserialized: Id = IdRef::parse(&mut &serialized[..]).map(ToOwned::to_owned).unwrap();
assert_eq!(id, &deserialized);
source

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

Provides a reference from bytes without any check. bytes must not include any extra octet.

If it is not sure whether bytes is valid octets as an identifer, use parse instead.

§Safety

The behaviour is undefined if the format of bytes is bad as ‘identifier octets’.

§Examples
use bsn1::{Id, IdRef};

let id = IdRef::eoc();
let serialized = id.as_ref();
let deserialized = unsafe { Id::from_bytes_unchecked(serialized) };
assert_eq!(deserialized, id);
source

pub fn set_number(&mut self, num: TagNumber)

Update the number of self.

§Examples
use bsn1::{ClassTag, Id, PCTag};

let mut id = Id::new(ClassTag::Private, PCTag::Primitive, 13_u8.into());
assert_eq!(id.number().unwrap(), 13_u8.into());

id.set_number(34_u8.into());
assert_eq!(id.number().unwrap(), 34_u8.into());

Methods from Deref<Target = IdRef>§

source

pub fn len(&self) -> usize

Returns the byte count of the inner bytes.

§Examples
use bsn1::IdRef;

// Id of Universal Integer is [0x02]. It is 1 byte long.
let id = IdRef::integer();
assert_eq!(id.len(), 1);
source

pub fn class(&self) -> ClassTag

Returns the ClassTag of self.

§Examples
use bsn1::{ClassTag, IdRef};

// 'EOC' is defined as Universal class.
let eoc = IdRef::eoc();
assert_eq!(ClassTag::Universal, eoc.class());
source

pub fn is_universal(&self) -> bool

Returns true if self is ‘Universal class’, or false.

§Examples
use bsn1::{ClassTag, Id, PCTag};

// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_universal());
source

pub fn is_application(&self) -> bool

Returns true if self is ‘Application class’, or false.

§Examples
use bsn1::{ClassTag, Id, PCTag};

// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Application, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_application());
source

pub fn is_context_specific(&self) -> bool

Returns true if self is ‘Context Specific class’, or false.

§Examples
use bsn1::{ClassTag, Id, PCTag};

// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::ContextSpecific, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_context_specific());
source

pub fn is_private(&self) -> bool

Returns true if self is ‘Private class’, or false.

§Examples
use bsn1::{ClassTag, Id, PCTag};

// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Private, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_private());
source

pub fn pc(&self) -> PCTag

Returns the Primitive/Constructed flag of self.

§Examples
use bsn1::{ClassTag, Id, PCTag};

// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Primitive, 0_u8.into());
assert_eq!(PCTag::Primitive, id.pc());

let id = Id::new(ClassTag::Application, PCTag::Constructed, 1_u8.into());
assert_eq!(PCTag::Constructed, id.pc());
source

pub fn is_primitive(&self) -> bool

Returns true if self is flagged as ‘Primitive’, or false.

§Examples
use bsn1::{ClassTag, Id, PCTag};

// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_primitive());
source

pub fn is_constructed(&self) -> bool

Returns true if self is flagged as ‘Constructed’, or false.

§Examples
use bsn1::{ClassTag, Id, PCTag};

// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Constructed, 0_u8.into());
assert_eq!(true, id.is_constructed());
source

pub fn number(&self) -> Result<TagNumber, Error>

Returns the number of self unless overflow.

§Examples
use bsn1::{ClassTag, Id, IdRef, PCTag};
use std::ops::Deref;

let id = Id::new(ClassTag::Application, PCTag::Primitive, 49_u8.into());
let idref: &IdRef = id.deref();
assert_eq!(49, idref.number().unwrap().get());
source

pub fn set_class(&mut self, cls: ClassTag)

Update the class of self.

§Examples
use bsn1::{ClassTag, IdRef};

// Creates a '&mut IdRef' representing 'Universal Integer'.
let mut bytes = Vec::from(IdRef::integer().as_ref());
let idref = IdRef::parse_mut(&mut &mut bytes[..]).unwrap();

assert_eq!(ClassTag::Universal, idref.class());

idref.set_class(ClassTag::Application);
assert_eq!(ClassTag::Application, idref.class());
source

pub fn set_pc(&mut self, pc: PCTag)

Update the Primitive/Constructed flag of self.

§Examples
use bsn1::{PCTag, IdRef};

let id = IdRef::integer();
let mut serialized = Vec::from(id.as_ref());
let deserialized = IdRef::parse_mut(&mut &mut serialized[..]).unwrap();

// 'Integer' is primitive.
assert_eq!(PCTag::Primitive, deserialized.pc());

// Force to change into constructed.
deserialized.set_pc(PCTag::Constructed);
assert_eq!(PCTag::Constructed, deserialized.pc());

Trait Implementations§

source§

impl AsMut<IdRef> for Id

source§

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

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

impl AsRef<[u8]> for Id

source§

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

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

impl AsRef<IdRef> for Id

source§

fn as_ref(&self) -> &IdRef

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

impl Borrow<IdRef> for Id

source§

fn borrow(&self) -> &IdRef

Immutably borrows from an owned value. Read more
source§

impl Clone for Id

source§

fn clone(&self) -> Id

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 Id

source§

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

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

impl Deref for Id

§

type Target = IdRef

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl DerefMut for Id

source§

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

Mutably dereferences the value.
source§

impl From<&IdRef> for Id

source§

fn from(idref: &IdRef) -> Self

Converts to this type from the input type.
source§

impl Hash for Id

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 Ord for Id

source§

fn cmp(&self, other: &Id) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T> PartialEq<T> for Id
where T: Borrow<IdRef>,

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<T> PartialOrd<T> for Id
where T: Borrow<IdRef>,

source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Id

Auto Trait Implementations§

§

impl Freeze for Id

§

impl RefUnwindSafe for Id

§

impl Send for Id

§

impl Sync for Id

§

impl Unpin for Id

§

impl UnwindSafe for Id

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.