pub struct U24(/* private fields */);Expand description
A 24-bit unsigned integer type.
The U24 type represents a 24-bit unsigned integer. It provides a memory-efficient
alternative to u32 when only 24 bits of precision are needed, while offering
a much larger range than u16.
This type is particularly suited to applications such as audio processing, graphics, binary file manipulation, embedded systems, or networking protocols where 24-bit unsigned integers are common.
§Range
The valid value range is:
MIN = 0 // 0
MAX = 16_777_215 // 2^24 - 1Arithmetic operations are implemented to match Rust’s standard integer behavior, including overflow and checked variants.
§Memory Layout and Safety
U24 is implemented as a #[repr(transparent)] wrapper around a 4-byte internal representation.
Although the logical width is 24 bits, one additional byte is used for alignment and padding control.
This struct:
- Contains no uninitialized or padding bytes
- Is safe to use with
bytemuck::NoUninit - Can be cast safely with
bytemuck::cast_slicefor use in FFI and low-level applications
The layout is verified via compile-time assertions to ensure portability and correctness.
Implementations§
Source§impl U24
impl U24
Sourcepub const fn as_bits(&self) -> &u32
pub const fn as_bits(&self) -> &u32
Returns a reference to the underlying 32-bit representation.
The 24-bit value is stored in the lower 24 bits, with the upper 8 bits guaranteed to be zero. This method provides direct access to the internal bit representation for advanced use cases.
Sourcepub const fn to_u32(self) -> u32
pub const fn to_u32(self) -> u32
Converts the 24-bit unsigned integer to a 32-bit unsigned integer.
This is a zero-extending conversion.
§Returns
The 32-bit unsigned integer representation of this U24.
Sourcepub const fn wrapping_from_u32(n: u32) -> Self
pub const fn wrapping_from_u32(n: u32) -> Self
Sourcepub const fn saturating_from_u32(n: u32) -> Self
pub const fn saturating_from_u32(n: u32) -> Self
Sourcepub const fn swap_bytes(self) -> Self
pub const fn swap_bytes(self) -> Self
Reverses the byte order of the integer.
Sourcepub const fn to_le(self) -> Self
pub const fn to_le(self) -> Self
Converts self to little endian from the target’s endianness. On little endian this is a no-op. On big endian the bytes are swapped.
Sourcepub const fn to_be(self) -> Self
pub const fn to_be(self) -> Self
Converts self to big endian from the target’s endianness. On big endian this is a no-op. On little endian the bytes are swapped.
Sourcepub const fn to_ne_bytes(self) -> [u8; 3]
pub const fn to_ne_bytes(self) -> [u8; 3]
Return the memory representation of this integer as a byte array in native byte order. As the target platform’s native endianness is used, portable code should use to_be_bytes or to_le_bytes, as appropriate, instead.
Sourcepub const fn to_le_bytes(self) -> [u8; 3]
pub const fn to_le_bytes(self) -> [u8; 3]
Create a native endian integer value from its representation as a byte array in little endian.
Sourcepub const fn to_be_bytes(self) -> [u8; 3]
pub const fn to_be_bytes(self) -> [u8; 3]
Return the memory representation of this integer as a byte array in big-endian (network) byte order.
Sourcepub const fn from_ne_bytes(bytes: [u8; 3]) -> Self
pub const fn from_ne_bytes(bytes: [u8; 3]) -> Self
Sourcepub const fn from_le_bytes(bytes: [u8; 3]) -> Self
pub const fn from_le_bytes(bytes: [u8; 3]) -> Self
Sourcepub const fn from_be_bytes(bytes: [u8; 3]) -> Self
pub const fn from_be_bytes(bytes: [u8; 3]) -> Self
Sourcepub fn checked_add(self, other: Self) -> Option<Self>
pub fn checked_add(self, other: Self) -> Option<Self>
Sourcepub fn checked_sub(self, other: Self) -> Option<Self>
pub fn checked_sub(self, other: Self) -> Option<Self>
Sourcepub fn checked_mul(self, other: Self) -> Option<Self>
pub fn checked_mul(self, other: Self) -> Option<Self>
Sourcepub fn checked_div(self, other: Self) -> Option<Self>
pub fn checked_div(self, other: Self) -> Option<Self>
Sourcepub fn checked_rem(self, other: Self) -> Option<Self>
pub fn checked_rem(self, other: Self) -> Option<Self>
Sourcepub fn wrapping_add(self, rhs: Self) -> Self
pub fn wrapping_add(self, rhs: Self) -> Self
Wrapping (modular) addition. Computes self + rhs, wrapping around at the boundary of the type.
Sourcepub fn wrapping_sub(self, rhs: Self) -> Self
pub fn wrapping_sub(self, rhs: Self) -> Self
Wrapping (modular) subtraction. Computes self - rhs, wrapping around at the boundary of the type.
Sourcepub fn wrapping_mul(self, rhs: Self) -> Self
pub fn wrapping_mul(self, rhs: Self) -> Self
Wrapping (modular) multiplication. Computes self * rhs, wrapping around at the boundary of the type.
Sourcepub fn wrapping_div(self, rhs: Self) -> Self
pub fn wrapping_div(self, rhs: Self) -> Self
Wrapping (modular) division. Computes self / rhs, wrapping around at the boundary of the type.
§Panics
This function will panic if rhs is 0.
Sourcepub fn wrapping_rem(self, rhs: Self) -> Self
pub fn wrapping_rem(self, rhs: Self) -> Self
Wrapping (modular) remainder. Computes self % rhs, wrapping around at the boundary of the type.
§Panics
This function will panic if rhs is 0.
Sourcepub fn saturating_add(self, rhs: Self) -> Self
pub fn saturating_add(self, rhs: Self) -> Self
Saturating integer addition. Computes self + rhs, saturating at the numeric bounds.
Sourcepub fn saturating_sub(self, rhs: Self) -> Self
pub fn saturating_sub(self, rhs: Self) -> Self
Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds.
Sourcepub const fn saturating_mul(self, rhs: Self) -> Self
pub const fn saturating_mul(self, rhs: Self) -> Self
Saturating integer multiplication. Computes self * rhs, saturating at the numeric bounds.
Sourcepub fn saturating_div(self, rhs: Self) -> Self
pub fn saturating_div(self, rhs: Self) -> Self
Saturating integer division. Computes self / rhs, saturating at the numeric bounds.
§Panics
This function will panic if rhs is 0.
Sourcepub fn clamp(self, min: Self, max: Self) -> Self
pub fn clamp(self, min: Self, max: Self) -> Self
Restricts the value to a certain interval.
Returns max if self is greater than max, and min if self is less than min.
Otherwise, returns self.
§Panics
Panics if min > max.
Sourcepub const fn from_bytes_le(bytes: U24Bytes) -> Self
pub const fn from_bytes_le(bytes: U24Bytes) -> Self
Construct a U24 from U24Bytes in little-endian byte order. This is a convenience method for converting from the wire format.
Sourcepub const fn from_bytes_be(bytes: U24Bytes) -> Self
pub const fn from_bytes_be(bytes: U24Bytes) -> Self
Construct a U24 from U24Bytes in big-endian byte order. This is a convenience method for converting from the wire format.
Sourcepub const fn to_bytes_le(self) -> U24Bytes
pub const fn to_bytes_le(self) -> U24Bytes
Convert a U24 into U24Bytes in little-endian byte order. This is a convenience method for converting to the wire format.
Sourcepub const fn to_bytes_be(self) -> U24Bytes
pub const fn to_bytes_be(self) -> U24Bytes
Convert a U24 into U24Bytes in big-endian byte order. This is a convenience method for converting to the wire format.
Source§impl U24
impl U24
Sourcepub const fn try_from_u32(value: u32) -> Option<Self>
pub const fn try_from_u32(value: u32) -> Option<Self>
Tries to create a U24 from a u32 value.
Returns Some(U24) if the value fits within the 24-bit unsigned range [0, 16,777,215],
or None if the value is out of range.
Source§impl U24
impl U24
Sourcepub const fn try_from_u64(value: u64) -> Option<Self>
pub const fn try_from_u64(value: u64) -> Option<Self>
Tries to create a U24 from a u64 value.
Returns Some(U24) if the value fits within the 24-bit unsigned range [0, 16,777,215],
or None if the value is out of range.
Source§impl U24
impl U24
Sourcepub const fn try_from_u128(value: u128) -> Option<Self>
pub const fn try_from_u128(value: u128) -> Option<Self>
Tries to create a U24 from a u128 value.
Returns Some(U24) if the value fits within the 24-bit unsigned range [0, 16,777,215],
or None if the value is out of range.
Source§impl U24
impl U24
Sourcepub const fn try_from_i32(value: i32) -> Option<Self>
pub const fn try_from_i32(value: i32) -> Option<Self>
Tries to create a U24 from a i32 value.
Returns Some(U24) if the value fits within the 24-bit unsigned range [0, 16,777,215],
or None if the value is out of range.
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 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 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 U24where
LittleEndianU24Repr: FromPrimitive,
impl FromPrimitive for U24where
LittleEndianU24Repr: FromPrimitive,
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 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 = ParseIntError
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 for U24
impl PartialOrd for U24
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 ShlAssign<u32> for U24
impl ShlAssign<u32> for U24
Source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<= operation. Read moreSource§impl ShrAssign<u32> for U24
impl ShrAssign<u32> for U24
Source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>= operation. Read moreSource§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 ToBytes for U24
impl ToBytes for U24
type Bytes = [u8; 3]
Source§fn to_be_bytes(&self) -> Self::Bytes
fn to_be_bytes(&self) -> Self::Bytes
Source§fn to_le_bytes(&self) -> Self::Bytes
fn to_le_bytes(&self) -> Self::Bytes
Source§fn to_ne_bytes(&self) -> Self::Bytes
fn to_ne_bytes(&self) -> Self::Bytes
Source§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_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_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_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 moreimpl Copy for U24
impl Eq for U24
impl NoUninit for U24where
LittleEndianU24Repr: NoUninit,
impl StructuralPartialEq for U24
Auto Trait Implementations§
impl Freeze for U24
impl RefUnwindSafe for U24
impl Send for U24
impl Sync for U24
impl Unpin for U24
impl UnwindSafe for U24
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)