Skip to main content

UInt

Struct UInt 

Source
pub struct UInt<T, const N: usize> { /* private fields */ }
Expand description

An unsigned integer constrained to N bits, backed by primitive T.

Use the aliases (u5, u13, …) rather than naming T directly. The value is always in 0..=MAX (MAX == 2^N - 1); construct with new (panicking) or try_new (checked), and read it back with value.

§Examples

use bnb::u5;

let a = u5::new(17);              // checked; panics if > 31
assert_eq!(a.value(), 17);
assert!(u5::try_new(32).is_err()); // out of range -> Err, no panic
assert_eq!(u5::from_raw(0xFF).value(), 31); // masks to the low 5 bits
assert_eq!(u5::MAX.value(), 31);
assert_eq!(u5::MIN.value(), 0);

Implementations§

Source§

impl<const N: usize> UInt<u8, N>

Source

pub const BITS: u32 = <Self as Bits>::BITS

The number of bits.

Source

pub const MASK: u8

A mask with the low N bits set.

Source

pub const MAX: Self

The largest representable value (2^N - 1).

Source

pub const MIN: Self

The zero value.

Source

pub const fn new(value: u8) -> Self

Creates a value, panicking if it does not fit in N bits.

§Panics

Panics if value > MAX.

Source

pub fn try_new(value: u8) -> Result<Self, WidthError>

Creates a value, or WidthError::ValueTooLarge if it does not fit in N bits.

Source

pub const fn from_raw(value: u8) -> Self

Creates a value from the low N bits of value, discarding any higher bits (the unchecked, masking constructor).

Source

pub const fn value(self) -> u8

The underlying value.

Source§

impl<const N: usize> UInt<u16, N>

Source

pub const BITS: u32 = <Self as Bits>::BITS

The number of bits.

Source

pub const MASK: u16

A mask with the low N bits set.

Source

pub const MAX: Self

The largest representable value (2^N - 1).

Source

pub const MIN: Self

The zero value.

Source

pub const fn new(value: u16) -> Self

Creates a value, panicking if it does not fit in N bits.

§Panics

Panics if value > MAX.

Source

pub fn try_new(value: u16) -> Result<Self, WidthError>

Creates a value, or WidthError::ValueTooLarge if it does not fit in N bits.

Source

pub const fn from_raw(value: u16) -> Self

Creates a value from the low N bits of value, discarding any higher bits (the unchecked, masking constructor).

Source

pub const fn value(self) -> u16

The underlying value.

Source§

impl<const N: usize> UInt<u32, N>

Source

pub const BITS: u32 = <Self as Bits>::BITS

The number of bits.

Source

pub const MASK: u32

A mask with the low N bits set.

Source

pub const MAX: Self

The largest representable value (2^N - 1).

Source

pub const MIN: Self

The zero value.

Source

pub const fn new(value: u32) -> Self

Creates a value, panicking if it does not fit in N bits.

§Panics

Panics if value > MAX.

Source

pub fn try_new(value: u32) -> Result<Self, WidthError>

Creates a value, or WidthError::ValueTooLarge if it does not fit in N bits.

Source

pub const fn from_raw(value: u32) -> Self

Creates a value from the low N bits of value, discarding any higher bits (the unchecked, masking constructor).

Source

pub const fn value(self) -> u32

The underlying value.

Source§

impl<const N: usize> UInt<u64, N>

Source

pub const BITS: u32 = <Self as Bits>::BITS

The number of bits.

Source

pub const MASK: u64

A mask with the low N bits set.

Source

pub const MAX: Self

The largest representable value (2^N - 1).

Source

pub const MIN: Self

The zero value.

Source

pub const fn new(value: u64) -> Self

Creates a value, panicking if it does not fit in N bits.

§Panics

Panics if value > MAX.

Source

pub fn try_new(value: u64) -> Result<Self, WidthError>

Creates a value, or WidthError::ValueTooLarge if it does not fit in N bits.

Source

pub const fn from_raw(value: u64) -> Self

Creates a value from the low N bits of value, discarding any higher bits (the unchecked, masking constructor).

Source

pub const fn value(self) -> u64

The underlying value.

Source§

impl<const N: usize> UInt<u128, N>

Source

pub const BITS: u32 = <Self as Bits>::BITS

The number of bits.

Source

pub const MASK: u128

A mask with the low N bits set.

Source

pub const MAX: Self

The largest representable value (2^N - 1).

Source

pub const MIN: Self

The zero value.

Source

pub const fn new(value: u128) -> Self

Creates a value, panicking if it does not fit in N bits.

§Panics

Panics if value > MAX.

Source

pub fn try_new(value: u128) -> Result<Self, WidthError>

Creates a value, or WidthError::ValueTooLarge if it does not fit in N bits.

Source

pub const fn from_raw(value: u128) -> Self

Creates a value from the low N bits of value, discarding any higher bits (the unchecked, masking constructor).

Source

pub const fn value(self) -> u128

The underlying value.

Trait Implementations§

Source§

impl<T, const N: usize> BitDecode for UInt<T, N>
where UInt<T, N>: Bits,

Source§

fn bit_decode<S: Source>(r: &mut S) -> Result<Self, BitError>

Decodes Self from any Source, advancing its cursor. Read more
Source§

impl<T, const N: usize> BitEncode for UInt<T, N>
where UInt<T, N>: Bits,

Source§

fn bit_encode<K: Sink>(&self, w: &mut K) -> Result<(), BitError>

Encodes self verbatim into any Sink, advancing its cursor. Read more
Source§

const LAYOUT: Layout = _

The message’s bit/byte order, used to size a fresh BitWriter when encoding to a Vec/writer. The derive sets it from the struct’s declared bit_order/bytes; a hand-written impl that only ever encodes into a caller-supplied Sink can leave the default.
Source§

fn canonical_bit_encode<K: Sink>(&self, w: &mut K) -> Result<(), BitError>

Encodes self’s canonical form into any Sink: reserved fields as their spec value, calc fields recomputed. Defaults to bit_encode (verbatim == canonical) for messages with no reserved/calc field. Read more
Source§

impl<const N: usize> Bits for UInt<u8, N>

Source§

const BITS: u32

The number of bits this value occupies on the wire.
Source§

fn into_bits(self) -> u128

This value as the low BITS bits of a u128.
Source§

fn from_bits(raw: u128) -> Self

Reconstruct from the low BITS bits of raw; any higher bits are ignored.
Source§

impl<const N: usize> Bits for UInt<u16, N>

Source§

const BITS: u32

The number of bits this value occupies on the wire.
Source§

fn into_bits(self) -> u128

This value as the low BITS bits of a u128.
Source§

fn from_bits(raw: u128) -> Self

Reconstruct from the low BITS bits of raw; any higher bits are ignored.
Source§

impl<const N: usize> Bits for UInt<u32, N>

Source§

const BITS: u32

The number of bits this value occupies on the wire.
Source§

fn into_bits(self) -> u128

This value as the low BITS bits of a u128.
Source§

fn from_bits(raw: u128) -> Self

Reconstruct from the low BITS bits of raw; any higher bits are ignored.
Source§

impl<const N: usize> Bits for UInt<u64, N>

Source§

const BITS: u32

The number of bits this value occupies on the wire.
Source§

fn into_bits(self) -> u128

This value as the low BITS bits of a u128.
Source§

fn from_bits(raw: u128) -> Self

Reconstruct from the low BITS bits of raw; any higher bits are ignored.
Source§

impl<const N: usize> Bits for UInt<u128, N>

Source§

const BITS: u32

The number of bits this value occupies on the wire.
Source§

fn into_bits(self) -> u128

This value as the low BITS bits of a u128.
Source§

fn from_bits(raw: u128) -> Self

Reconstruct from the low BITS bits of raw; any higher bits are ignored.
Source§

impl<T: Clone, const N: usize> Clone for UInt<T, N>

Source§

fn clone(&self) -> UInt<T, N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Copy, const N: usize> Copy for UInt<T, N>

Source§

impl<const N: usize> CountPrefix for UInt<u8, N>

Source§

fn try_from_count(len: usize) -> Result<Self, WidthError>

The prefix for a collection of len elements, or [WidthError::ValueTooLarge] when len exceeds the prefix’s range.
Source§

fn to_count(self) -> usize

The prefix as an element count. Read more
Source§

impl<const N: usize> CountPrefix for UInt<u16, N>

Source§

fn try_from_count(len: usize) -> Result<Self, WidthError>

The prefix for a collection of len elements, or [WidthError::ValueTooLarge] when len exceeds the prefix’s range.
Source§

fn to_count(self) -> usize

The prefix as an element count. Read more
Source§

impl<const N: usize> CountPrefix for UInt<u32, N>

Source§

fn try_from_count(len: usize) -> Result<Self, WidthError>

The prefix for a collection of len elements, or [WidthError::ValueTooLarge] when len exceeds the prefix’s range.
Source§

fn to_count(self) -> usize

The prefix as an element count. Read more
Source§

impl<const N: usize> CountPrefix for UInt<u64, N>

Source§

fn try_from_count(len: usize) -> Result<Self, WidthError>

The prefix for a collection of len elements, or [WidthError::ValueTooLarge] when len exceeds the prefix’s range.
Source§

fn to_count(self) -> usize

The prefix as an element count. Read more
Source§

impl<const N: usize> CountPrefix for UInt<u128, N>

Source§

fn try_from_count(len: usize) -> Result<Self, WidthError>

The prefix for a collection of len elements, or [WidthError::ValueTooLarge] when len exceeds the prefix’s range.
Source§

fn to_count(self) -> usize

The prefix as an element count. Read more
Source§

impl<const N: usize> Debug for UInt<u8, N>

Source§

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

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

impl<const N: usize> Debug for UInt<u16, N>

Source§

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

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

impl<const N: usize> Debug for UInt<u32, N>

Source§

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

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

impl<const N: usize> Debug for UInt<u64, N>

Source§

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

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

impl<const N: usize> Debug for UInt<u128, N>

Source§

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

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

impl<const N: usize> Default for UInt<u8, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Default for UInt<u16, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Default for UInt<u32, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Default for UInt<u64, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Default for UInt<u128, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Display for UInt<u8, N>

Source§

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

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

impl<const N: usize> Display for UInt<u16, N>

Source§

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

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

impl<const N: usize> Display for UInt<u32, N>

Source§

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

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

impl<const N: usize> Display for UInt<u64, N>

Source§

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

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

impl<const N: usize> Display for UInt<u128, N>

Source§

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

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

impl<T: Eq, const N: usize> Eq for UInt<T, N>

Source§

impl<T, const N: usize> FixedBitLen for UInt<T, N>
where UInt<T, N>: Bits,

Source§

const BIT_LEN: u32 = <Self as Bits>::BITS

Total encoded width of the message in bits — the sum of its fields’ widths.
Source§

impl<const N: usize> From<UInt<u8, N>> for u8

Source§

fn from(v: UInt<u8, N>) -> u8

Converts to this type from the input type.
Source§

impl<const N: usize> From<UInt<u16, N>> for u16

Source§

fn from(v: UInt<u16, N>) -> u16

Converts to this type from the input type.
Source§

impl<const N: usize> From<UInt<u32, N>> for u32

Source§

fn from(v: UInt<u32, N>) -> u32

Converts to this type from the input type.
Source§

impl<const N: usize> From<UInt<u64, N>> for u64

Source§

fn from(v: UInt<u64, N>) -> u64

Converts to this type from the input type.
Source§

impl<const N: usize> From<UInt<u128, N>> for u128

Source§

fn from(v: UInt<u128, N>) -> u128

Converts to this type from the input type.
Source§

impl<T: Hash, const N: usize> Hash for UInt<T, N>

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<T: Ord, const N: usize> Ord for UInt<T, N>

Source§

fn cmp(&self, other: &UInt<T, N>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

impl<T: PartialEq, const N: usize> PartialEq for UInt<T, N>

Source§

fn eq(&self, other: &UInt<T, N>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: PartialOrd, const N: usize> PartialOrd for UInt<T, N>

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: PartialEq, const N: usize> StructuralPartialEq for UInt<T, N>

Source§

impl<const N: usize> TryFrom<u8> for UInt<u8, N>

Source§

type Error = WidthError

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

fn try_from(value: u8) -> Result<Self, WidthError>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<u16> for UInt<u16, N>

Source§

type Error = WidthError

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

fn try_from(value: u16) -> Result<Self, WidthError>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<u32> for UInt<u32, N>

Source§

type Error = WidthError

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

fn try_from(value: u32) -> Result<Self, WidthError>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<u64> for UInt<u64, N>

Source§

type Error = WidthError

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

fn try_from(value: u64) -> Result<Self, WidthError>

Performs the conversion.
Source§

impl<const N: usize> TryFrom<u128> for UInt<u128, N>

Source§

type Error = WidthError

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

fn try_from(value: u128) -> Result<Self, WidthError>

Performs the conversion.

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for UInt<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for UInt<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for UInt<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for UInt<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for UInt<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for UInt<T, N>
where T: UnsafeUnpin,

§

impl<T, const N: usize> UnwindSafe for UInt<T, N>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DecodeWith<()> for T
where T: BitDecode,

Source§

fn decode_with<S>(r: &mut S, _args: ()) -> Result<T, BitError>
where S: Source,

Decodes Self from a Source given args. Read more
Source§

impl<T> EncodeExt for T
where T: BitEncode,

Source§

fn encode<W: Write>(&self, w: &mut W) -> Result<(), BitError>
where Self: Sized,

Available on crate feature std only.
Encodes self verbatim to any std::io::Write (socket, file, Vec) — exactly what’s stored. For the canonical form, encode self.to_canonical().encode(&mut w), or use the inherent to_bytes (verbatim) / to_canonical_bytes (canonical) instead. Read more
Source§

impl<T> EncodeWith<()> for T
where T: BitEncode,

Source§

fn encode_with<K>(&self, w: &mut K, _args: ()) -> Result<(), BitError>
where K: Sink,

Encodes self into a Sink given args. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> ToOwned for T
where T: Clone,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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