#slab #derive #wrapped-slab

macro wrapped_slab_derive

WrappedSlab: Auto-generate newtype idiom based on Slab

7 unstable releases (3 breaking)

new 0.4.0 Apr 17, 2025
0.3.0 Apr 16, 2025
0.2.2 Jul 25, 2023
0.1.1 Jul 19, 2023

#39 in #slab

Download history 1/week @ 2025-01-11 1/week @ 2025-01-18 1/week @ 2025-01-25 4/week @ 2025-02-01 6/week @ 2025-02-08 4/week @ 2025-02-15 3/week @ 2025-03-22 6/week @ 2025-03-29 14/week @ 2025-04-05 193/week @ 2025-04-12

216 downloads per month
Used in wrapped_slab

MIT license

15KB
228 lines

WrappedSlab Tests

Also available on crates.io.

Very simple Rust library useful when you want stronger type guarantees than Slab's usize keys. Generates TSlab(Slab<T>) that accepts TKey instead of usize. TVacantEntry(VacantEntry<T>) is also generated along the same lines. This should be a drop-in replacement for Slab<T>, provided all the keys are changed from usize to TKey.

Example

use wrapped_slab::WrappedSlab;

#[derive(WrappedSlab)]
struct TestUnitStruct(String);

fn main() {
    let mut slab = TestUnitStructSlab::default();
    let key: TestUnitStructKey = slab.insert(TestUnitStruct("testing".into()));
    let val: Option<&TestUnitStruct> = slab.get(key);
    let next_entry: TestUnitStructVacantEntry = slab.vacant_entry();
    let next_key: TestUnitStructKey = next_entry.key();
    let next_entry_ref: &mut TestUnitStruct = next_entry.insert(TestUnitStruct(format!("{next_key:?}")));

    // See wrapped_slab/tests/ for more examples
}

Dependencies

~1.5MB
~39K SLoC