Crate any_key [] [src]

Dynamically typed keys for associative arrays.


use std::collections::{BTreeMap, HashMap};
use any_key::{AnyHash, AnyOrd};

#[derive(PartialEq, Eq, Hash, PartialOrd, Ord)]
struct Foo;

// AnyHash can be used as a key for HashMap-like types
let mut map = HashMap::new();
map.insert(Box::new("hello") as Box<AnyHash>, 1);
map.insert(Box::new(42) as Box<AnyHash>, 2);
map.insert(Box::new(Foo) as Box<AnyHash>, 3);
assert_eq!(map.get(&(Box::new("hello") as Box<AnyHash>)), Some(&1));
assert_eq!(map.get(&(Box::new(42) as Box<AnyHash>)), Some(&2));
assert_eq!(map.get(&(Box::new(Foo) as Box<AnyHash>)), Some(&3));

// AnyOrd can be used as a key for HashMap-like types
let mut map = BTreeMap::new();
map.insert(Box::new("hello") as Box<AnyOrd>, 1);
map.insert(Box::new(42) as Box<AnyOrd>, 2);
map.insert(Box::new(Foo) as Box<AnyOrd>, 3);
assert_eq!(map.get(&(Box::new("hello") as Box<AnyOrd>)), Some(&1));
assert_eq!(map.get(&(Box::new(42) as Box<AnyOrd>)), Some(&2));
assert_eq!(map.get(&(Box::new(Foo) as Box<AnyOrd>)), Some(&3));



Work around the inability of Hash to accept unsized Hashers.



Object-safe trait for dynamically typed hashable keys.


Object-safe trait for dynamically typed totally ordered keys.