Skip to main content

Crate soavec

Crate soavec 

Source
Expand description

A vector-like data-structure for convenient growable Struct-of-Arrays creation and manipulation.

The SoAVec type is recommended for use in places where multiple structs need to be stored on the heap and their access to or iteration of is done mostly field-wise as opposed to all-fields-together.

§Basic usage

The SoAble trait can be derived on structs to conveniently split them up into Struct-of-Arrays format field-wise and to generate wrapper types for the as_ref, as_mut, as_slice, and as_mut_slice methods’ return types. For advanced usage, the SoAble trait can be implemented manually.

When deriving the SoAble trait it is recommended to write the struct definition in order of alignment and size, eg. a u64 field should come before a [u32; 3] field. If the SoAble trait is implemented manually then the struct’s layout ordering does not matter but the chosen [tuple representation] should still uphold this same order. This ensures that the SoAVec does not need to add extra padding between field slices.

§Example

use soavec::soavec;
use soavec_derive::SoAble;

#[derive(Clone, SoAble)]
struct Basic {
  a: Vec<u32>,
  b: usize,
  c: u16,
  d: bool,
}

let mut vec = soavec![Basic { a: vec![1, 2, 3], b: 55, c: 4, d: false }; 32].unwrap();
let BasicSlice {
  a,
  b,
  c,
  d,
} = vec.as_slice();
assert_eq!(a, vec![vec![1, 2, 3]; 32]);
assert_eq!(b, vec![55usize; 32]);
assert_eq!(c, vec![4u16; 32]);
assert_eq!(d, vec![false; 32]);

Macros§

soavec
Creates a SoAVec containing the arguments.

Structs§

IndexOutOfBoundsError
The error type for when an index is out of bounds.
SoAVec
A contiguous growable Struct-of-Arrays type, written as SoAVec<T>, short for ‘Struct-of-Arrays vector’.

Enums§

InsertError
A union of possible errors that can occur when inserting into a SoAVec.

Traits§

SoATuple
A trait for working with Struct-of-Arrays tuples. This enables working with Struct-of-Arrays allocations in a way similar to normal vector allocations.
SoAble
Used for defining the format and API of a type stored in SoAVec in a Struct-of-Arrays format.