Skip to main content

PackedInt

Struct PackedInt 

Source
pub struct PackedInt { /* private fields */ }
Expand description

An unsigned integer packed into 16 bits with variable precision.

See the crate-level documentation for the encoding, the rounding behaviour, and the ordering guarantee.

Implementations§

Source§

impl PackedInt

Source

pub const fn from_12_bits(bits: &[u8; 2]) -> PackedInt

Reads a packed value from 12 bits: all of bits[0], and the high nibble of bits[1].

The low nibble of bits[1] is reserved for the caller and is ignored.

Source

pub const fn to_12_bits(self) -> [u8; 2]

Writes the packed value into 12 bits: all of bits[0], and the high nibble of bits[1].

The low nibble of bits[1] is left zero for the caller to use.

Twelve bits hold only the low nibble of the exponent, so this is lossless for values up to 511 × 2¹⁴ (8 372 224) and no further. Beyond that the exponent is truncated and the value reads back as an unrelated number.

Source

pub const fn from_16_bits(bits: &[u8; 2]) -> PackedInt

Reads a packed value from two little-endian bytes.

Source

pub const fn to_16_bits(self) -> [u8; 2]

Writes the packed value as two little-endian bytes.

Source

pub const fn from_inner_u16(inner: u16) -> PackedInt

Reinterprets a u16 as a packed value.

Every u16 is a valid representation, so this cannot fail. It is the inverse of to_inner_u16.

Source

pub const fn to_inner_u16(self) -> u16

Returns the underlying representation.

Source

pub const fn from_usize(value: usize) -> PackedInt

Packs a usize, rounding up.

Returns the smallest representable value greater than or equal to value, which is value itself whenever it is below 256.

Source

pub const fn from_u128(value: u128) -> PackedInt

Packs a u128, rounding up.

Returns the smallest representable value greater than or equal to value, which is value itself whenever it is below 256.

Source

pub const fn from_u64(value: u64) -> PackedInt

Packs a u64, rounding up.

Returns the smallest representable value greater than or equal to value, which is value itself whenever it is below 256.

Source

pub const fn from_u32(value: u32) -> PackedInt

Packs a u32, rounding up.

Returns the smallest representable value greater than or equal to value, which is value itself whenever it is below 256.

Source

pub const fn from_u16(value: u16) -> PackedInt

Packs a u16, rounding up.

Returns the smallest representable value greater than or equal to value, which is value itself whenever it is below 256.

Source

pub const fn to_usize(self) -> usize

Unpacks into a usize, saturating at usize::MAX.

Values that do not fit in the target type saturate rather than wrap.

Source

pub const fn to_u128(self) -> u128

Unpacks into a u128, saturating at u128::MAX.

Values that do not fit in the target type saturate rather than wrap.

Source

pub const fn to_u64(self) -> u64

Unpacks into a u64, saturating at u64::MAX.

Values that do not fit in the target type saturate rather than wrap.

Source

pub const fn to_u32(self) -> u32

Unpacks into a u32, saturating at u32::MAX.

Values that do not fit in the target type saturate rather than wrap.

Source

pub const fn to_u16(self) -> u16

Unpacks into a u16, saturating at u16::MAX.

Values that do not fit in the target type saturate rather than wrap.

Trait Implementations§

Source§

impl Clone for PackedInt

Source§

fn clone(&self) -> PackedInt

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for PackedInt

Source§

impl Debug for PackedInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for PackedInt

Source§

fn default() -> PackedInt

Returns the “default value” for a type. Read more
Source§

impl Eq for PackedInt

Source§

impl From<u32> for PackedInt

Source§

fn from(value: u32) -> PackedInt

Converts to this type from the input type.
Source§

impl From<u64> for PackedInt

Source§

fn from(value: u64) -> PackedInt

Converts to this type from the input type.
Source§

impl From<u128> for PackedInt

Source§

fn from(value: u128) -> PackedInt

Converts to this type from the input type.
Source§

impl From<usize> for PackedInt

Source§

fn from(value: usize) -> PackedInt

Converts to this type from the input type.
Source§

impl Hash for PackedInt

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for PackedInt

Source§

fn cmp(&self, other: &PackedInt) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for PackedInt

Source§

fn eq(&self, other: &PackedInt) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for PackedInt

Source§

fn partial_cmp(&self, other: &PackedInt) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for PackedInt

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToResult for T

Source§

fn ok<Err>(self) -> Result<Self, Err>
where Self: Sized,

Wraps self in Ok(self).
Source§

fn err<Any>(self) -> Result<Any, Self>
where Self: Sized,

Wraps self in Err(self).
Source§

fn some(self) -> Option<Self>
where Self: Sized,

Wraps self in Some(self).
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.