Skip to main content

DirectKey

Trait DirectKey 

Source
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 == b exactly when a.index() == b.index() and a.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§

Source

type Tag: Copy + Eq

What a slot stores besides the value: the part of the key that the slot index does not give.

Required Methods§

Source

fn tag(self) -> Self::Tag

The part of this key that its index does not give.

Source

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".

Implementations on Foreign Types§

Source§

impl DirectKey for u8

Source§

type Tag = ()

Source§

fn tag(self)

Source§

fn from_parts(index: usize, (): ()) -> Self

Source§

impl DirectKey for u16

Source§

type Tag = ()

Source§

fn tag(self)

Source§

fn from_parts(index: usize, (): ()) -> Self

Source§

impl DirectKey for u32

Source§

type Tag = ()

Source§

fn tag(self)

Source§

fn from_parts(index: usize, (): ()) -> Self

Source§

impl DirectKey for u64

Source§

type Tag = ()

Source§

fn tag(self)

Source§

fn from_parts(index: usize, (): ()) -> Self

Source§

impl DirectKey for usize

Source§

type Tag = ()

Source§

fn tag(self)

Source§

fn from_parts(index: usize, (): ()) -> Self

Implementors§

Source§

impl DirectKey for Id

Available on 32-bit or 64-bit only.

A direct map stores only the generation; its NonZeroU32 niche keeps Option<(NonZeroU32, V)> as small as (u32, V).