Skip to content

Commit f3a9f21

Browse files
committed
Auto merge of #335 - JustForFun88:pub_map_inner, r=Amanieu
Adding function for getting access to map `table: RawTable<(K, V), A>` field I was a little interested in issues "API for removing by key hash and value" #330. The original question was about adding a fancy API, and of course it wasn't merged (#334). However, it seems to me that this shows that it would be nice to just add the ability to access to map's fields. And let the user himself do what he pleases. The user will still not be able to change the existing API. But at the same time, he will have the opportunity to add small extensions. Otherwise, when using `RawTable`, he will have to not only add one or two functions, but generally recreate all the `HashMap`’s APIs, which can be difficult.
2 parents 7c518c7 + c773eb2 commit f3a9f21

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/map.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,67 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
20122012
pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S, A> {
20132013
RawEntryBuilder { map: self }
20142014
}
2015+
2016+
/// Returns a mutable reference to the [`RawTable`] used underneath [`HashMap`].
2017+
/// This function is only available if the `raw` feature of the crate is enabled.
2018+
///
2019+
/// # Note
2020+
///
2021+
/// Calling the function safe, but using raw hash table API's may require
2022+
/// unsafe functions or blocks.
2023+
///
2024+
/// `RawTable` API gives the lowest level of control under the map that can be useful
2025+
/// for extending the HashMap's API, but may lead to *[undefined behavior]*.
2026+
///
2027+
/// [`HashMap`]: struct.HashMap.html
2028+
/// [`RawTable`]: raw/struct.RawTable.html
2029+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
2030+
///
2031+
/// # Examples
2032+
///
2033+
/// ```
2034+
/// use core::hash::{BuildHasher, Hash};
2035+
/// use hashbrown::HashMap;
2036+
///
2037+
/// let mut map = HashMap::new();
2038+
/// map.extend([("a", 10), ("b", 20), ("c", 30)]);
2039+
/// assert_eq!(map.len(), 3);
2040+
///
2041+
/// // Let's imagine that we have a value and a hash of the key, but not the key itself.
2042+
/// // However, if you want to remove the value from the map by hash and value, and you
2043+
/// // know exactly that the value is unique, then you can create a function like this:
2044+
/// fn remove_by_hash<K, V, S, F>(
2045+
/// map: &mut HashMap<K, V, S>,
2046+
/// hash: u64,
2047+
/// is_match: F,
2048+
/// ) -> Option<(K, V)>
2049+
/// where
2050+
/// F: Fn(&(K, V)) -> bool,
2051+
/// {
2052+
/// let raw_table = map.raw_table();
2053+
/// match raw_table.find(hash, is_match) {
2054+
/// Some(bucket) => Some(unsafe { raw_table.remove(bucket) }),
2055+
/// None => None,
2056+
/// }
2057+
/// }
2058+
///
2059+
/// fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
2060+
/// use core::hash::Hasher;
2061+
/// let mut state = hash_builder.build_hasher();
2062+
/// key.hash(&mut state);
2063+
/// state.finish()
2064+
/// }
2065+
///
2066+
/// let hash = compute_hash(map.hasher(), "a");
2067+
/// assert_eq!(remove_by_hash(&mut map, hash, |(_, v)| *v == 10), Some(("a", 10)));
2068+
/// assert_eq!(map.get(&"a"), None);
2069+
/// assert_eq!(map.len(), 2);
2070+
/// ```
2071+
#[cfg(feature = "raw")]
2072+
#[cfg_attr(feature = "inline-more", inline)]
2073+
pub fn raw_table(&mut self) -> &mut RawTable<(K, V), A> {
2074+
&mut self.table
2075+
}
20152076
}
20162077

20172078
impl<K, V, S, A> PartialEq for HashMap<K, V, S, A>

0 commit comments

Comments
 (0)