[−][src]Crate slots
Data structure with only constant time operations
Slots implements a "heapless", fixed size, unordered data structure where elements can only be modified using the key retrieved when storing them. Slots behaves similarly to a stack, except the key can be used to retrieve (and delete) elements without restriction.
Example usage:
use slots::Slots; use slots::consts::U6; let mut slots: Slots<_, U6> = Slots::new(); // Capacity of 6 elements // Store elements let k1 = slots.store(2).unwrap(); // returns Err(2) if full let k2 = slots.store(4).unwrap(); // Remove first element let first = slots.take(k1); // k1 is consumed and can no longer be used assert_eq!(2, first); // Read element without modification let three = slots.read(&k2, |&e| e-1); // closure can be used to transform element assert_eq!(3, three); // Try to read from an index without the key: let this_will_be_none = slots.try_read(5, |&e| e); // closure *is not* called because index is not used assert_eq!(None, this_will_be_none); // Try to read from an index extracted from the key: let index = k2.index(); // this will only allow us to read since there are no guarantees the item will be valid let this_will_be_five = slots.try_read(index, |&e| e+1).unwrap(); //closure *is* called assert_eq!(5, this_will_be_five); // Modify a stored element let three = slots.modify(&k2, |e| { *e = 2 + *e; 3 }); assert_eq!(3, three); // Information about the storage assert_eq!(6, slots.capacity()); assert_eq!(1, slots.count());
Modules
consts | Type aliases for many constants. |
Structs
Key | |
Slots |
Traits
ArrayLength | Trait making |