Struct bcder::int::Integer

source ·
pub struct Integer(/* private fields */);
Expand description

A BER encoded integer.

As integers are variable length in BER, this type is just a simple wrapper atop the underlying Bytes value containing the raw content. A value of this type is a signed integer. If a value is defined as an unsigned integer, i.e., as INTEGER (0..MAX), you should use the sibling type Unsigned instead.

In addition to these two generic types, the content decoders also provide methods to parse integers into native integer types such as i8. If the range of such a type is obviously enough, you might want to consider using these methods instead.

BER Encoding

In BER, an INTEGER is encoded as a primitive value with the content octets providing a variable-length, big-endian, two‘s complement byte sequence of that integer. Thus, the most-significant bit of the first octet serves as the sign bit.

Implementations§

source§

impl Integer

source

pub fn take_from<S: Source>( cons: &mut Constructed<'_, S> ) -> Result<Self, DecodeError<S::Error>>

Takes a single signed integer from the beginning of an encoded value.

This requires the next value in cons to be a primitive value with a correctly encoded signed integer.

source

pub fn from_primitive<S: Source>( prim: &mut Primitive<'_, S> ) -> Result<Self, DecodeError<S::Error>>

Constructs a signed integer from the content of a primitive value.

source

pub fn i8_from_primitive<S: Source>( prim: &mut Primitive<'_, S> ) -> Result<i8, DecodeError<S::Error>>

Constructs an i8 from the content of a primitive value.

source

pub fn i16_from_primitive<S: Source>( prim: &mut Primitive<'_, S> ) -> Result<i16, DecodeError<S::Error>>

Constructs an i16 from the content of a primitive value.

source

pub fn i32_from_primitive<S: Source>( prim: &mut Primitive<'_, S> ) -> Result<i32, DecodeError<S::Error>>

Constructs an i32 from the content of a primitive value.

source

pub fn i64_from_primitive<S: Source>( prim: &mut Primitive<'_, S> ) -> Result<i64, DecodeError<S::Error>>

Constructs an i64 from the content of a primitive value.

source

pub fn i128_from_primitive<S: Source>( prim: &mut Primitive<'_, S> ) -> Result<i128, DecodeError<S::Error>>

Constructs an i128 from the content of a primitive value.

source

pub fn into_bytes(self) -> Bytes

Trades the integer into a bytes value with the raw content octets.

source

pub fn as_slice(&self) -> &[u8]

Returns a bytes slice with the raw content.

source

pub fn is_zero(&self) -> bool

Returns whether the number is zero.

source

pub fn is_positive(&self) -> bool

Returns whether the integer is positive.

Also returns false if the number is zero.

source

pub fn is_negative(&self) -> bool

Returns whether the integer is negative.

Also returns false if the number is zero.

Trait Implementations§

source§

impl AsRef<[u8]> for Integer

source§

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

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

impl AsRef<Bytes> for Integer

source§

fn as_ref(&self) -> &Bytes

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

impl AsRef<Integer> for Unsigned

source§

fn as_ref(&self) -> &Integer

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

impl Clone for Integer

source§

fn clone(&self) -> Integer

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 Integer

source§

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

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

impl From<i128> for Integer

source§

fn from(val: i128) -> Integer

Converts to this type from the input type.
source§

impl From<i16> for Integer

source§

fn from(val: i16) -> Integer

Converts to this type from the input type.
source§

impl From<i32> for Integer

source§

fn from(val: i32) -> Integer

Converts to this type from the input type.
source§

impl From<i64> for Integer

source§

fn from(val: i64) -> Integer

Converts to this type from the input type.
source§

impl From<i8> for Integer

source§

fn from(val: i8) -> Integer

Converts to this type from the input type.
source§

impl From<u128> for Integer

source§

fn from(val: u128) -> Integer

Converts to this type from the input type.
source§

impl From<u16> for Integer

source§

fn from(val: u16) -> Integer

Converts to this type from the input type.
source§

impl From<u32> for Integer

source§

fn from(val: u32) -> Integer

Converts to this type from the input type.
source§

impl From<u64> for Integer

source§

fn from(val: u64) -> Integer

Converts to this type from the input type.
source§

impl From<u8> for Integer

source§

fn from(val: u8) -> Integer

Converts to this type from the input type.
source§

impl Hash for Integer

source§

fn hash<H: Hasher>(&self, h: &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 Integer

source§

fn cmp(&self, other: &Self) -> 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 PartialEq<Unsigned> for Integer

source§

fn eq(&self, other: &Unsigned) -> 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 PartialEq for Integer

source§

fn eq(&self, other: &Self) -> 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 PartialOrd for Integer

source§

fn partial_cmp(&self, other: &Self) -> 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<'a> PrimitiveContent for &'a Integer

source§

const TAG: Tag = Tag::INTEGER

The natural tag of an encoded value of this type.
source§

fn encoded_len(&self, _mode: Mode) -> usize

Returns the length of the encoded content of this type.
source§

fn write_encoded<W: Write>( &self, _mode: Mode, target: &mut W ) -> Result<(), Error>

Writes the encoded content to a writer.
source§

fn to_encoded_bytes(&self, mode: Mode) -> Bytes

Encodes the value to bytes (useful when you need to sign a structure)
source§

fn encode(self) -> Primitive<Self>

Returns a value encoder for this content using the natural tag. Read more
source§

fn encode_as(self, tag: Tag) -> Primitive<Self>

Returns a value encoder for this content using the given tag. Read more
source§

fn encode_ref(&self) -> Primitive<&Self>

Returns a value encoder for a reference using the natural tag.
source§

fn encode_ref_as(&self, tag: Tag) -> Primitive<&Self>

Returns a value encoder for a reference using the given tag.
source§

impl<'a> TryFrom<&'a Integer> for i128

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<i128, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for i16

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<i16, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for i32

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<i32, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for i64

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<i64, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for i8

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<i8, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for u128

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<u128, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for u16

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<u16, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for u32

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<u32, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for u64

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<u64, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a Integer> for u8

§

type Error = OverflowError

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

fn try_from(val: &'a Integer) -> Result<u8, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for i128

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<i128, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for i16

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<i16, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for i32

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<i32, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for i64

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<i64, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for i8

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<i8, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for u128

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<u128, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for u16

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<u16, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for u32

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<u32, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for u64

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<u64, Self::Error>

Performs the conversion.
source§

impl TryFrom<Integer> for u8

§

type Error = OverflowError

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

fn try_from(val: Integer) -> Result<u8, Self::Error>

Performs the conversion.
source§

impl Eq for Integer

Auto Trait Implementations§

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.