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(), &[0x0, 0xff, 0xff, 0xff, 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

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

Creates a borrowed Any for this object

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

Attempts to convert an Integer to a i8.

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

Converts a i8 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a i16.

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

Converts a i16 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a i32.

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

Converts a i32 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a i64.

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

Converts a i64 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a i128.

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

Converts a i128 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a u8.

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

Converts a u8 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a u16.

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

Converts a u16 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a u32.

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

Converts a u32 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a u64.

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

Converts a u64 to an Integer

Note: this function allocates data.

Attempts to convert an Integer to a u128.

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

Converts a u128 to an Integer

Note: this function allocates data.

Trait Implementations

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

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Get the length of the object, when encoded Read more

Attempt to write the DER header to this writer.

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

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

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

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.