Expand description
The integer types: i24, i40, i48, i56, i72 through i120, and
the unsigned equivalents u24, u40, … u120.
Each type is a fixed-width integer whose width is a whole number of bytes but not one of the native widths (8, 16, 32, 64 or 128 bits). For those, use the primitive types.
§Layout
Every type has the same size and alignment as the primitive that backs it
(i24 and u24 use i32 and u32; i40 through i56 use 64-bit
integers; i72 through i120 use 128-bit integers). The unused upper
bytes are always zero. That leaves invalid bit patterns, so
Option<i24> is usually the same size as i24.
§Creating values
new: checked, returnsNoneif the value doesn’t fitnew_truncated: wraps like anascast, never failsnew_unchecked:unsafe, for values already known to be in rangefrom_bits:unsafe, takes the raw, pre-masked bit pattern- the constant macros in
literal: checked at compile time
get returns the value in the backing type, sign-extended for signed
types. to_bits returns the raw, unextended pattern.
§Conversions
Every integer type in this crate, including the primitives, converts to
every other. Conversions that can never fail implement From; the rest
implement TryFrom with TryFromIntError.
§Examples
use nbyte_int::prelude::*;
let x = i24::new(-5).unwrap();
assert_eq!(x.get(), -5);
// out of range
assert!(i24::new(i32::MAX).is_none());
assert_eq!(i24::MAX.get(), 8_388_607);
// same size as the backing type, and Option is free (usually - not a guarantee)
assert_eq!(size_of::<i24>(), size_of::<i32>());
assert_eq!(size_of::<Option<u40>>(), size_of::<u40>());
// lossless conversions use `From`, the rest `TryFrom`
assert_eq!(i24::from(100_i16).get(), 100);
assert!(u24::try_from(-1_i32).is_err());
assert_eq!(u24::try_from(70_000_i64).unwrap().get(), 70_000);
// byte order
assert_eq!(u24::new(0x010203).unwrap().to_le_bytes(), [0x03, 0x02, 0x01]);