Skip to main content

PackedValue

Trait PackedValue 

Source
pub unsafe trait PackedValue:
    'static
    + Copy
    + Send
    + Sync {
    type Value: Packable;

    const WIDTH: usize;
Show 18 methods // Required methods fn from_fn<F>(f: F) -> Self where F: FnMut(usize) -> Self::Value; fn from_slice(slice: &[Self::Value]) -> &Self; fn from_slice_mut(slice: &mut [Self::Value]) -> &mut Self; fn as_slice(&self) -> &[Self::Value]; fn as_slice_mut(&mut self) -> &mut [Self::Value]; // Provided methods fn broadcast(value: Self::Value) -> Self { ... } fn extract(&self, lane: usize) -> Self::Value { ... } fn pack_slice(buf: &[Self::Value]) -> &[Self] { ... } 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(buf: &[Self::Value]) -> (&[Self], &[Self::Value]) { ... } 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] { ... } fn pack_columns<const N: usize>(rows: &[[Self::Value; N]]) -> [Self; N] { ... } fn pack_columns_fn<const N: usize>( row_fn: impl Fn(usize) -> [Self::Value; N], ) -> [Self; N] { ... } fn unpack_into<const N: usize>( packed: &[Self; N], rows: &mut [[Self::Value; N]], ) { ... } fn unpack_iter<const N: usize>( packed: [Self; N], ) -> impl Iterator<Item = [Self::Value; N]> { ... }
}
Expand description

A trait for array-like structs made up of multiple scalar elements.

§Safety

  • If P implements PackedField then P must be castable to/from [P::Value; P::WIDTH] without UB.

Required Associated Constants§

Source

const WIDTH: usize

Number of scalar values packed together.

Required Associated Types§

Source

type Value: Packable

The scalar type that is packed into this value.

Required Methods§

Source

fn from_fn<F>(f: F) -> Self
where F: FnMut(usize) -> Self::Value,

Constructs a packed value using a function to generate each element.

Similar to core::array::from_fn.

Source

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

Source

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

Source

fn as_slice(&self) -> &[Self::Value]

Returns the underlying scalar values as an immutable slice.

Source

fn as_slice_mut(&mut self) -> &mut [Self::Value]

Returns the underlying scalar values as a mutable slice.

Provided Methods§

Source

fn broadcast(value: Self::Value) -> Self

Create a packed value with all lanes set to the same scalar value.

Source

fn extract(&self, lane: usize) -> Self::Value

Extract the scalar value at the given SIMD lane.

This is equivalent to self.as_slice()[lane] but more explicit about the SIMD extraction semantics.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.
Source

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.
Source

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.

Source

fn pack_columns<const N: usize>(rows: &[[Self::Value; N]]) -> [Self; N]

Pack columns from WIDTH rows of scalar values into N packed values.

Given WIDTH rows of N scalar values, extract each column and pack it into a single packed value. This is the inverse of unpack_into.

§Panics

Panics if rows.len() != WIDTH.

Source

fn pack_columns_fn<const N: usize>( row_fn: impl Fn(usize) -> [Self::Value; N], ) -> [Self; N]

Pack columns using a closure that provides each row’s data.

Calls row_fn(lane) for each lane 0..WIDTH to get [Self::Value; N], then transposes columns into packed values. Useful when rows aren’t contiguous in memory (e.g., strided access).

Source

fn unpack_into<const N: usize>( packed: &[Self; N], rows: &mut [[Self::Value; N]], )

Unpack N packed values into WIDTH rows of N scalars.

§Inputs
  • packed: An array of N packed values.
  • rows: A mutable slice of exactly WIDTH arrays to write the unpacked values.
§Panics

Panics if rows.len() != WIDTH.

Source

fn unpack_iter<const N: usize>( packed: [Self; N], ) -> impl Iterator<Item = [Self::Value; N]>

Unpack N packed values into an iterator of WIDTH rows.

This is the iterator equivalent of unpack_into, yielding each row without requiring a pre-allocated buffer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: Packable, const WIDTH: usize> PackedValue for [T; WIDTH]

Source§

const WIDTH: usize = WIDTH

Source§

type Value = T

Source§

fn from_slice(slice: &[Self::Value]) -> &Self

Source§

fn from_slice_mut(slice: &mut [Self::Value]) -> &mut Self

Source§

fn from_fn<Fn>(f: Fn) -> Self
where Fn: FnMut(usize) -> Self::Value,

Source§

fn as_slice(&self) -> &[Self::Value]

Source§

fn as_slice_mut(&mut self) -> &mut [Self::Value]

Implementors§