Trait slotmap::Key

source ·
pub unsafe trait Key: From<KeyData> + Copy + Clone + Default + Eq + PartialEq + Ord + PartialOrd + Hash + Debug {
    // Required method
    fn data(&self) -> KeyData;

    // Provided methods
    fn null() -> Self { ... }
    fn is_null(&self) -> bool { ... }
}
Expand description

Key used to access stored values in a slot map.

Do not use a key from one slot map in another. The behavior is safe but non-sensical (and might panic in case of out-of-bounds).

To prevent this, it is suggested to have a unique key type for each slot map. You can create new key types using new_key_type!, which makes a new type identical to DefaultKey, just with a different name.

This trait is intended to be a thin wrapper around KeyData, and all methods must behave exactly as if we’re operating on a KeyData directly. The internal unsafe code relies on this, therefore this trait is unsafe to implement. It is strongly suggested to simply use new_key_type! instead of implementing this trait yourself.

Required Methods§

source

fn data(&self) -> KeyData

Gets the KeyData stored in this key.

Examples
new_key_type! { struct MyKey; }
let dk = DefaultKey::null();
let mk = MyKey::null();
assert_eq!(dk.data(), mk.data());

Provided Methods§

source

fn null() -> Self

Creates a new key that is always invalid and distinct from any non-null key. A null key can only be created through this method (or default initialization of keys made with new_key_type!, which calls this method).

A null key is always invalid, but an invalid key (that is, a key that has been removed from the slot map) does not become a null key. A null is safe to use with any safe method of any slot map instance.

Examples
let mut sm = SlotMap::new();
let k = sm.insert(42);
let nk = DefaultKey::null();
assert!(nk.is_null());
assert!(k != nk);
assert_eq!(sm.get(nk), None);
source

fn is_null(&self) -> bool

Checks if a key is null. There is only a single null key, that is a.is_null() && b.is_null() implies a == b.

Examples
new_key_type! { struct MyKey; }
let a = MyKey::null();
let b = MyKey::default();
assert_eq!(a, b);
assert!(a.is_null());

Object Safety§

This trait is not object safe.

Implementors§