#[derive(Soars)]
{
// Attributes available to this derive:
#[align]
#[soa_derive]
#[soa_array]
}
Expand description
Derive macro for the Soars trait.
Deriving Soars for some struct Foo will create the following additional
structs:
| Struct | Field type | Use |
|---|---|---|
FooSoaRaw | *mut T | Low-level, unsafe memory handling for SoA |
FooRef | &T | SoA element reference |
FooRefMut | &mut T | Mutable SoA element reference |
FooSlices | &[T] | SoA fields |
FooSlicesMut | &mut [T] | Mutable SoA fields |
FooArray | [T; N] | const-compatible SoA |
FooDeref | SoA Deref target, provides slice getters |
The Soars trait implementation for Foo references these as associated
types. AsSoaRef is also implemented for Foo, FooRef, and FooRefMut.
§Arrays
The FooArray type is only generated when the #[soa_array] attribute is
added to the struct. Only structs without interior mutability support this
attribute for the time being, due to this
issue. SOA array types are
stack-allocated like normal arrays and are const-initializable.
§Derive for generated types
The soa_derive attribute can be used to derive traits for the generated
types. Copy and Clone are added automatically for FooRef and
FooSlices. In the following example, we have the following trait
implementations:
| Struct | Copy/Clone | Debug/PartialEq | Eq | PartialOrd |
|---|---|---|---|---|
FooRef | ✅ | ✅ | ✅ | |
FooRefMut | ✅ | ✅ | ✅ | |
FooSlices | ✅ | ✅ | ||
FooSlicesMut | ✅ | ✅ | ||
FooArray | ✅ | ✅ |
#[derive(Soars)]
#[soa_derive(Debug, PartialEq)]
#[soa_derive(include(Ref, RefMut), Eq)]
#[soa_derive(exclude(Ref, Slices), PartialOrd)]
struct Foo(u8);§Alignment
Individual fields can be tagged with the align attribute to raise their
alignment. The slice for that field will start at a multiple of the
requested alignment if it is greater than or equal to the alignment of the
field’s type. This can be useful for vector operations.
struct Foo(#[align(8)] u8);