pub struct ExtractMap<K, V, S = RandomState> { /* private fields */ }Expand description
A hash map for memory efficent storage of value types which contain their own keys.
This is achieved by the V type deriving the ExtractKey trait for their K type,
and is backed by a HashSet<Wrap<K>, V, S>, meaning this library only uses unsafe code
for performance reasons.
The default hashing algorithm is the same as the standard library’s HashSet, RandomState,
although your own hasher can be provided via ExtractMap::with_hasher and it’s similar methods.
Implementations§
Source§impl<K, V> ExtractMap<K, V, RandomState>
impl<K, V> ExtractMap<K, V, RandomState>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty ExtractMap with the RandomState hasher.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new ExtractMap with the RandomState hasher and preallocated capacity.
§Examples
use extract_map::{ExtractMap, ExtractKey};
struct User {
id: u64,
name: &'static str,
}
impl ExtractKey<u64> for User {
fn extract_key(&self) -> &u64 {
&self.id
}
}
let map = ExtractMap::<u64, User>::with_capacity(5);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 5);Source§impl<K, V, S> ExtractMap<K, V, S>
impl<K, V, S> ExtractMap<K, V, S>
Sourcepub fn with_hasher(hasher: S) -> Self
pub fn with_hasher(hasher: S) -> Self
Creates a new, empty ExtractMap with the provided hasher.
Sourcepub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
Creates a new ExtractMap with the provided hasher and preallocated capacity.
§Examples
use std::collections::hash_map::RandomState;
use extract_map::{ExtractMap, ExtractKey};
struct User {
id: u64,
name: &'static str,
}
impl ExtractKey<u64> for User {
fn extract_key(&self) -> &u64 {
&self.id
}
}
let map = ExtractMap::<u64, User>::with_capacity_and_hasher(5, RandomState::new());
assert!(map.is_empty());
assert!(map.capacity() >= 5);Source§impl<K, V, S> ExtractMap<K, V, S>
impl<K, V, S> ExtractMap<K, V, S>
Sourcepub fn insert(&mut self, value: V) -> Option<V>
pub fn insert(&mut self, value: V) -> Option<V>
Inserts a value into the ExtractMap.
This extracts the key from the value using the ExtractKey trait, and therefore does not need a key to be provided.
§Examples
use extract_map::{ExtractMap, ExtractKey};
struct User {
id: u64,
name: &'static str,
}
impl ExtractKey<u64> for User {
fn extract_key(&self) -> &u64 {
&self.id
}
}
let mut map = ExtractMap::new();
map.insert(User { id: 1, name: "Daisy" });
map.insert(User { id: 2, name: "Elliott" });
assert_eq!(map.len(), 2);Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a value from the ExtractMap.
§Examples
use extract_map::{ExtractMap, ExtractKey};
#[derive(Debug, Clone, PartialEq)]
struct User {
id: u64,
name: &'static str,
}
impl ExtractKey<u64> for User {
fn extract_key(&self) -> &u64 {
&self.id
}
}
let user = User { id: 1, name: "Daisy" };
let mut map = ExtractMap::new();
map.insert(user.clone());
assert_eq!(map.remove(&1), Some(user));
assert!(map.is_empty())Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Checks if a value is in the ExtractMap.
Sourcepub fn get_mut<'a>(&'a mut self, key: &K) -> Option<MutGuard<'a, K, V, S>>
pub fn get_mut<'a>(&'a mut self, key: &K) -> Option<MutGuard<'a, K, V, S>>
Retrieves a mutable guard to a value in the ExtractMap.
This guard is required as the current implementation takes the value out of the map and reinserts on Drop to allow mutation of the key field.
Source§impl<K, V, S> ExtractMap<K, V, S>
impl<K, V, S> ExtractMap<K, V, S>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Retrieves the number of remaining values that can be inserted before a reallocation.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Retrieves the number of values currently in the ExtractMap.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Retrieves if the ExtractMap contains no values.
Sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
Retrieves an iterator over the borrowed values.
If you need an iterator over the keys and values, simply use ExtractKey.
Use IntoIterator::into_iter for an iterator over owned values.
Trait Implementations§
Source§impl<K, V, S> Debug for ExtractMap<K, V, S>
impl<K, V, S> Debug for ExtractMap<K, V, S>
Source§impl<K, V> Default for ExtractMap<K, V, RandomState>
impl<K, V> Default for ExtractMap<K, V, RandomState>
Source§impl<K, V, S> Extend<V> for ExtractMap<K, V, S>
impl<K, V, S> Extend<V> for ExtractMap<K, V, S>
Source§fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)