#[repr(C, align(4))]pub struct u24 { /* private fields */ }Expand description
An unsigned little-endian encoded 24-bit integer.
§Memory Layout
The u24 type has the same size (4 bytes), alignment (4 bytes), and memory layout as a
little-endian encoded u32. The most significant byte is always zero, ensuring that
the value never exceeds the 24-bit range.
Memory layout (little-endian):
[byte0] [byte1] [byte2] [0x00]This layout ensures that u24 values can be safely transmuted to/from u32 values
while maintaining the 24-bit constraint.
§Examples
use u24::u24;
// Create from literal values
let val = u24!(0x123456);
assert_eq!(val.into_u32(), 0x00_123456);
// Create from byte array
let val = u24::from_le_bytes([0x56, 0x34, 0x12]);
assert_eq!(val.into_u32(), 0x00_123456);
// Convert back to bytes
assert_eq!(val.to_le_bytes(), [0x56, 0x34, 0x12]);
// Arithmetic operations
let a = u24!(1000);
let b = u24!(2000);
let sum = a + b;
assert_eq!(sum, u24!(3000));Implementations§
Source§impl u24
impl u24
Sourcepub const fn from_le_bytes(bytes: [u8; 3]) -> Self
pub const fn from_le_bytes(bytes: [u8; 3]) -> Self
Creates a u24 from a little-endian byte array.
§Examples
use u24::u24;
let val = u24::from_le_bytes([0x34, 0x12, 0xAB]);
assert_eq!(val.into_u32(), 0x00_AB1234);Sourcepub const fn to_le_bytes(self) -> [u8; 3]
pub const fn to_le_bytes(self) -> [u8; 3]
Returns the memory representation of this u24 as a little-endian byte array.
§Examples
use u24::u24;
let val = u24::truncating_from_u32(0x00_AB1234);
assert_eq!(val.to_le_bytes(), [0x34, 0x12, 0xAB]);Sourcepub const fn into_u32(self) -> u32
pub const fn into_u32(self) -> u32
Converts this u24 to a u32 representation.
§Examples
use u24::u24;
let val = u24::from_le_bytes([0x34, 0x12, 0xAB]);
assert_eq!(val.into_u32(), 0x00_AB1234);
assert_eq!(u24::MAX.into_u32(), 0x00_FFFFFF);
assert_eq!(u24::MIN.into_u32(), 0x00_000000);Sourcepub const fn truncating_from_u32(v: u32) -> Self
pub const fn truncating_from_u32(v: u32) -> Self
Creates a u24 from a u32, truncating the most significant bytes if
necessary.
§Examples
use u24::u24;
let val = u24::truncating_from_u32(0x01_234567);
assert_eq!(val.into_u32(), 0x00_234567);
let val = u24::truncating_from_u32(0xFF_FFFFFF);
assert_eq!(val.into_u32(), 0x00_FFFFFF);Sourcepub const fn checked_from_u32(v: u32) -> Option<Self>
pub const fn checked_from_u32(v: u32) -> Option<Self>
Creates a u24 from a u32 if it fits, otherwise returns None.
This function returns None if the input value is greater than u24::MAX.
§Examples
use u24::u24;
assert_eq!(u24::checked_from_u32(0x00_FFFFFF), Some(u24::MAX));
assert_eq!(u24::checked_from_u32(0x01_000000), None);
assert_eq!(u24::checked_from_u32(0), Some(u24::MIN));Sourcepub const fn saturating_from_u32(v: u32) -> Self
pub const fn saturating_from_u32(v: u32) -> Self
Creates a u24 from a u32, saturating at the bounds.
If the input value is greater than u24::MAX, returns u24::MAX.
§Examples
use u24::u24;
assert_eq!(u24::saturating_from_u32(0x00_FFFFFF), u24::MAX);
assert_eq!(u24::saturating_from_u32(0x01_000000), u24::MAX);
assert_eq!(u24::saturating_from_u32(0x00_123456), u24::truncating_from_u32(0x00_123456));Sourcepub const fn checked_add(self, other: Self) -> Option<Self>
pub const fn checked_add(self, other: Self) -> Option<Self>
Checked integer addition. Returns None on overflow.
§Examples
use u24::u24;
assert_eq!(u24!(100).checked_add(u24!(50)), Some(u24!(150)));
assert_eq!(u24::MAX.checked_add(u24!(1)), None);Sourcepub const fn checked_sub(self, other: Self) -> Option<Self>
pub const fn checked_sub(self, other: Self) -> Option<Self>
Checked integer subtraction. Returns None on underflow.
§Examples
use u24::u24;
assert_eq!(u24!(100).checked_sub(u24!(50)), Some(u24!(50)));
assert_eq!(u24!(0).checked_sub(u24!(1)), None);Sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
Checked integer multiplication. Returns None on overflow.
§Examples
use u24::u24;
assert_eq!(u24!(100).checked_mul(u24!(200)), Some(u24!(20000)));
assert_eq!(u24::MAX.checked_mul(u24!(2)), None);Sourcepub const fn checked_div(self, other: Self) -> Option<Self>
pub const fn checked_div(self, other: Self) -> Option<Self>
Checked integer division. Returns None if other is zero.
§Examples
use u24::u24;
assert_eq!(u24!(100).checked_div(u24!(5)), Some(u24!(20)));
assert_eq!(u24!(100).checked_div(u24!(0)), None);Sourcepub const fn saturating_add(self, other: Self) -> Self
pub const fn saturating_add(self, other: Self) -> Self
Saturating integer addition. Clamps the result at the maximum value.
§Examples
use u24::u24;
assert_eq!(u24!(100).saturating_add(u24!(50)), u24!(150));
assert_eq!(u24::MAX.saturating_add(u24!(1)), u24::MAX);Sourcepub const fn saturating_sub(self, other: Self) -> Self
pub const fn saturating_sub(self, other: Self) -> Self
Saturating integer subtraction. Clamps the result at the minimum value.
§Examples
use u24::u24;
assert_eq!(u24!(100).saturating_sub(u24!(50)), u24!(50));
assert_eq!(u24!(10).saturating_sub(u24!(50)), u24::MIN);Sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Saturating integer multiplication. Clamps the result at the maximum value.
§Examples
use u24::u24;
assert_eq!(u24!(100).saturating_mul(u24!(200)), u24!(20000));
assert_eq!(u24::MAX.saturating_mul(u24!(2)), u24::MAX);Trait Implementations§
Source§impl AddAssign<&u24> for u24
impl AddAssign<&u24> for u24
Source§fn add_assign(&mut self, rhs: &u24)
fn add_assign(&mut self, rhs: &u24)
+= operation. Read moreSource§impl AddAssign for u24
impl AddAssign for u24
Source§fn add_assign(&mut self, rhs: u24)
fn add_assign(&mut self, rhs: u24)
+= operation. Read moreSource§impl AsPrimitive<i16> for u24
impl AsPrimitive<i16> for u24
Source§impl AsPrimitive<i32> for u24
impl AsPrimitive<i32> for u24
Source§impl AsPrimitive<i64> for u24
impl AsPrimitive<i64> for u24
Source§impl AsPrimitive<i8> for u24
impl AsPrimitive<i8> for u24
Source§impl AsPrimitive<u16> for u24
impl AsPrimitive<u16> for u24
Source§impl AsPrimitive<u24> for i16
impl AsPrimitive<u24> for i16
Source§impl AsPrimitive<u24> for i32
impl AsPrimitive<u24> for i32
Source§impl AsPrimitive<u24> for i64
impl AsPrimitive<u24> for i64
Source§impl AsPrimitive<u24> for i8
impl AsPrimitive<u24> for i8
Source§impl AsPrimitive<u24> for u16
impl AsPrimitive<u24> for u16
Source§impl AsPrimitive<u24> for u24
impl AsPrimitive<u24> for u24
Source§impl AsPrimitive<u24> for u32
impl AsPrimitive<u24> for u32
Source§impl AsPrimitive<u24> for u64
impl AsPrimitive<u24> for u64
Source§impl AsPrimitive<u24> for u8
impl AsPrimitive<u24> for u8
Source§impl AsPrimitive<u24> for usize
impl AsPrimitive<u24> for usize
Source§impl AsPrimitive<u32> for u24
impl AsPrimitive<u32> for u24
Source§impl AsPrimitive<u64> for u24
impl AsPrimitive<u64> for u24
Source§impl AsPrimitive<u8> for u24
impl AsPrimitive<u8> for u24
Source§impl AsPrimitive<usize> for u24
impl AsPrimitive<usize> for u24
Source§impl BitAndAssign<&u24> for u24
impl BitAndAssign<&u24> for u24
Source§fn bitand_assign(&mut self, rhs: &u24)
fn bitand_assign(&mut self, rhs: &u24)
&= operation. Read moreSource§impl BitAndAssign for u24
impl BitAndAssign for u24
Source§fn bitand_assign(&mut self, rhs: u24)
fn bitand_assign(&mut self, rhs: u24)
&= operation. Read moreSource§impl BitOrAssign<&u24> for u24
impl BitOrAssign<&u24> for u24
Source§fn bitor_assign(&mut self, rhs: &u24)
fn bitor_assign(&mut self, rhs: &u24)
|= operation. Read moreSource§impl BitOrAssign for u24
impl BitOrAssign for u24
Source§fn bitor_assign(&mut self, rhs: u24)
fn bitor_assign(&mut self, rhs: u24)
|= operation. Read moreSource§impl BitXorAssign<&u24> for u24
impl BitXorAssign<&u24> for u24
Source§fn bitxor_assign(&mut self, rhs: &u24)
fn bitxor_assign(&mut self, rhs: &u24)
^= operation. Read moreSource§impl BitXorAssign for u24
impl BitXorAssign for u24
Source§fn bitxor_assign(&mut self, rhs: u24)
fn bitxor_assign(&mut self, rhs: u24)
^= operation. Read moreSource§impl CheckedAdd for u24
impl CheckedAdd for u24
Source§fn checked_add(&self, other: &Self) -> Option<u24>
fn checked_add(&self, other: &Self) -> Option<u24>
None is
returned.Source§impl CheckedDiv for u24
impl CheckedDiv for u24
Source§fn checked_div(&self, other: &Self) -> Option<u24>
fn checked_div(&self, other: &Self) -> Option<u24>
None is returned.Source§impl CheckedMul for u24
impl CheckedMul for u24
Source§fn checked_mul(&self, other: &Self) -> Option<u24>
fn checked_mul(&self, other: &Self) -> Option<u24>
None is returned.Source§impl CheckedSub for u24
impl CheckedSub for u24
Source§fn checked_sub(&self, other: &Self) -> Option<u24>
fn checked_sub(&self, other: &Self) -> Option<u24>
None is returned.Source§impl DivAssign<&u24> for u24
impl DivAssign<&u24> for u24
Source§fn div_assign(&mut self, rhs: &u24)
fn div_assign(&mut self, rhs: &u24)
/= operation. Read moreSource§impl DivAssign for u24
impl DivAssign for u24
Source§fn div_assign(&mut self, rhs: u24)
fn div_assign(&mut self, rhs: u24)
/= operation. Read moreSource§impl FromPrimitive for u24
impl FromPrimitive for u24
Source§fn from_i64(n: i64) -> Option<Self>
fn from_i64(n: i64) -> Option<Self>
i64 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u64(n: u64) -> Option<Self>
fn from_u64(n: u64) -> Option<Self>
u64 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_isize(n: isize) -> Option<Self>
fn from_isize(n: isize) -> Option<Self>
isize to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i8(n: i8) -> Option<Self>
fn from_i8(n: i8) -> Option<Self>
i8 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i16(n: i16) -> Option<Self>
fn from_i16(n: i16) -> Option<Self>
i16 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i32(n: i32) -> Option<Self>
fn from_i32(n: i32) -> Option<Self>
i32 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i128(n: i128) -> Option<Self>
fn from_i128(n: i128) -> Option<Self>
i128 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned. Read moreSource§fn from_usize(n: usize) -> Option<Self>
fn from_usize(n: usize) -> Option<Self>
usize to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u8(n: u8) -> Option<Self>
fn from_u8(n: u8) -> Option<Self>
u8 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u16(n: u16) -> Option<Self>
fn from_u16(n: u16) -> Option<Self>
u16 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u32(n: u32) -> Option<Self>
fn from_u32(n: u32) -> Option<Self>
u32 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u128(n: u128) -> Option<Self>
fn from_u128(n: u128) -> Option<Self>
u128 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned. Read moreSource§impl IntoBytes for u24
impl IntoBytes for u24
Source§impl MulAssign<&u24> for u24
impl MulAssign<&u24> for u24
Source§fn mul_assign(&mut self, rhs: &u24)
fn mul_assign(&mut self, rhs: &u24)
*= operation. Read moreSource§impl MulAssign for u24
impl MulAssign for u24
Source§fn mul_assign(&mut self, rhs: u24)
fn mul_assign(&mut self, rhs: u24)
*= operation. Read moreSource§impl Num for u24
impl Num for u24
type FromStrRadixErr = ParseU24Err
Source§fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
2..=36). Read moreSource§impl Ord for u24
impl Ord for u24
Source§impl PartialOrd<i16> for u24
impl PartialOrd<i16> for u24
Source§impl PartialOrd<i32> for u24
impl PartialOrd<i32> for u24
Source§impl PartialOrd<i64> for u24
impl PartialOrd<i64> for u24
Source§impl PartialOrd<i8> for u24
impl PartialOrd<i8> for u24
Source§impl PartialOrd<u16> for u24
impl PartialOrd<u16> for u24
Source§impl PartialOrd<u32> for u24
impl PartialOrd<u32> for u24
Source§impl PartialOrd<u64> for u24
impl PartialOrd<u64> for u24
Source§impl PartialOrd<u8> for u24
impl PartialOrd<u8> for u24
Source§impl PartialOrd<usize> for u24
impl PartialOrd<usize> for u24
Source§impl PartialOrd for u24
impl PartialOrd for u24
Source§impl PrimInt for u24
impl PrimInt for u24
Source§fn count_ones(self) -> u32
fn count_ones(self) -> u32
self. Read moreSource§fn count_zeros(self) -> u32
fn count_zeros(self) -> u32
self. Read moreSource§fn leading_zeros(self) -> u32
fn leading_zeros(self) -> u32
self. Read moreSource§fn trailing_zeros(self) -> u32
fn trailing_zeros(self) -> u32
self. Read moreSource§fn rotate_left(self, n: u32) -> Self
fn rotate_left(self, n: u32) -> Self
n, wrapping
the truncated bits to the end of the resulting integer. Read moreSource§fn rotate_right(self, n: u32) -> Self
fn rotate_right(self, n: u32) -> Self
n, wrapping
the truncated bits to the beginning of the resulting integer. Read moreSource§fn signed_shl(self, n: u32) -> Self
fn signed_shl(self, n: u32) -> Self
n, filling
zeros in the least significant bits. Read moreSource§fn signed_shr(self, n: u32) -> Self
fn signed_shr(self, n: u32) -> Self
n, copying
the “sign bit” in the most significant bits even for unsigned types. Read moreSource§fn unsigned_shl(self, n: u32) -> Self
fn unsigned_shl(self, n: u32) -> Self
n, filling
zeros in the least significant bits. Read moreSource§fn unsigned_shr(self, n: u32) -> Self
fn unsigned_shr(self, n: u32) -> Self
n, filling
zeros in the most significant bits. Read moreSource§fn swap_bytes(self) -> Self
fn swap_bytes(self) -> Self
Source§fn from_be(x: Self) -> Self
fn from_be(x: Self) -> Self
Source§fn from_le(x: Self) -> Self
fn from_le(x: Self) -> Self
Source§fn pow(self, exp: u32) -> Self
fn pow(self, exp: u32) -> Self
exp, using exponentiation by squaring. Read moreSource§fn leading_ones(self) -> u32
fn leading_ones(self) -> u32
self. Read moreSource§fn trailing_ones(self) -> u32
fn trailing_ones(self) -> u32
self. Read moreSource§fn reverse_bits(self) -> Self
fn reverse_bits(self) -> Self
Source§impl RemAssign<&u24> for u24
impl RemAssign<&u24> for u24
Source§fn rem_assign(&mut self, rhs: &u24)
fn rem_assign(&mut self, rhs: &u24)
%= operation. Read moreSource§impl RemAssign for u24
impl RemAssign for u24
Source§fn rem_assign(&mut self, rhs: u24)
fn rem_assign(&mut self, rhs: u24)
%= operation. Read moreSource§impl Saturating for u24
impl Saturating for u24
Source§fn saturating_add(self, v: Self) -> Self
fn saturating_add(self, v: Self) -> Self
Source§fn saturating_sub(self, v: Self) -> Self
fn saturating_sub(self, v: Self) -> Self
Source§impl SaturatingAdd for u24
impl SaturatingAdd for u24
Source§fn saturating_add(&self, other: &Self) -> u24
fn saturating_add(&self, other: &Self) -> u24
self + other, saturating at the relevant high or low boundary of
the type.Source§impl SaturatingMul for u24
impl SaturatingMul for u24
Source§fn saturating_mul(&self, other: &Self) -> u24
fn saturating_mul(&self, other: &Self) -> u24
self * other, saturating at the relevant high or low boundary of
the type.Source§impl SaturatingSub for u24
impl SaturatingSub for u24
Source§fn saturating_sub(&self, other: &Self) -> u24
fn saturating_sub(&self, other: &Self) -> u24
self - other, saturating at the relevant high or low boundary of
the type.Source§impl SubAssign<&u24> for u24
impl SubAssign<&u24> for u24
Source§fn sub_assign(&mut self, rhs: &u24)
fn sub_assign(&mut self, rhs: &u24)
-= operation. Read moreSource§impl SubAssign for u24
impl SubAssign for u24
Source§fn sub_assign(&mut self, rhs: u24)
fn sub_assign(&mut self, rhs: u24)
-= operation. Read moreSource§impl ToPrimitive for u24
impl ToPrimitive for u24
Source§fn to_i64(&self) -> Option<i64>
fn to_i64(&self) -> Option<i64>
self to an i64. If the value cannot be
represented by an i64, then None is returned.Source§fn to_u64(&self) -> Option<u64>
fn to_u64(&self) -> Option<u64>
self to a u64. If the value cannot be
represented by a u64, then None is returned.Source§fn to_u32(&self) -> Option<u32>
fn to_u32(&self) -> Option<u32>
self to a u32. If the value cannot be
represented by a u32, then None is returned.Source§fn to_isize(&self) -> Option<isize>
fn to_isize(&self) -> Option<isize>
self to an isize. If the value cannot be
represented by an isize, then None is returned.Source§fn to_i8(&self) -> Option<i8>
fn to_i8(&self) -> Option<i8>
self to an i8. If the value cannot be
represented by an i8, then None is returned.Source§fn to_i16(&self) -> Option<i16>
fn to_i16(&self) -> Option<i16>
self to an i16. If the value cannot be
represented by an i16, then None is returned.Source§fn to_i32(&self) -> Option<i32>
fn to_i32(&self) -> Option<i32>
self to an i32. If the value cannot be
represented by an i32, then None is returned.Source§fn to_i128(&self) -> Option<i128>
fn to_i128(&self) -> Option<i128>
self to an i128. If the value cannot be
represented by an i128 (i64 under the default implementation), then
None is returned. Read moreSource§fn to_usize(&self) -> Option<usize>
fn to_usize(&self) -> Option<usize>
self to a usize. If the value cannot be
represented by a usize, then None is returned.Source§fn to_u8(&self) -> Option<u8>
fn to_u8(&self) -> Option<u8>
self to a u8. If the value cannot be
represented by a u8, then None is returned.Source§fn to_u16(&self) -> Option<u16>
fn to_u16(&self) -> Option<u16>
self to a u16. If the value cannot be
represented by a u16, then None is returned.Source§fn to_u128(&self) -> Option<u128>
fn to_u128(&self) -> Option<u128>
self to a u128. If the value cannot be
represented by a u128 (u64 under the default implementation), then
None is returned. Read more