pub unsafe trait PackedValue:
'static
+ Copy
+ Send
+ Sync {
type Value: Packable;
const WIDTH: usize;
// Required methods
fn from_slice(slice: &[Self::Value]) -> &Self;
fn from_slice_mut(slice: &mut [Self::Value]) -> &mut Self;
fn from_fn<F>(f: F) -> Self
where F: FnMut(usize) -> Self::Value;
fn as_slice(&self) -> &[Self::Value];
fn as_slice_mut(&mut self) -> &mut [Self::Value];
// Provided methods
fn pack_slice(buf: &[Self::Value]) -> &[Self] { ... }
fn pack_slice_with_suffix(buf: &[Self::Value]) -> (&[Self], &[Self::Value]) { ... }
fn pack_slice_mut(buf: &mut [Self::Value]) -> &mut [Self] { ... }
fn pack_maybe_uninit_slice_mut(
buf: &mut [MaybeUninit<Self::Value>],
) -> &mut [MaybeUninit<Self>] { ... }
fn pack_slice_with_suffix_mut(
buf: &mut [Self::Value],
) -> (&mut [Self], &mut [Self::Value]) { ... }
fn pack_maybe_uninit_slice_with_suffix_mut(
buf: &mut [MaybeUninit<Self::Value>],
) -> (&mut [MaybeUninit<Self>], &mut [MaybeUninit<Self::Value>]) { ... }
fn unpack_slice(buf: &[Self]) -> &[Self::Value] { ... }
}
Expand description
A trait for array-like structs made up of multiple scalar elements.
§Safety
- If
P
implementsPackedField
thenP
must be castable to/from[P::Value; P::WIDTH]
without UB.
Required Associated Constants§
Required Associated Types§
Required Methods§
Sourcefn from_slice(slice: &[Self::Value]) -> &Self
fn from_slice(slice: &[Self::Value]) -> &Self
Interprets a slice of scalar values as a packed value reference.
§Panics:
This function will panic if slice.len() != Self::WIDTH
Sourcefn from_slice_mut(slice: &mut [Self::Value]) -> &mut Self
fn from_slice_mut(slice: &mut [Self::Value]) -> &mut Self
Interprets a mutable slice of scalar values as a mutable packed value.
§Panics:
This function will panic if slice.len() != Self::WIDTH
Sourcefn from_fn<F>(f: F) -> Self
fn from_fn<F>(f: F) -> Self
Constructs a packed value using a function to generate each element.
Similar to core:array::from_fn
.
Sourcefn as_slice(&self) -> &[Self::Value]
fn as_slice(&self) -> &[Self::Value]
Returns the underlying scalar values as an immutable slice.
Sourcefn as_slice_mut(&mut self) -> &mut [Self::Value]
fn as_slice_mut(&mut self) -> &mut [Self::Value]
Returns the underlying scalar values as a mutable slice.
Provided Methods§
Sourcefn pack_slice(buf: &[Self::Value]) -> &[Self]
fn pack_slice(buf: &[Self::Value]) -> &[Self]
Packs a slice of scalar values into a slice of packed values.
§Panics
Panics if the slice length is not divisible by WIDTH
.
Sourcefn pack_slice_with_suffix(buf: &[Self::Value]) -> (&[Self], &[Self::Value])
fn pack_slice_with_suffix(buf: &[Self::Value]) -> (&[Self], &[Self::Value])
Packs a slice into packed values and returns the packed portion and any remaining suffix.
Sourcefn pack_slice_mut(buf: &mut [Self::Value]) -> &mut [Self]
fn pack_slice_mut(buf: &mut [Self::Value]) -> &mut [Self]
Converts a mutable slice of scalar values into a mutable slice of packed values.
§Panics
Panics if the slice length is not divisible by WIDTH
.
Sourcefn pack_maybe_uninit_slice_mut(
buf: &mut [MaybeUninit<Self::Value>],
) -> &mut [MaybeUninit<Self>]
fn pack_maybe_uninit_slice_mut( buf: &mut [MaybeUninit<Self::Value>], ) -> &mut [MaybeUninit<Self>]
Converts a mutable slice of possibly uninitialized scalar values into a mutable slice of possibly uninitialized packed values.
§Panics
Panics if the slice length is not divisible by WIDTH
.
Sourcefn pack_slice_with_suffix_mut(
buf: &mut [Self::Value],
) -> (&mut [Self], &mut [Self::Value])
fn pack_slice_with_suffix_mut( buf: &mut [Self::Value], ) -> (&mut [Self], &mut [Self::Value])
Converts a mutable slice of scalar values into a pair:
- a slice of packed values covering the largest aligned portion,
- and a remainder slice of scalar values that couldn’t be packed.
Sourcefn pack_maybe_uninit_slice_with_suffix_mut(
buf: &mut [MaybeUninit<Self::Value>],
) -> (&mut [MaybeUninit<Self>], &mut [MaybeUninit<Self::Value>])
fn pack_maybe_uninit_slice_with_suffix_mut( buf: &mut [MaybeUninit<Self::Value>], ) -> (&mut [MaybeUninit<Self>], &mut [MaybeUninit<Self::Value>])
Converts a mutable slice of possibly uninitialized scalar values into a pair:
- a slice of possibly uninitialized packed values covering the largest aligned portion,
- and a remainder slice of possibly uninitialized scalar values that couldn’t be packed.
Sourcefn unpack_slice(buf: &[Self]) -> &[Self::Value]
fn unpack_slice(buf: &[Self]) -> &[Self::Value]
Reinterprets a slice of packed values as a flat slice of scalar values.
Each packed value contains Self::WIDTH
scalar values, which are laid out
contiguously in memory. This function allows direct access to those scalars.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.