pub trait DirectKey: Key {
type Tag: Copy + Eq;
// Required methods
fn tag(self) -> Self::Tag;
fn from_parts(index: usize, tag: Self::Tag) -> Self;
}Expand description
A Key that its slot index and a small tag determine, so a direct map
stores only the tag in each slot, not the whole key.
PagedDirectMap keeps
Option<(K::Tag, V)> in each slot and rebuilds keys with
from_parts. For plain integers the index is the
whole key and the tag is (), so a u32 -> u64 map uses 16-byte slots
instead of 24. A generational id’s tag is its generation. A key that its
index does not help to rebuild can use type Tag = Self, which stores the
whole key as before.
§Contract
For all keys a and b:
a == bexactly whena.index() == b.index()anda.tag() == b.tag();K::from_parts(a.index(), a.tag()) == a.
Neither tag nor from_parts may panic: the map calls from_parts only
with the parts of a key it was given, while iterating or removing.
Breaking the contract cannot cause undefined behaviour, but maps may then
return wrong keys or results.
use paged_collections::direct_map::PagedDirectMap;
use paged_collections::key::{DirectKey, Key};
/// 32-bit entity id: low 24 bits are the index, high 8 bits the generation.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct Entity(u32);
impl Key for Entity {
fn index(self) -> usize {
(self.0 & 0x00ff_ffff) as usize
}
}
impl DirectKey for Entity {
type Tag = u8;
fn tag(self) -> u8 {
(self.0 >> 24) as u8
}
fn from_parts(index: usize, tag: u8) -> Self {
Entity(index as u32 | u32::from(tag) << 24)
}
}
let mut map: PagedDirectMap<Entity, &str> = PagedDirectMap::new();
map.insert(Entity(5 | 1 << 24), "second generation");
assert_eq!(map.keys().next(), Some(Entity(5 | 1 << 24)));
assert_eq!(map.get(Entity(5)), None);Required Associated Types§
Required Methods§
Sourcefn from_parts(index: usize, tag: Self::Tag) -> Self
fn from_parts(index: usize, tag: Self::Tag) -> Self
The key with slot index index and tag tag.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".