Struct Integer

pub struct Integer<'a> { /* private fields */ }
Expand description

ASN.1 INTEGER type

Generic representation for integer types. BER/DER integers can be of any size, so it is not possible to store them as simple integers (they are stored as raw bytes).

The internal representation can be obtained using .as_ref().

§Note

Methods from/to BER and DER encodings are also implemented for primitive types (u8, u16 to u128, and i8 to i128). In most cases, it is easier to use these types directly.

§Examples

Creating an Integer

use asn1_rs::Integer;

// unsigned
let i = Integer::from(4);
assert_eq!(i.as_ref(), &[4]);
// signed
let j = Integer::from(-2);
assert_eq!(j.as_ref(), &[0xfe]);

Converting an Integer to a primitive type (using the TryInto trait)

use asn1_rs::{Error, Integer};
use std::convert::TryInto;

let i = Integer::new(&[0x12, 0x34, 0x56, 0x78]);
// converts to an u32
let n: u32 = i.try_into().unwrap();

// Same, but converting to an u16: will fail, value cannot fit into an u16
let i = Integer::new(&[0x12, 0x34, 0x56, 0x78]);
assert_eq!(i.try_into() as Result<u16, _>, Err(Error::IntegerTooLarge));

Encoding an Integer to DER

use asn1_rs::{Integer, ToDer};

let i = Integer::from(4);
let v = i.to_der_vec().unwrap();
assert_eq!(&v, &[2, 1, 4]);

// same, with primitive types
let v = 4.to_der_vec().unwrap();
assert_eq!(&v, &[2, 1, 4]);

Implementations§

§

impl<'a> Integer<'a>

pub const fn new(s: &'a [u8]) -> Integer<'a>

Creates a new Integer containing the given value (borrowed).

pub fn any(&'a self) -> Any<'a>

Creates a borrowed Any for this object

pub fn from_const_array<const N: usize>(b: [u8; N]) -> Integer<'a>

Build an Integer from a constant array of bytes representation of an integer.

§

impl Integer<'_>

pub fn as_i8(&self) -> Result<i8, Error>

Attempts to convert an Integer to a i8.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_i8(i: i8) -> Integer<'_>

Converts a i8 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_i16(&self) -> Result<i16, Error>

Attempts to convert an Integer to a i16.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_i16(i: i16) -> Integer<'_>

Converts a i16 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_i32(&self) -> Result<i32, Error>

Attempts to convert an Integer to a i32.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_i32(i: i32) -> Integer<'_>

Converts a i32 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_i64(&self) -> Result<i64, Error>

Attempts to convert an Integer to a i64.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_i64(i: i64) -> Integer<'_>

Converts a i64 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_i128(&self) -> Result<i128, Error>

Attempts to convert an Integer to a i128.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_i128(i: i128) -> Integer<'_>

Converts a i128 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_u8(&self) -> Result<u8, Error>

Attempts to convert an Integer to a u8.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_u8(i: u8) -> Integer<'_>

Converts a u8 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_u16(&self) -> Result<u16, Error>

Attempts to convert an Integer to a u16.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_u16(i: u16) -> Integer<'_>

Converts a u16 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_u32(&self) -> Result<u32, Error>

Attempts to convert an Integer to a u32.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_u32(i: u32) -> Integer<'_>

Converts a u32 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_u64(&self) -> Result<u64, Error>

Attempts to convert an Integer to a u64.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_u64(i: u64) -> Integer<'_>

Converts a u64 to an Integer

Note: this function allocates data.

§

impl Integer<'_>

pub fn as_u128(&self) -> Result<u128, Error>

Attempts to convert an Integer to a u128.

This function returns an IntegerTooLarge error if the integer will not fit into the output type.

§

impl Integer<'_>

pub fn from_u128(i: u128) -> Integer<'_>

Converts a u128 to an Integer

Note: this function allocates data.

Trait Implementations§

§

impl AsRef<[u8]> for Integer<'_>

§

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

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

impl CheckDerConstraints for Integer<'_>

§

impl<'a> Debug for Integer<'a>

§

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

Formats the value using the given formatter. Read more
§

impl From<i128> for Integer<'_>

§

fn from(i: i128) -> Integer<'_>

Converts to this type from the input type.
§

impl From<i16> for Integer<'_>

§

fn from(i: i16) -> Integer<'_>

Converts to this type from the input type.
§

impl From<i32> for Integer<'_>

§

fn from(i: i32) -> Integer<'_>

Converts to this type from the input type.
§

impl From<i64> for Integer<'_>

§

fn from(i: i64) -> Integer<'_>

Converts to this type from the input type.
§

impl From<i8> for Integer<'_>

§

fn from(i: i8) -> Integer<'_>

Converts to this type from the input type.
§

impl From<u128> for Integer<'_>

§

fn from(i: u128) -> Integer<'_>

Converts to this type from the input type.
§

impl From<u16> for Integer<'_>

§

fn from(i: u16) -> Integer<'_>

Converts to this type from the input type.
§

impl From<u32> for Integer<'_>

§

fn from(i: u32) -> Integer<'_>

Converts to this type from the input type.
§

impl From<u64> for Integer<'_>

§

fn from(i: u64) -> Integer<'_>

Converts to this type from the input type.
§

impl From<u8> for Integer<'_>

§

fn from(i: u8) -> Integer<'_>

Converts to this type from the input type.
§

impl<'a> PartialEq for Integer<'a>

§

fn eq(&self, other: &Integer<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

impl Tagged for Integer<'_>

§

const TAG: Tag = Tag::Integer

§

impl ToDer for Integer<'_>

§

fn to_der_len(&self) -> Result<usize, Error>

Get the length of the object (including the header), when encoded
§

fn write_der_header( &self, writer: &mut dyn Write, ) -> Result<usize, SerializeError>

Attempt to write the DER header to this writer.
§

fn write_der_content( &self, writer: &mut dyn Write, ) -> Result<usize, SerializeError>

Attempt to write the DER content (all except header) to this writer.
§

fn to_der_vec(&self) -> Result<Vec<u8>, SerializeError>

Write the DER encoded representation to a newly allocated Vec<u8>.
§

fn to_der_vec_raw(&self) -> Result<Vec<u8>, SerializeError>

Similar to using to_vec, but uses provided values without changes. This can generate an invalid encoding for a DER object.
§

fn write_der(&self, writer: &mut dyn Write) -> Result<usize, SerializeError>

Attempt to write the DER encoded representation (header and content) into this writer. Read more
§

fn write_der_raw(&self, writer: &mut dyn Write) -> Result<usize, SerializeError>

Similar to using to_der, but uses provided values without changes. This can generate an invalid encoding for a DER object.
§

impl<'a, 'b> TryFrom<&'b Any<'a>> for Integer<'a>

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(any: &'b Any<'a>) -> Result<Integer<'a>, Error>

Performs the conversion.
§

impl<'a> TryFrom<Any<'a>> for Integer<'a>

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(any: Any<'a>) -> Result<Integer<'a>, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for i128

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<i128, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for i16

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<i16, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for i32

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<i32, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for i64

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<i64, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for i8

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<i8, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for u128

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<u128, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for u16

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<u16, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for u32

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<u32, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for u64

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<u64, Error>

Performs the conversion.
§

impl TryFrom<Integer<'_>> for u8

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(value: Integer<'_>) -> Result<u8, Error>

Performs the conversion.
§

impl DerAutoDerive for Integer<'_>

§

impl<'a> Eq for Integer<'a>

§

impl<'a> StructuralPartialEq for Integer<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Integer<'a>

§

impl<'a> RefUnwindSafe for Integer<'a>

§

impl<'a> Send for Integer<'a>

§

impl<'a> Sync for Integer<'a>

§

impl<'a> Unpin for Integer<'a>

§

impl<'a> UnwindSafe for Integer<'a>

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<T> Choice for T
where T: Tagged,

§

fn can_decode(tag: Tag) -> bool

Is the provided Tag decodable as a variant of this CHOICE?
§

impl<T> DynTagged for T
where T: Tagged,

§

fn tag(&self) -> Tag

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<'a, T, E> FromBer<'a, E> for T
where T: TryFrom<Any<'a>, Error = E>, E: From<Error>,

§

fn from_ber(bytes: &'a [u8]) -> Result<(&'a [u8], T), Err<E>>

Attempt to parse input bytes into a BER object
§

impl<'a, T, E> FromDer<'a, E> for T
where T: TryFrom<Any<'a>, Error = E> + CheckDerConstraints + DerAutoDerive, E: From<Error> + Display + Debug,

§

fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], T), Err<E>>

Attempt to parse input bytes into a DER object (enforcing constraints)
§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
§

impl<T, U> RamaInto<U> for T
where U: RamaFrom<T>,

§

fn rama_into(self) -> U

§

impl<T, U> RamaInto<U> for T
where U: RamaFrom<T>,

§

fn rama_into(self) -> U

§

impl<T, U> RamaTryInto<U> for T
where U: RamaTryFrom<T>,

§

type Error = <U as RamaTryFrom<T>>::Error

§

fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T>>::Error>

§

impl<T, U> RamaTryInto<U> for T
where U: RamaTryFrom<T>,

§

type Error = <U as RamaTryFrom<T>>::Error

§

fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T>>::Error>

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,