Crates.io | automap |
lib.rs | automap |
version | 0.1.0 |
source | src |
created_at | 2021-02-26 20:42:50.572012 |
updated_at | 2021-02-26 20:42:50.572012 |
description | Simple pattern to implement key-value maps where the value type contains the key type |
homepage | |
repository | https://github.com/maackle/automap-rs |
max_upload_size | |
id | 361066 |
size | 10,061 |
Simple pattern to implement key-value maps where the value type contains the key type.
Implementations for HashMap
and BTreeMap
from std::collections
are provided.
use std::collections::HashMap;
use automap::{AutoHashMap, AutoMapped};
// Let's say we want a `Person` to be keyed by their `name` in a HashMap
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct Person {
name: String,
age: u16,
}
// We can specify how to derive the key from the value
// As long as the Key type meets the bounds for a normal HashMap key, we
// can use this value in an AutoHashMap.
// (Similarly for BTreeMap.)
impl AutoMapped for Person {
type Key = String;
fn key(&self) -> &Self::Key {
&self.name
}
}
// Then, we can simply use an `AutoHashMap` to insert values directly.
let mut map = AutoHashMap::new();
let michelle = Person { name: "Michelle".into(), age: 37 };
// We don't need to provide the key, because it is derived from the value.
map.insert(michelle.clone());
// You can access all other normal HashMap methods directly:
assert_eq!(map.get("Michelle".into()), Some(&michelle));
assert_eq!(map.remove("Michelle".into()), Some(michelle));
// We can also go From and Into a normal HashMap easily.
let inner: HashMap<_, _> = map.into();
let map: AutoHashMap<_> = inner.into();
Set the "serde"
feature in your dependencies to include de/serialization support.
std::collections
.
Automapped
implementations, specifying different keys for different situations