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>
impl<const N: usize> UInt<u8, N>
Sourcepub fn try_new(value: u8) -> Result<Self>
pub fn try_new(value: u8) -> Result<Self>
Creates a value, or Error::ValueTooLarge if it does not
fit in N bits.
Source§impl<const N: usize> UInt<u16, N>
impl<const N: usize> UInt<u16, N>
Sourcepub fn try_new(value: u16) -> Result<Self>
pub fn try_new(value: u16) -> Result<Self>
Creates a value, or Error::ValueTooLarge if it does not
fit in N bits.
Source§impl<const N: usize> UInt<u32, N>
impl<const N: usize> UInt<u32, N>
Sourcepub fn try_new(value: u32) -> Result<Self>
pub fn try_new(value: u32) -> Result<Self>
Creates a value, or Error::ValueTooLarge if it does not
fit in N bits.
Source§impl<const N: usize> UInt<u64, N>
impl<const N: usize> UInt<u64, N>
Sourcepub fn try_new(value: u64) -> Result<Self>
pub fn try_new(value: u64) -> Result<Self>
Creates a value, or Error::ValueTooLarge if it does not
fit in N bits.
Source§impl<const N: usize> UInt<u128, N>
impl<const N: usize> UInt<u128, N>
Sourcepub fn try_new(value: u128) -> Result<Self>
pub fn try_new(value: u128) -> Result<Self>
Creates a value, or Error::ValueTooLarge if it does not
fit in N bits.
Trait Implementations§
Source§impl<T, const N: usize> BitEncode for UInt<T, N>
impl<T, const N: usize> BitEncode for UInt<T, N>
Source§fn canonical_bit_encode<K: Sink>(&self, w: &mut K) -> Result<(), BitError>
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 moreSource§fn encode_mode(&self) -> EncodeMode
fn encode_mode(&self) -> EncodeMode
The form
EncodeExt::encode writes for this value. Defaults to
EncodeMode::Verbatim; a #[bin] message with a reserved/calc field carries a
settable encode_mode and overrides this to return it.impl<T: Copy, const N: usize> Copy for UInt<T, N>
impl<T: Eq, const N: usize> Eq for UInt<T, N>
Source§impl<T, const N: usize> FixedBitLen for UInt<T, N>
impl<T, const N: usize> FixedBitLen for UInt<T, N>
Source§impl<T: Ord, const N: usize> Ord for UInt<T, N>
impl<T: Ord, const N: usize> Ord for UInt<T, N>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialEq, const N: usize> PartialEq for UInt<T, N>
impl<T: PartialEq, const N: usize> PartialEq for UInt<T, N>
Source§impl<T: PartialOrd, const N: usize> PartialOrd for UInt<T, N>
impl<T: PartialOrd, const N: usize> PartialOrd for UInt<T, N>
impl<T: PartialEq, const N: usize> StructuralPartialEq for UInt<T, N>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DecodeWith<()> for Twhere
T: BitDecode,
impl<T> DecodeWith<()> for Twhere
T: BitDecode,
Source§impl<T> EncodeExt for Twhere
T: BitEncode,
impl<T> EncodeExt for Twhere
T: BitEncode,
Source§fn encode<W: Write>(&self, w: &mut W) -> Result<(), BitError>where
Self: Sized,
fn encode<W: Write>(&self, w: &mut W) -> Result<(), BitError>where
Self: Sized,
Available on crate feature
std only.Encodes
self to any std::io::Write (socket, file, Vec) in the value’s
encode_mode — verbatim unless its mode is set to
Canonical. For an unconditional choice, use the inherent
to_bytes (verbatim) / to_canonical_bytes (canonical) instead. Read more