Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,67 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S, A> {
RawEntryBuilder { map: self }
}

/// Returns a mutable reference to the [`RawTable`] used underneath [`HashMap`].
/// This function is only available if the `raw` feature of the crate is enabled.
///
/// # Note
///
/// Calling the function safe, but using raw hash table API's may require
/// unsafe functions or blocks.
///
/// `RawTable` API gives the lowest level of control under the map that can be useful
/// for extending the HashMap's API, but may lead to *[undefined behavior]*.
///
/// [`HashMap`]: struct.HashMap.html
/// [`RawTable`]: raw/struct.RawTable.html
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
///
/// # Examples
///
/// ```
/// use core::hash::{BuildHasher, Hash};
/// use hashbrown::HashMap;
///
/// let mut map = HashMap::new();
/// map.extend([("a", 10), ("b", 20), ("c", 30)]);
/// assert_eq!(map.len(), 3);
///
/// // Let's imagine that we have a value and a hash of the key, but not the key itself.
/// // However, if you want to remove the value from the map by hash and value, and you
/// // know exactly that the value is unique, then you can create a function like this:
/// fn remove_by_hash<K, V, S, F>(
/// map: &mut HashMap<K, V, S>,
/// hash: u64,
/// is_match: F,
/// ) -> Option<(K, V)>
/// where
/// F: Fn(&(K, V)) -> bool,
/// {
/// let raw_table = map.raw_table();
/// match raw_table.find(hash, is_match) {
/// Some(bucket) => Some(unsafe { raw_table.remove(bucket) }),
/// None => None,
/// }
/// }
///
/// fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
/// use core::hash::Hasher;
/// let mut state = hash_builder.build_hasher();
/// key.hash(&mut state);
/// state.finish()
/// }
///
/// let hash = compute_hash(map.hasher(), "a");
/// assert_eq!(remove_by_hash(&mut map, hash, |(_, v)| *v == 10), Some(("a", 10)));
/// assert_eq!(map.get(&"a"), None);
/// assert_eq!(map.len(), 2);
/// ```
#[cfg(feature = "raw")]
#[cfg_attr(feature = "inline-more", inline)]
pub fn raw_table(&mut self) -> &mut RawTable<(K, V), A> {
&mut self.table
}
}

impl<K, V, S, A> PartialEq for HashMap<K, V, S, A>
Expand Down