diff --git a/phf/src/map.rs b/phf/src/map.rs index cc218f71..11a3ee18 100644 --- a/phf/src/map.rs +++ b/phf/src/map.rs @@ -102,6 +102,11 @@ impl Map { } } + /// Like `get`, but returns both the key and the value. + pub fn get_kv(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv { + self.get_entry(key, |k| key.equiv(k)) + } + /// Like `get`, but can operate on any type that is equivalent to a key. pub fn get_equiv(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv { self.get_entry(key, |k| key.equiv(k)).map(|e| &e.1) @@ -113,6 +118,12 @@ impl Map { self.get_entry(key, |k| key.equiv(k)).map(|e| &e.0) } + /// Like `get_kv`, but can operate on any type that is equivalent to a + /// key. + pub fn get_kv_equiv(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv { + self.get_entry(key, |k| key.equiv(k)) + } + /// Returns an iterator over the key/value pairs in the map. /// /// Entries are retuned in an arbitrary but fixed order. @@ -203,5 +214,3 @@ impl<'a, K, V> DoubleEndedIterator<&'a V> for Values<'a, K, V> { } impl<'a, K, V> ExactSize<&'a V> for Values<'a, K, V> {} - - diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index b2ccd369..84387043 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -115,6 +115,11 @@ impl OrderedMap { } } + /// Like `get`, but returns both the key and the value. + pub fn get_kv(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv { + self.get_entry(key, |k| key.equiv(k)).map(|(_, r)| r) + } + /// Like `get`, but can operate on any type that is equivalent to a key. pub fn get_equiv(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv { self.get_entry(key, |k| key.equiv(k)).map(|(_, e)| &e.1) @@ -132,6 +137,12 @@ impl OrderedMap { self.get_entry(key, |k| key.equiv(k)).map(|(i, _)| i) } + /// Like `get_kv`, but can operate on any type that is equivalent to a + /// key. + pub fn get_kv_equiv(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv { + self.get_entry(key, |k| key.equiv(k)).map(|(_, r)| r) + } + /// Returns an iterator over the key/value pairs in the map. /// /// Entries are returned in the same order in which they were defined. @@ -252,4 +263,3 @@ impl<'a, K, V> RandomAccessIterator<&'a V> for Values<'a, K, V> { } impl<'a, K, V> ExactSize<&'a V> for Values<'a, K, V> {} -