mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-24 00:31:34 +11:00
Also do Borrow shenanigans for get and get_key_value
This commit is contained in:
parent
150b1a3078
commit
39edc4ab36
|
@ -342,7 +342,11 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the key-value pair corresponding to the supplied key
|
/// Returns the key-value pair corresponding to the supplied key
|
||||||
pub fn get_key_value(&self, key: &K) -> Option<(&K, &V)> {
|
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
|
||||||
|
where
|
||||||
|
K: Borrow<Q>,
|
||||||
|
Q: Hash + Eq + ?Sized,
|
||||||
|
{
|
||||||
let hash = self.hash(key);
|
let hash = self.hash(key);
|
||||||
|
|
||||||
self.nodes
|
self.nodes
|
||||||
|
@ -352,7 +356,21 @@ where
|
||||||
|
|
||||||
/// Returns a reference to the value corresponding to the key. Returns [`None`] if there is
|
/// Returns a reference to the value corresponding to the key. Returns [`None`] if there is
|
||||||
/// no element in the map with the given key.
|
/// no element in the map with the given key.
|
||||||
pub fn get(&self, key: &K) -> Option<&V> {
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use agb_hashmap::HashMap;
|
||||||
|
///
|
||||||
|
/// let mut map = HashMap::new();
|
||||||
|
/// map.insert("a".to_string(), "A");
|
||||||
|
/// assert_eq!(map.get("a"), Some(&"A"));
|
||||||
|
/// assert_eq!(map.get("b"), None);
|
||||||
|
/// ```
|
||||||
|
pub fn get<Q>(&self, key: &Q) -> Option<&V>
|
||||||
|
where
|
||||||
|
K: Borrow<Q>,
|
||||||
|
Q: Hash + Eq + ?Sized,
|
||||||
|
{
|
||||||
self.get_key_value(key).map(|(_, v)| v)
|
self.get_key_value(key).map(|(_, v)| v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue