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, WidthError>
pub fn try_new(value: u8) -> Result<Self, WidthError>
Creates a value, or WidthError::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, WidthError>
pub fn try_new(value: u16) -> Result<Self, WidthError>
Creates a value, or WidthError::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, WidthError>
pub fn try_new(value: u32) -> Result<Self, WidthError>
Creates a value, or WidthError::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, WidthError>
pub fn try_new(value: u64) -> Result<Self, WidthError>
Creates a value, or WidthError::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, WidthError>
pub fn try_new(value: u128) -> Result<Self, WidthError>
Creates a value, or WidthError::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>
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: 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>
Source§impl<const N: usize> TryFrom<u8> for UInt<u8, N>
impl<const N: usize> TryFrom<u8> for UInt<u8, N>
Source§type Error = WidthError
type Error = WidthError
The type returned in the event of a conversion error.
Source§impl<const N: usize> TryFrom<u16> for UInt<u16, N>
impl<const N: usize> TryFrom<u16> for UInt<u16, N>
Source§type Error = WidthError
type Error = WidthError
The type returned in the event of a conversion error.
Source§impl<const N: usize> TryFrom<u32> for UInt<u32, N>
impl<const N: usize> TryFrom<u32> for UInt<u32, N>
Source§type Error = WidthError
type Error = WidthError
The type returned in the event of a conversion error.
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 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