Derive Macro SlabItem
#[derive(SlabItem)]
{
// Attributes available to this derive:
#[offsets]
}
Expand description
Derives SlabItem
for a struct.
use crabslab::{CpuSlab, GrowableSlab, Id, Slab, SlabItem};
#[derive(Debug, Default, PartialEq, SlabItem)]
struct Foo {
a: u32,
b: u32,
c: u32,
}
let foo_one = Foo { a: 1, b: 2, c: 3 };
let foo_two = Foo { a: 4, b: 5, c: 6 };
let mut slab = CpuSlab::new(vec![]);
let foo_one_id = slab.append(&foo_one);
let foo_two_id = slab.append(&foo_two);
// Overwrite the second item of the second `Foo`:
slab.write(Id::<u32>::new(foo_two_id.inner() + 1), &42);
assert_eq!(Foo { a: 4, b: 42, c: 6 }, slab.read(foo_two_id));
No such offsets are derived for enums.
use crabslab::{CpuSlab, GrowableSlab, Slab, SlabItem};
#[derive(Debug, Default, PartialEq, SlabItem)]
struct Bar {
a: u32,
}
#[derive(Debug, Default, PartialEq, SlabItem)]
enum Baz {
#[default]
One,
Two {
a: u32,
b: u32,
},
Three(u32, u32),
Four(Bar),
}
assert_eq!(3, Baz::SLAB_SIZE);
let mut slab = CpuSlab::new(vec![]);
let one_id = slab.append(&Baz::One);
let two_id = slab.append(&Baz::Two { a: 1, b: 2 });
let three_id = slab.append(&Baz::Three(3, 4));
let four_id = slab.append(&Baz::Four(Bar { a: 5 }));
assert_eq!(Baz::One, slab.read(one_id));
assert_eq!(Baz::Two { a: 1, b: 2 }, slab.read(two_id));
assert_eq!(Baz::Three(3, 4), slab.read(three_id));
assert_eq!(Baz::Four(Bar { a: 5 }), slab.read(four_id));