usecrate::alloc::{Allocator, Global}; usecrate::raw::{Bucket, RawDrain, RawExtractIf, RawIntoIter, RawIter, RawTable}; usecrate::{DefaultHashBuilder, Equivalent, TryReserveError}; use core::borrow::Borrow; use core::fmt::{self, Debug}; use core::hash::{BuildHasher, Hash}; use core::iter::FusedIterator; use core::marker::PhantomData; use core::mem; use core::ops::Index; use stdalloc::borrow::ToOwned;
/// A hash map implemented with quadratic probing and SIMD lookup. /// /// The default hashing algorithm is currently [`foldhash`], though this is /// subject to change at any point in the future. This hash function is very /// fast for all types of keys, but this algorithm will typically *not* protect /// against attacks such as HashDoS. /// /// The hashing algorithm can be replaced on a per-`HashMap` basis using the /// [`default`], [`with_hasher`], and [`with_capacity_and_hasher`] methods. Many /// alternative algorithms are available on crates.io, such as the [`fnv`] crate. /// /// It is required that the keys implement the [`Eq`] and [`Hash`] traits, although /// this can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`. /// If you implement these yourself, it is important that the following /// property holds: /// /// ```text /// k1 == k2 -> hash(k1) == hash(k2) /// ``` /// /// In other words, if two keys are equal, their hashes must be equal. /// /// It is a logic error for a key to be modified in such a way that the key's /// hash, as determined by the [`Hash`] trait, or its equality, as determined by /// the [`Eq`] trait, changes while it is in the map. This is normally only /// possible through [`Cell`], [`RefCell`], global state, I/O, or unsafe code. /// /// It is also a logic error for the [`Hash`] implementation of a key to panic. /// This is generally only possible if the trait is implemented manually. If a /// panic does occur then the contents of the `HashMap` may become corrupted and /// some items may be dropped from the table. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// // Type inference lets us omit an explicit type signature (which /// // would be `HashMap<String, String>` in this example). /// let mut book_reviews = HashMap::new(); /// /// // Review some books. /// book_reviews.insert( /// "Adventures of Huckleberry Finn".to_string(), /// "My favorite book.".to_string(), /// ); /// book_reviews.insert( /// "Grimms' Fairy Tales".to_string(), /// "Masterpiece.".to_string(), /// ); /// book_reviews.insert( /// "Pride and Prejudice".to_string(), /// "Very enjoyable.".to_string(), /// ); /// book_reviews.insert( /// "The Adventures of Sherlock Holmes".to_string(), /// "Eye lyked it alot.".to_string(), /// ); /// /// // Check for a specific one. /// // When collections store owned values (String), they can still be /// // queried using references (&str). /// if !book_reviews.contains_key("Les Misérables") { /// println!("We've got {} reviews, but Les Misérables ain't one.", /// book_reviews.len()); /// } /// /// // oops, this review has a lot of spelling mistakes, let's delete it. /// book_reviews.remove("The Adventures of Sherlock Holmes"); /// /// // Look up the values associated with some keys. /// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"]; /// for &book in &to_find { /// match book_reviews.get(book) { /// Some(review) => println!("{}: {}", book, review), /// None => println!("{} is unreviewed.", book) /// } /// } /// /// // Look up the value for a key (will panic if the key is not found). /// println!("Review for Jane: {}", book_reviews["Pride and Prejudice"]); /// /// // Iterate over everything. /// for (book, review) in &book_reviews { /// println!("{}: \"{}\"", book, review); /// } /// ``` /// /// `HashMap` also implements an [`Entry API`](#method.entry), which allows /// for more complex methods of getting, setting, updating and removing keys and /// their values: /// /// ``` /// use hashbrown::HashMap; /// /// // type inference lets us omit an explicit type signature (which /// // would be `HashMap<&str, u8>` in this example). /// let mut player_stats = HashMap::new(); /// /// fn random_stat_buff() -> u8 { /// // could actually return some random value here - let's just return /// // some fixed value for now /// 42 /// } /// /// // insert a key only if it doesn't already exist /// player_stats.entry("health").or_insert(100); /// /// // insert a key using a function that provides a new value only if it /// // doesn't already exist /// player_stats.entry("defence").or_insert_with(random_stat_buff); /// /// // update a key, guarding against the key possibly not being set /// let stat = player_stats.entry("attack").or_insert(100); /// *stat += random_stat_buff(); /// ``` /// /// The easiest way to use `HashMap` with a custom key type is to derive [`Eq`] and [`Hash`]. /// We must also derive [`PartialEq`]. /// /// [`RefCell`]: std::cell::RefCell /// [`Cell`]: std::cell::Cell /// [`default`]: Default::default /// [`with_hasher`]: HashMap::with_hasher /// [`with_capacity_and_hasher`]: HashMap::with_capacity_and_hasher /// [`fnv`]: https://crates.io/crates/fnv /// [`foldhash`]: https://crates.io/crates/foldhash /// /// ``` /// use hashbrown::HashMap; /// /// #[derive(Hash, Eq, PartialEq, Debug)] /// struct Viking { /// name: String, /// country: String, /// } /// /// impl Viking { /// /// Creates a new Viking. /// fn new(name: &str, country: &str) -> Viking { /// Viking { name: name.to_string(), country: country.to_string() } /// } /// } /// /// // Use a HashMap to store the vikings' health points. /// let mut vikings = HashMap::new(); /// /// vikings.insert(Viking::new("Einar", "Norway"), 25); /// vikings.insert(Viking::new("Olaf", "Denmark"), 24); /// vikings.insert(Viking::new("Harald", "Iceland"), 12); /// /// // Use derived implementation to print the status of the vikings. /// for (viking, health) in &vikings { /// println!("{:?} has {} hp", viking, health); /// } /// ``` /// /// A `HashMap` with fixed list of elements can be initialized from an array: /// /// ``` /// use hashbrown::HashMap; /// /// let timber_resources: HashMap<&str, i32> = [("Norway", 100), ("Denmark", 50), ("Iceland", 10)] /// .into_iter().collect(); /// // use the values stored in map /// ``` pubstruct HashMap<K, V, S = DefaultHashBuilder, A: Allocator = Global> { pub(crate) hash_builder: S, pub(crate) table: RawTable<(K, V), A>,
}
// Update hash_builder only if we successfully cloned all elements. self.hash_builder.clone_from(&source.hash_builder);
}
}
/// Ensures that a single closure type across uses of this which, in turn prevents multiple /// instances of any functions like `RawTable::reserve` from being generated #[cfg_attr(feature = "inline-more", inline)] pub(crate) fn make_hasher<Q, V, S>(hash_builder: &S) -> implFn(&(Q, V)) -> u64 + '_ where
Q: Hash,
S: BuildHasher,
{ move |val| make_hash::<Q, S>(hash_builder, &val.0)
}
/// Ensures that a single closure type across uses of this which, in turn prevents multiple /// instances of any functions like `RawTable::reserve` from being generated #[cfg_attr(feature = "inline-more", inline)] pub(crate) fn equivalent_key<Q, K, V>(k: &Q) -> implFn(&(K, V)) -> bool + '_ where
Q: Equivalent<K> + ?Sized,
{ move |x| k.equivalent(&x.0)
}
/// Ensures that a single closure type across uses of this which, in turn prevents multiple /// instances of any functions like `RawTable::reserve` from being generated #[cfg_attr(feature = "inline-more", inline)] #[cfg(feature = "raw-entry")] pub(crate) fn equivalent<Q, K>(k: &Q) -> implFn(&K) -> bool + '_ where
Q: Equivalent<K> + ?Sized,
{ move |x| k.equivalent(x)
}
#[cfg(feature = "default-hasher")] impl<K, V> HashMap<K, V, DefaultHashBuilder> { /// Creates an empty `HashMap`. /// /// The hash map is initially created with a capacity of 0, so it will not allocate until it /// is first inserted into. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`], for example with /// [`with_hasher`](HashMap::with_hasher) method. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// let mut map: HashMap<&str, i32> = HashMap::new(); /// assert_eq!(map.len(), 0); /// assert_eq!(map.capacity(), 0); /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] pubfn new() -> Self { Self::default()
}
/// Creates an empty `HashMap` with the specified capacity. /// /// The hash map will be able to hold at least `capacity` elements without /// reallocating. If `capacity` is 0, the hash map will not allocate. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`], for example with /// [`with_capacity_and_hasher`](HashMap::with_capacity_and_hasher) method. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10); /// assert_eq!(map.len(), 0); /// assert!(map.capacity() >= 10); /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] pubfn with_capacity(capacity: usize) -> Self { Self::with_capacity_and_hasher(capacity, DefaultHashBuilder::default())
}
}
#[cfg(feature = "default-hasher")] impl<K, V, A: Allocator> HashMap<K, V, DefaultHashBuilder, A> { /// Creates an empty `HashMap` using the given allocator. /// /// The hash map is initially created with a capacity of 0, so it will not allocate until it /// is first inserted into. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`], for example with /// [`with_hasher_in`](HashMap::with_hasher_in) method. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use bumpalo::Bump; /// /// let bump = Bump::new(); /// let mut map = HashMap::new_in(&bump); /// /// // The created HashMap holds none elements /// assert_eq!(map.len(), 0); /// /// // The created HashMap also doesn't allocate memory /// assert_eq!(map.capacity(), 0); /// /// // Now we insert element inside created HashMap /// map.insert("One", 1); /// // We can see that the HashMap holds 1 element /// assert_eq!(map.len(), 1); /// // And it also allocates some capacity /// assert!(map.capacity() > 1); /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] pubfn new_in(alloc: A) -> Self { Self::with_hasher_in(DefaultHashBuilder::default(), alloc)
}
/// Creates an empty `HashMap` with the specified capacity using the given allocator. /// /// The hash map will be able to hold at least `capacity` elements without /// reallocating. If `capacity` is 0, the hash map will not allocate. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`], for example with /// [`with_capacity_and_hasher_in`](HashMap::with_capacity_and_hasher_in) method. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use bumpalo::Bump; /// /// let bump = Bump::new(); /// let mut map = HashMap::with_capacity_in(5, &bump); /// /// // The created HashMap holds none elements /// assert_eq!(map.len(), 0); /// // But it can hold at least 5 elements without reallocating /// let empty_map_capacity = map.capacity(); /// assert!(empty_map_capacity >= 5); /// /// // Now we insert some 5 elements inside created HashMap /// map.insert("One", 1); /// map.insert("Two", 2); /// map.insert("Three", 3); /// map.insert("Four", 4); /// map.insert("Five", 5); /// /// // We can see that the HashMap holds 5 elements /// assert_eq!(map.len(), 5); /// // But its capacity isn't changed /// assert_eq!(map.capacity(), empty_map_capacity) /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] pubfn with_capacity_in(capacity: usize, alloc: A) -> Self { Self::with_capacity_and_hasher_in(capacity, DefaultHashBuilder::default(), alloc)
}
}
impl<K, V, S> HashMap<K, V, S> { /// Creates an empty `HashMap` which will use the given hash builder to hash /// keys. /// /// The hash map is initially created with a capacity of 0, so it will not /// allocate until it is first inserted into. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`]. /// /// The `hash_builder` passed should implement the [`BuildHasher`] trait for /// the `HashMap` to be useful, see its documentation for details. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::DefaultHashBuilder; /// /// let s = DefaultHashBuilder::default(); /// let mut map = HashMap::with_hasher(s); /// assert_eq!(map.len(), 0); /// assert_eq!(map.capacity(), 0); /// /// map.insert(1, 2); /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] #[cfg_attr(feature = "rustc-dep-of-std", rustc_const_stable_indirect)] pubconstfn with_hasher(hash_builder: S) -> Self { Self {
hash_builder,
table: RawTable::new(),
}
}
/// Creates an empty `HashMap` with the specified capacity, using `hash_builder` /// to hash the keys. /// /// The hash map will be able to hold at least `capacity` elements without /// reallocating. If `capacity` is 0, the hash map will not allocate. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`]. /// /// The `hash_builder` passed should implement the [`BuildHasher`] trait for /// the `HashMap` to be useful, see its documentation for details. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::DefaultHashBuilder; /// /// let s = DefaultHashBuilder::default(); /// let mut map = HashMap::with_capacity_and_hasher(10, s); /// assert_eq!(map.len(), 0); /// assert!(map.capacity() >= 10); /// /// map.insert(1, 2); /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] pubfn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self { Self {
hash_builder,
table: RawTable::with_capacity(capacity),
}
}
}
impl<K, V, S, A: Allocator> HashMap<K, V, S, A> { /// Returns a reference to the underlying allocator. #[inline] pubfn allocator(&self) -> &A { self.table.allocator()
}
/// Creates an empty `HashMap` which will use the given hash builder to hash /// keys. It will be allocated with the given allocator. /// /// The hash map is initially created with a capacity of 0, so it will not allocate until it /// is first inserted into. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`]. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::DefaultHashBuilder; /// /// let s = DefaultHashBuilder::default(); /// let mut map = HashMap::with_hasher(s); /// map.insert(1, 2); /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] #[cfg_attr(feature = "rustc-dep-of-std", rustc_const_stable_indirect)] pubconstfn with_hasher_in(hash_builder: S, alloc: A) -> Self { Self {
hash_builder,
table: RawTable::new_in(alloc),
}
}
/// Creates an empty `HashMap` with the specified capacity, using `hash_builder` /// to hash the keys. It will be allocated with the given allocator. /// /// The hash map will be able to hold at least `capacity` elements without /// reallocating. If `capacity` is 0, the hash map will not allocate. /// /// # HashDoS resistance /// /// The `hash_builder` normally use a fixed key by default and that does /// not allow the `HashMap` to be protected against attacks such as [`HashDoS`]. /// Users who require HashDoS resistance should explicitly use /// [`std::hash::RandomState`] /// as the hasher when creating a [`HashMap`]. /// /// [`HashDoS`]: https://en.wikipedia.org/wiki/Collision_attack /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::DefaultHashBuilder; /// /// let s = DefaultHashBuilder::default(); /// let mut map = HashMap::with_capacity_and_hasher(10, s); /// map.insert(1, 2); /// ``` #[must_use] #[cfg_attr(feature = "inline-more", inline)] pubfn with_capacity_and_hasher_in(capacity: usize, hash_builder: S, alloc: A) -> Self { Self {
hash_builder,
table: RawTable::with_capacity_in(capacity, alloc),
}
}
/// Returns a reference to the map's [`BuildHasher`]. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::DefaultHashBuilder; /// /// let hasher = DefaultHashBuilder::default(); /// let map: HashMap<i32, i32> = HashMap::with_hasher(hasher); /// let hasher: &DefaultHashBuilder = map.hasher(); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn hasher(&self) -> &S {
&self.hash_builder
}
/// Returns the number of elements the map can hold without reallocating. /// /// This number is a lower bound; the `HashMap<K, V>` might be able to hold /// more, but is guaranteed to be able to hold at least this many. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// let map: HashMap<i32, i32> = HashMap::with_capacity(100); /// assert_eq!(map.len(), 0); /// assert!(map.capacity() >= 100); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn capacity(&self) -> usize { self.table.capacity()
}
/// An iterator visiting all keys in arbitrary order. /// The iterator element type is `&'a K`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert("a", 1); /// map.insert("b", 2); /// map.insert("c", 3); /// assert_eq!(map.len(), 3); /// let mut vec: Vec<&str> = Vec::new(); /// /// for key in map.keys() { /// println!("{}", key); /// vec.push(*key); /// } /// /// // The `Keys` iterator produces keys in arbitrary order, so the /// // keys must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, ["a", "b", "c"]); /// /// assert_eq!(map.len(), 3); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn keys(&self) -> Keys<'_, K, V> {
Keys { inner: self.iter() }
}
/// An iterator visiting all values in arbitrary order. /// The iterator element type is `&'a V`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert("a", 1); /// map.insert("b", 2); /// map.insert("c", 3); /// assert_eq!(map.len(), 3); /// let mut vec: Vec<i32> = Vec::new(); /// /// for val in map.values() { /// println!("{}", val); /// vec.push(*val); /// } /// /// // The `Values` iterator produces values in arbitrary order, so the /// // values must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [1, 2, 3]); /// /// assert_eq!(map.len(), 3); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn values(&self) -> Values<'_, K, V> {
Values { inner: self.iter() }
}
/// An iterator visiting all values mutably in arbitrary order. /// The iterator element type is `&'a mut V`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// /// map.insert("a", 1); /// map.insert("b", 2); /// map.insert("c", 3); /// /// for val in map.values_mut() { /// *val = *val + 10; /// } /// /// assert_eq!(map.len(), 3); /// let mut vec: Vec<i32> = Vec::new(); /// /// for val in map.values() { /// println!("{}", val); /// vec.push(*val); /// } /// /// // The `Values` iterator produces values in arbitrary order, so the /// // values must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [11, 12, 13]); /// /// assert_eq!(map.len(), 3); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn values_mut(&mutself) -> ValuesMut<'_, K, V> {
ValuesMut {
inner: self.iter_mut(),
}
}
/// An iterator visiting all key-value pairs in arbitrary order. /// The iterator element type is `(&'a K, &'a V)`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert("a", 1); /// map.insert("b", 2); /// map.insert("c", 3); /// assert_eq!(map.len(), 3); /// let mut vec: Vec<(&str, i32)> = Vec::new(); /// /// for (key, val) in map.iter() { /// println!("key: {} val: {}", key, val); /// vec.push((*key, *val)); /// } /// /// // The `Iter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]); /// /// assert_eq!(map.len(), 3); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn iter(&self) -> Iter<'_, K, V> { // Here we tie the lifetime of self to the iter. unsafe {
Iter {
inner: self.table.iter(),
marker: PhantomData,
}
}
}
/// An iterator visiting all key-value pairs in arbitrary order, /// with mutable references to the values. /// The iterator element type is `(&'a K, &'a mut V)`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert("a", 1); /// map.insert("b", 2); /// map.insert("c", 3); /// /// // Update all values /// for (_, val) in map.iter_mut() { /// *val *= 2; /// } /// /// assert_eq!(map.len(), 3); /// let mut vec: Vec<(&str, i32)> = Vec::new(); /// /// for (key, val) in &map { /// println!("key: {} val: {}", key, val); /// vec.push((*key, *val)); /// } /// /// // The `Iter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [("a", 2), ("b", 4), ("c", 6)]); /// /// assert_eq!(map.len(), 3); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn iter_mut(&mutself) -> IterMut<'_, K, V> { // Here we tie the lifetime of self to the iter. unsafe {
IterMut {
inner: self.table.iter(),
marker: PhantomData,
}
}
}
/// Returns the number of elements in the map. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut a = HashMap::new(); /// assert_eq!(a.len(), 0); /// a.insert(1, "a"); /// assert_eq!(a.len(), 1); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn len(&self) -> usize { self.table.len()
}
/// Returns `true` if the map contains no elements. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut a = HashMap::new(); /// assert!(a.is_empty()); /// a.insert(1, "a"); /// assert!(!a.is_empty()); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn is_empty(&self) -> bool { self.len() == 0
}
/// Clears the map, returning all key-value pairs as an iterator. Keeps the /// allocated memory for reuse. /// /// If the returned iterator is dropped before being fully consumed, it /// drops the remaining key-value pairs. The returned iterator keeps a /// mutable borrow on the vector to optimize its implementation. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut a = HashMap::new(); /// a.insert(1, "a"); /// a.insert(2, "b"); /// let capacity_before_drain = a.capacity(); /// /// for (k, v) in a.drain().take(1) { /// assert!(k == 1 || k == 2); /// assert!(v == "a" || v == "b"); /// } /// /// // As we can see, the map is empty and contains no element. /// assert!(a.is_empty() && a.len() == 0); /// // But map capacity is equal to old one. /// assert_eq!(a.capacity(), capacity_before_drain); /// /// let mut a = HashMap::new(); /// a.insert(1, "a"); /// a.insert(2, "b"); /// /// { // Iterator is dropped without being consumed. /// let d = a.drain(); /// } /// /// // But the map is empty even if we do not use Drain iterator. /// assert!(a.is_empty()); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn drain(&mutself) -> Drain<'_, K, V, A> {
Drain {
inner: self.table.drain(),
}
}
/// Retains only the elements specified by the predicate. Keeps the /// allocated memory for reuse. /// /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`. /// The elements are visited in unsorted (and unspecified) order. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect(); /// assert_eq!(map.len(), 8); /// /// map.retain(|&k, _| k % 2 == 0); /// /// // We can see, that the number of elements inside map is changed. /// assert_eq!(map.len(), 4); /// /// let mut vec: Vec<(i32, i32)> = map.iter().map(|(&k, &v)| (k, v)).collect(); /// vec.sort_unstable(); /// assert_eq!(vec, [(0, 0), (2, 20), (4, 40), (6, 60)]); /// ``` pubfn retain<F>(&mutself, mut f: F) where
F: FnMut(&K, &mut V) -> bool,
{ // Here we only use `iter` as a temporary, preventing use-after-free unsafe { for item inself.table.iter() { let &mut (ref key, refmut value) = item.as_mut(); if !f(key, value) { self.table.erase(item);
}
}
}
}
/// Drains elements which are true under the given predicate, /// and returns an iterator over the removed items. /// /// In other words, move all pairs `(k, v)` such that `f(&k, &mut v)` returns `true` out /// into another iterator. /// /// Note that `extract_if` lets you mutate every value in the filter closure, regardless of /// whether you choose to keep or remove it. /// /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating /// or the iteration short-circuits, then the remaining elements will be retained. /// Use [`retain()`] with a negated predicate if you do not need the returned iterator. /// /// Keeps the allocated memory for reuse. /// /// [`retain()`]: HashMap::retain /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect(); /// /// let drained: HashMap<i32, i32> = map.extract_if(|k, _v| k % 2 == 0).collect(); /// /// let mut evens = drained.keys().cloned().collect::<Vec<_>>(); /// let mut odds = map.keys().cloned().collect::<Vec<_>>(); /// evens.sort(); /// odds.sort(); /// /// assert_eq!(evens, vec![0, 2, 4, 6]); /// assert_eq!(odds, vec![1, 3, 5, 7]); /// /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect(); /// /// { // Iterator is dropped without being consumed. /// let d = map.extract_if(|k, _v| k % 2 != 0); /// } /// /// // ExtractIf was not exhausted, therefore no elements were drained. /// assert_eq!(map.len(), 8); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn extract_if<F>(&mutself, f: F) -> ExtractIf<'_, K, V, F, A> where
F: FnMut(&K, &mut V) -> bool,
{
ExtractIf {
f,
inner: RawExtractIf {
iter: unsafe { self.table.iter() },
table: &mutself.table,
},
}
}
/// Clears the map, removing all key-value pairs. Keeps the allocated memory /// for reuse. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut a = HashMap::new(); /// a.insert(1, "a"); /// let capacity_before_clear = a.capacity(); /// /// a.clear(); /// /// // Map is empty. /// assert!(a.is_empty()); /// // But map capacity is equal to old one. /// assert_eq!(a.capacity(), capacity_before_clear); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn clear(&mutself) { self.table.clear();
}
/// Creates a consuming iterator visiting all the keys in arbitrary order. /// The map cannot be used after calling this. /// The iterator element type is `K`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert("a", 1); /// map.insert("b", 2); /// map.insert("c", 3); /// /// let mut vec: Vec<&str> = map.into_keys().collect(); /// /// // The `IntoKeys` iterator produces keys in arbitrary order, so the /// // keys must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, ["a", "b", "c"]); /// ``` #[inline] pubfn into_keys(self) -> IntoKeys<K, V, A> {
IntoKeys {
inner: self.into_iter(),
}
}
/// Creates a consuming iterator visiting all the values in arbitrary order. /// The map cannot be used after calling this. /// The iterator element type is `V`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert("a", 1); /// map.insert("b", 2); /// map.insert("c", 3); /// /// let mut vec: Vec<i32> = map.into_values().collect(); /// /// // The `IntoValues` iterator produces values in arbitrary order, so /// // the values must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [1, 2, 3]); /// ``` #[inline] pubfn into_values(self) -> IntoValues<K, V, A> {
IntoValues {
inner: self.into_iter(),
}
}
}
impl<K, V, S, A> HashMap<K, V, S, A> where
K: Eq + Hash,
S: BuildHasher,
A: Allocator,
{ /// Reserves capacity for at least `additional` more elements to be inserted /// in the `HashMap`. The collection may reserve more space to avoid /// frequent reallocations. /// /// # Panics /// /// Panics if the new capacity exceeds [`isize::MAX`] bytes and [`abort`] the program /// in case of allocation error. Use [`try_reserve`](HashMap::try_reserve) instead /// if you want to handle memory allocation failure. /// /// [`abort`]: stdalloc::alloc::handle_alloc_error /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// let mut map: HashMap<&str, i32> = HashMap::new(); /// // Map is empty and doesn't allocate memory /// assert_eq!(map.capacity(), 0); /// /// map.reserve(10); /// /// // And now map can hold at least 10 elements /// assert!(map.capacity() >= 10); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn reserve(&mutself, additional: usize) { self.table
.reserve(additional, make_hasher::<_, V, S>(&self.hash_builder));
}
/// Tries to reserve capacity for at least `additional` more elements to be inserted /// in the given `HashMap<K,V>`. The collection may reserve more space to avoid /// frequent reallocations. /// /// # Errors /// /// If the capacity overflows, or the allocator reports a failure, then an error /// is returned. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, isize> = HashMap::new(); /// // Map is empty and doesn't allocate memory /// assert_eq!(map.capacity(), 0); /// /// map.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?"); /// /// // And now map can hold at least 10 elements /// assert!(map.capacity() >= 10); /// ``` /// If the capacity overflows, or the allocator reports a failure, then an error /// is returned: /// ``` /// # fn test() { /// use hashbrown::HashMap; /// use hashbrown::TryReserveError; /// let mut map: HashMap<i32, i32> = HashMap::new(); /// /// match map.try_reserve(usize::MAX) { /// Err(error) => match error { /// TryReserveError::CapacityOverflow => {} /// _ => panic!("TryReserveError::AllocError ?"), /// }, /// _ => panic!(), /// } /// # } /// # fn main() { /// # #[cfg(not(miri))] /// # test() /// # } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn try_reserve(&mutself, additional: usize) -> Result<(), TryReserveError> { self.table
.try_reserve(additional, make_hasher::<_, V, S>(&self.hash_builder))
}
/// Shrinks the capacity of the map as much as possible. It will drop /// down as much as possible while maintaining the internal rules /// and possibly leaving some space in accordance with the resize policy. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<i32, i32> = HashMap::with_capacity(100); /// map.insert(1, 2); /// map.insert(3, 4); /// assert!(map.capacity() >= 100); /// map.shrink_to_fit(); /// assert!(map.capacity() >= 2); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn shrink_to_fit(&mutself) { self.table
.shrink_to(0, make_hasher::<_, V, S>(&self.hash_builder));
}
/// Shrinks the capacity of the map with a lower limit. It will drop /// down no lower than the supplied limit while maintaining the internal rules /// and possibly leaving some space in accordance with the resize policy. /// /// This function does nothing if the current capacity is smaller than the /// supplied minimum capacity. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<i32, i32> = HashMap::with_capacity(100); /// map.insert(1, 2); /// map.insert(3, 4); /// assert!(map.capacity() >= 100); /// map.shrink_to(10); /// assert!(map.capacity() >= 10); /// map.shrink_to(0); /// assert!(map.capacity() >= 2); /// map.shrink_to(10); /// assert!(map.capacity() >= 2); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn shrink_to(&mutself, min_capacity: usize) { self.table
.shrink_to(min_capacity, make_hasher::<_, V, S>(&self.hash_builder));
}
/// Gets the given key's corresponding entry in the map for in-place manipulation. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut letters = HashMap::new(); /// /// for ch in "a short treatise on fungi".chars() { /// let counter = letters.entry(ch).or_insert(0); /// *counter += 1; /// } /// /// assert_eq!(letters[&'s'], 2); /// assert_eq!(letters[&'t'], 3); /// assert_eq!(letters[&'u'], 1); /// assert_eq!(letters.get(&'y'), None); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn entry(&mutself, key: K) -> Entry<'_, K, V, S, A> { let hash = make_hash::<K, S>(&self.hash_builder, &key); iflet Some(elem) = self.table.find(hash, equivalent_key(&key)) {
Entry::Occupied(OccupiedEntry {
hash,
elem,
table: self,
})
} else {
Entry::Vacant(VacantEntry {
hash,
key,
table: self,
})
}
}
/// Gets the given key's corresponding entry by reference in the map for in-place manipulation. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut words: HashMap<String, usize> = HashMap::new(); /// let source = ["poneyland", "horseyland", "poneyland", "poneyland"]; /// for (i, &s) in source.iter().enumerate() { /// let counter = words.entry_ref(s).or_insert(0); /// *counter += 1; /// } /// /// assert_eq!(words["poneyland"], 3); /// assert_eq!(words["horseyland"], 1); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn entry_ref<'a, 'b, Q>(&'a mut self, key: &'b Q) -> EntryRef<'a, 'b, K, Q, V, S, A> where
Q: Hash + Equivalent<K> + ?Sized,
{ let hash = make_hash::<Q, S>(&self.hash_builder, key); iflet Some(elem) = self.table.find(hash, equivalent_key(key)) {
EntryRef::Occupied(OccupiedEntry {
hash,
elem,
table: self,
})
} else {
EntryRef::Vacant(VacantEntryRef {
hash,
key,
table: self,
})
}
}
/// Returns a reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, "a"); /// assert_eq!(map.get(&1), Some(&"a")); /// assert_eq!(map.get(&2), None); /// ``` #[inline] pubfn get<Q>(&self, k: &Q) -> Option<&V> where
Q: Hash + Equivalent<K> + ?Sized,
{ // Avoid `Option::map` because it bloats LLVM IR. ifself.table.is_empty() {
None
} else { let hash = make_hash::<Q, S>(&self.hash_builder, k); matchself.table.get(hash, equivalent_key(k)) {
Some((_, v)) => Some(v),
None => None,
}
}
}
/// Returns the key-value pair corresponding to the supplied key. /// /// The supplied key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, "a"); /// assert_eq!(map.get_key_value(&1), Some((&1, &"a"))); /// assert_eq!(map.get_key_value(&2), None); /// ``` #[inline] pubfn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)> where
Q: Hash + Equivalent<K> + ?Sized,
{ // Avoid `Option::map` because it bloats LLVM IR. ifself.table.is_empty() {
None
} else { let hash = make_hash::<Q, S>(&self.hash_builder, k); matchself.table.get(hash, equivalent_key(k)) {
Some((key, value)) => Some((key, value)),
None => None,
}
}
}
/// Returns the key-value pair corresponding to the supplied key, with a mutable reference to value. /// /// The supplied key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, "a"); /// let (k, v) = map.get_key_value_mut(&1).unwrap(); /// assert_eq!(k, &1); /// assert_eq!(v, &mut "a"); /// *v = "b"; /// assert_eq!(map.get_key_value_mut(&1), Some((&1, &mut "b"))); /// assert_eq!(map.get_key_value_mut(&2), None); /// ``` #[inline] pubfn get_key_value_mut<Q>(&mutself, k: &Q) -> Option<(&K, &style='color:red'>mut V)> where
Q: Hash + Equivalent<K> + ?Sized,
{ // Avoid `Option::map` because it bloats LLVM IR. ifself.table.is_empty() {
None
} else { let hash = make_hash::<Q, S>(&self.hash_builder, k); matchself.table.get_mut(hash, equivalent_key(k)) {
Some(&mut (ref key, refmut value)) => Some((key, value)),
None => None,
}
}
}
/// Returns `true` if the map contains a value for the specified key. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, "a"); /// assert_eq!(map.contains_key(&1), true); /// assert_eq!(map.contains_key(&2), false); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn contains_key<Q>(&self, k: &Q) -> bool where
Q: Hash + Equivalent<K> + ?Sized,
{ ifself.table.is_empty() { false
} else { let hash = make_hash::<Q, S>(&self.hash_builder, k); self.table.get(hash, equivalent_key(k)).is_some()
}
}
/// Returns a mutable reference to the value corresponding to the key. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, "a"); /// if let Some(x) = map.get_mut(&1) { /// *x = "b"; /// } /// assert_eq!(map[&1], "b"); /// /// assert_eq!(map.get_mut(&2), None); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn get_mut<Q>(&mutself, k: &Q) -> Option<&mut V> where
Q: Hash + Equivalent<K> + ?Sized,
{ // Avoid `Option::map` because it bloats LLVM IR. ifself.table.is_empty() {
None
} else { let hash = make_hash::<Q, S>(&self.hash_builder, k); matchself.table.get_mut(hash, equivalent_key(k)) {
Some(&mut (_, refmut v)) => Some(v),
None => None,
}
}
}
/// Attempts to get mutable references to `N` values in the map at once. /// /// Returns an array of length `N` with the results of each query. For soundness, at most one /// mutable reference will be returned to any value. `None` will be used if the key is missing. /// /// # Panics /// /// Panics if any keys are overlapping. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut libraries = HashMap::new(); /// libraries.insert("Bodleian Library".to_string(), 1602); /// libraries.insert("Athenæum".to_string(), 1807); /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691); /// libraries.insert("Library of Congress".to_string(), 1800); /// /// // Get Athenæum and Bodleian Library /// let [Some(a), Some(b)] = libraries.get_disjoint_mut([ /// "Athenæum", /// "Bodleian Library", /// ]) else { panic!() }; /// /// // Assert values of Athenæum and Library of Congress /// let got = libraries.get_disjoint_mut([ /// "Athenæum", /// "Library of Congress", /// ]); /// assert_eq!( /// got, /// [ /// Some(&mut 1807), /// Some(&mut 1800), /// ], /// ); /// /// // Missing keys result in None /// let got = libraries.get_disjoint_mut([ /// "Athenæum", /// "New York Public Library", /// ]); /// assert_eq!( /// got, /// [ /// Some(&mut 1807), /// None /// ] /// ); /// ``` /// /// ```should_panic /// use hashbrown::HashMap; /// /// let mut libraries = HashMap::new(); /// libraries.insert("Athenæum".to_string(), 1807); /// /// // Duplicate keys panic! /// let got = libraries.get_disjoint_mut([ /// "Athenæum", /// "Athenæum", /// ]); /// ``` pubfn get_disjoint_mut<Q, const N: usize>(&mutself, ks: [&Q; N]) -> [Option<&'_ mut V>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ self.get_disjoint_mut_inner(ks)
.map(|res| res.map(|(_, v)| v))
}
/// Attempts to get mutable references to `N` values in the map at once. #[deprecated(note = "use `get_disjoint_mut` instead")] pubfn get_many_mut<Q, const N: usize>(&mutself, ks: [&Q; N]) -> [Option<&an style='color:blue'>'_ mut V>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ self.get_disjoint_mut(ks)
}
/// Attempts to get mutable references to `N` values in the map at once, without validating that /// the values are unique. /// /// Returns an array of length `N` with the results of each query. `None` will be used if /// the key is missing. /// /// For a safe alternative see [`get_disjoint_mut`](`HashMap::get_disjoint_mut`). /// /// # Safety /// /// Calling this method with overlapping keys is *[undefined behavior]* even if the resulting /// references are not used. /// /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut libraries = HashMap::new(); /// libraries.insert("Bodleian Library".to_string(), 1602); /// libraries.insert("Athenæum".to_string(), 1807); /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691); /// libraries.insert("Library of Congress".to_string(), 1800); /// /// // SAFETY: The keys do not overlap. /// let [Some(a), Some(b)] = (unsafe { libraries.get_disjoint_unchecked_mut([ /// "Athenæum", /// "Bodleian Library", /// ]) }) else { panic!() }; /// /// // SAFETY: The keys do not overlap. /// let got = unsafe { libraries.get_disjoint_unchecked_mut([ /// "Athenæum", /// "Library of Congress", /// ]) }; /// assert_eq!( /// got, /// [ /// Some(&mut 1807), /// Some(&mut 1800), /// ], /// ); /// /// // SAFETY: The keys do not overlap. /// let got = unsafe { libraries.get_disjoint_unchecked_mut([ /// "Athenæum", /// "New York Public Library", /// ]) }; /// // Missing keys result in None /// assert_eq!(got, [Some(&mut 1807), None]); /// ``` pubunsafefn get_disjoint_unchecked_mut<Q, const N: usize>(
&mutself,
ks: [&Q; N],
) -> [Option<&'_ mut V>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ unsafe { self.get_disjoint_unchecked_mut_inner(ks)
.map(|res| res.map(|(_, v)| v))
}
}
/// Attempts to get mutable references to `N` values in the map at once, without validating that /// the values are unique. #[deprecated(note = "use `get_disjoint_unchecked_mut` instead")] pubunsafefn get_many_unchecked_mut<Q, const N: usize>(
&mutself,
ks: [&Q; N],
) -> [Option<&'_ mut V>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ unsafe { self.get_disjoint_unchecked_mut(ks) }
}
/// Attempts to get mutable references to `N` values in the map at once, with immutable /// references to the corresponding keys. /// /// Returns an array of length `N` with the results of each query. For soundness, at most one /// mutable reference will be returned to any value. `None` will be used if the key is missing. /// /// # Panics /// /// Panics if any keys are overlapping. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut libraries = HashMap::new(); /// libraries.insert("Bodleian Library".to_string(), 1602); /// libraries.insert("Athenæum".to_string(), 1807); /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691); /// libraries.insert("Library of Congress".to_string(), 1800); /// /// let got = libraries.get_disjoint_key_value_mut([ /// "Bodleian Library", /// "Herzogin-Anna-Amalia-Bibliothek", /// ]); /// assert_eq!( /// got, /// [ /// Some((&"Bodleian Library".to_string(), &mut 1602)), /// Some((&"Herzogin-Anna-Amalia-Bibliothek".to_string(), &mut 1691)), /// ], /// ); /// // Missing keys result in None /// let got = libraries.get_disjoint_key_value_mut([ /// "Bodleian Library", /// "Gewandhaus", /// ]); /// assert_eq!(got, [Some((&"Bodleian Library".to_string(), &mut 1602)), None]); /// ``` /// /// ```should_panic /// use hashbrown::HashMap; /// /// let mut libraries = HashMap::new(); /// libraries.insert("Bodleian Library".to_string(), 1602); /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691); /// /// // Duplicate keys result in panic! /// let got = libraries.get_disjoint_key_value_mut([ /// "Bodleian Library", /// "Herzogin-Anna-Amalia-Bibliothek", /// "Herzogin-Anna-Amalia-Bibliothek", /// ]); /// ``` pubfn get_disjoint_key_value_mut<Q, const N: usize>(
&mutself,
ks: [&Q; N],
) -> [Option<(&'_ K, &'_ mut V)>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ self.get_disjoint_mut_inner(ks)
.map(|res| res.map(|(k, v)| (&*k, v)))
}
/// Attempts to get mutable references to `N` values in the map at once, with immutable /// references to the corresponding keys. #[deprecated(note = "use `get_disjoint_key_value_mut` instead")] pubfn get_many_key_value_mut<Q, const N: usize>(
&mutself,
ks: [&Q; N],
) -> [Option<(&'_ K, &'_ mut V)>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ self.get_disjoint_key_value_mut(ks)
}
/// Attempts to get mutable references to `N` values in the map at once, with immutable /// references to the corresponding keys, without validating that the values are unique. /// /// Returns an array of length `N` with the results of each query. `None` will be returned if /// any of the keys are missing. /// /// For a safe alternative see [`get_disjoint_key_value_mut`](`HashMap::get_disjoint_key_value_mut`). /// /// # Safety /// /// Calling this method with overlapping keys is *[undefined behavior]* even if the resulting /// references are not used. /// /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut libraries = HashMap::new(); /// libraries.insert("Bodleian Library".to_string(), 1602); /// libraries.insert("Athenæum".to_string(), 1807); /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691); /// libraries.insert("Library of Congress".to_string(), 1800); /// /// let got = libraries.get_disjoint_key_value_mut([ /// "Bodleian Library", /// "Herzogin-Anna-Amalia-Bibliothek", /// ]); /// assert_eq!( /// got, /// [ /// Some((&"Bodleian Library".to_string(), &mut 1602)), /// Some((&"Herzogin-Anna-Amalia-Bibliothek".to_string(), &mut 1691)), /// ], /// ); /// // Missing keys result in None /// let got = libraries.get_disjoint_key_value_mut([ /// "Bodleian Library", /// "Gewandhaus", /// ]); /// assert_eq!( /// got, /// [ /// Some((&"Bodleian Library".to_string(), &mut 1602)), /// None, /// ], /// ); /// ``` pubunsafefn get_disjoint_key_value_unchecked_mut<Q, const N: usize>(
&mutself,
ks: [&Q; N],
) -> [Option<(&'_ K, &'_ mut V)>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ unsafe { self.get_disjoint_unchecked_mut_inner(ks)
.map(|res| res.map(|(k, v)| (&*k, v)))
}
}
/// Attempts to get mutable references to `N` values in the map at once, with immutable /// references to the corresponding keys, without validating that the values are unique. #[deprecated(note = "use `get_disjoint_key_value_unchecked_mut` instead")] pubunsafefn get_many_key_value_unchecked_mut<Q, const N: usize>(
&mutself,
ks: [&Q; N],
) -> [Option<(&'_ K, &'_ mut V)>; N] where
Q: Hash + Equivalent<K> + ?Sized,
{ unsafe { self.get_disjoint_key_value_unchecked_mut(ks) }
}
/// Inserts a key-value pair into the map. /// /// If the map did not have this key present, [`None`] is returned. /// /// If the map did have this key present, the value is updated, and the old /// value is returned. The key is not updated, though; this matters for /// types that can be `==` without being identical. See the [`std::collections`] /// module-level documentation for more. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// assert_eq!(map.insert(37, "a"), None); /// assert_eq!(map.is_empty(), false); /// /// map.insert(37, "b"); /// assert_eq!(map.insert(37, "c"), Some("b")); /// assert_eq!(map[&37], "c"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(&mutself, k: K, v: V) -> Option<V> { let hash = make_hash(&self.hash_builder, &k); let equivalent = equivalent_key(&k); let hasher = make_hasher(&self.hash_builder); matchself
.table
.find_or_find_insert_index(hash, equivalent, hasher)
{
Ok(bucket) => Some(mem::replace(unsafe { &mut bucket.as_mut().1 }, v)),
Err(index) => { unsafe { self.table.insert_at_index(hash, index, (k, v));
}
None
}
}
}
/// Insert a key-value pair into the map without checking /// if the key already exists in the map. /// /// This operation is faster than regular insert, because it does not perform /// lookup before insertion. /// /// This operation is useful during initial population of the map. /// For example, when constructing a map from another map, we know
/// that keys are unique. /// /// Returns a reference to the key and value just inserted. /// /// # Safety /// /// This operation is safe if a key does not exist in the map. /// /// However, if a key exists in the map already, the behavior is unspecified: /// this operation may panic, loop forever, or any following operation with the map /// may panic, loop forever or return arbitrary result. /// /// That said, this operation (and following operations) are guaranteed to /// not violate memory safety. /// /// However this operation is still unsafe because the resulting `HashMap` /// may be passed to unsafe code which does expect the map to behave /// correctly, and would cause unsoundness as a result. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map1 = HashMap::new(); /// assert_eq!(map1.insert(1, "a"), None); /// assert_eq!(map1.insert(2, "b"), None); /// assert_eq!(map1.insert(3, "c"), None); /// assert_eq!(map1.len(), 3); /// /// let mut map2 = HashMap::new(); /// /// for (key, value) in map1.into_iter() { /// unsafe { /// map2.insert_unique_unchecked(key, value); /// } /// } /// /// let (key, value) = unsafe { map2.insert_unique_unchecked(4, "d") }; /// assert_eq!(key, &4); /// assert_eq!(value, &mut "d"); /// *value = "e"; /// /// assert_eq!(map2[&1], "a"); /// assert_eq!(map2[&2], "b"); /// assert_eq!(map2[&3], "c"); /// assert_eq!(map2[&4], "e"); /// assert_eq!(map2.len(), 4); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubunsafefn insert_unique_unchecked(&mutself, k: K, v: V) -> (&K, &mut V) { let hash = make_hash::<K, S>(&self.hash_builder, &k); let bucket = self
.table
.insert(hash, (k, v), make_hasher::<_, V, S>(&self.hash_builder)); let (k_ref, v_ref) = unsafe { bucket.as_mut() };
(k_ref, v_ref)
}
/// Tries to insert a key-value pair into the map, and returns /// a mutable reference to the value in the entry. /// /// # Errors /// /// If the map already had this key present, nothing is updated, and /// an error containing the occupied entry and the value is returned. /// /// # Examples /// /// Basic usage: /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::OccupiedError; /// /// let mut map = HashMap::new(); /// assert_eq!(map.try_insert(37, "a").unwrap(), &"a"); /// /// match map.try_insert(37, "b") { /// Err(OccupiedError { entry, value }) => { /// assert_eq!(entry.key(), &37); /// assert_eq!(entry.get(), &"a"); /// assert_eq!(value, "b"); /// } /// _ => panic!() /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn try_insert(
&mutself,
key: K,
value: V,
) -> Result<&mut V, OccupiedError<'_, K, V, S, A>> { matchself.entry(key) {
Entry::Occupied(entry) => Err(OccupiedError { entry, value }),
Entry::Vacant(entry) => Ok(entry.insert(value)),
}
}
/// Removes a key from the map, returning the value at the key if the key /// was previously in the map. Keeps the allocated memory for reuse. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// // The map is empty /// assert!(map.is_empty() && map.capacity() == 0); /// /// map.insert(1, "a"); /// /// assert_eq!(map.remove(&1), Some("a")); /// assert_eq!(map.remove(&1), None); /// /// // Now map holds none elements /// assert!(map.is_empty()); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn remove<Q>(&mutself, k: &Q) -> Option<V> where
Q: Hash + Equivalent<K> + ?Sized,
{ // Avoid `Option::map` because it bloats LLVM IR. matchself.remove_entry(k) {
Some((_, v)) => Some(v),
None => None,
}
}
/// Removes a key from the map, returning the stored key and value if the /// key was previously in the map. Keeps the allocated memory for reuse. /// /// The key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for /// the key type. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map = HashMap::new(); /// // The map is empty /// assert!(map.is_empty() && map.capacity() == 0); /// /// map.insert(1, "a"); /// /// assert_eq!(map.remove_entry(&1), Some((1, "a"))); /// assert_eq!(map.remove(&1), None); /// /// // Now map hold none elements /// assert!(map.is_empty()); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn remove_entry<Q>(&mutself, k: &Q) -> Option<(K, V)> where
Q: Hash + Equivalent<K> + ?Sized,
{ let hash = make_hash::<Q, S>(&self.hash_builder, k); self.table.remove_entry(hash, equivalent_key(k))
}
/// Returns the total amount of memory allocated internally by the hash /// set, in bytes. /// /// The returned number is informational only. It is intended to be /// primarily used for memory profiling. #[inline] pubfn allocation_size(&self) -> usize { self.table.allocation_size()
}
}
impl<K, V, S, A> PartialEq for HashMap<K, V, S, A> where
K: Eq + Hash,
V: PartialEq,
S: BuildHasher,
A: Allocator,
{ fn eq(&self, other: &Self) -> bool { ifself.len() != other.len() { returnfalse;
}
impl<K, V, S, A> Eq for HashMap<K, V, S, A> where
K: Eq + Hash,
V: Eq,
S: BuildHasher,
A: Allocator,
{
}
impl<K, V, S, A> Debug for HashMap<K, V, S, A> where
K: Debug,
V: Debug,
A: Allocator,
{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
impl<K, V, S, A> Default for HashMap<K, V, S, A> where
S: Default,
A: Default + Allocator,
{ /// Creates an empty `HashMap<K, V, S, A>`, with the `Default` value for the hasher and allocator. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use std::hash::RandomState; /// /// // You can specify all types of HashMap, including hasher and allocator. /// // Created map is empty and don't allocate memory /// let map: HashMap<u32, String> = Default::default(); /// assert_eq!(map.capacity(), 0); /// let map: HashMap<u32, String, RandomState> = HashMap::default(); /// assert_eq!(map.capacity(), 0); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn default() -> Self { Self::with_hasher_in(Default::default(), Default::default())
}
}
impl<K, Q, V, S, A> Index<&Q> for HashMap<K, V, S, A> where
K: Eq + Hash,
Q: Hash + Equivalent<K> + ?Sized,
S: BuildHasher,
A: Allocator,
{ type Output = V;
/// Returns a reference to the value corresponding to the supplied key. /// /// # Panics /// /// Panics if the key is not present in the `HashMap`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [("a", "One"), ("b", "Two")].into(); /// /// assert_eq!(map[&"a"], "One"); /// assert_eq!(map[&"b"], "Two"); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn index(&self, key: &Q) -> &V { self.get(key).expect("no entry found for key")
}
}
// The default hasher is used to match the std implementation signature #[cfg(feature = "default-hasher")] impl<K, V, A, const N: usize> From<[(K, V); N]> for HashMap<K, V, DefaultHashBuilder, A> where
K: Eq + Hash,
A: Default + Allocator,
{ /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map1 = HashMap::from([(1, 2), (3, 4)]); /// let map2: HashMap<_, _> = [(1, 2), (3, 4)].into(); /// assert_eq!(map1, map2); /// ``` fn from(arr: [(K, V); N]) -> Self {
arr.into_iter().collect()
}
}
/// An iterator over the entries of a `HashMap` in arbitrary order. /// The iterator element type is `(&'a K, &'a V)`. /// /// This `struct` is created by the [`iter`] method on [`HashMap`]. See its /// documentation for more. /// /// [`iter`]: HashMap::iter /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut iter = map.iter(); /// let mut vec = vec![iter.next(), iter.next(), iter.next()]; /// /// // The `Iter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some((&1, &"a")), Some((&2, &"b")), Some((&3, &"c"))]); /// /// // It is fused iterator /// assert_eq!(iter.next(), None); /// assert_eq!(iter.next(), None); /// ``` pubstruct Iter<'a, K, V> {
inner: RawIter<(K, V)>,
marker: PhantomData<(&'a K, &'a V)>,
}
// FIXME(#26925) Remove in favor of `#[derive(Clone)]` impl<K, V> Clone for Iter<'_, K, V> { #[cfg_attr(feature = "inline-more", inline)] fn clone(&self) -> Self {
Iter {
inner: self.inner.clone(),
marker: PhantomData,
}
}
}
/// A mutable iterator over the entries of a `HashMap` in arbitrary order. /// The iterator element type is `(&'a K, &'a mut V)`. /// /// This `struct` is created by the [`iter_mut`] method on [`HashMap`]. See its /// documentation for more. /// /// [`iter_mut`]: HashMap::iter_mut /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<_, _> = [(1, "One".to_owned()), (2, "Two".into())].into(); /// /// let mut iter = map.iter_mut(); /// iter.next().map(|(_, v)| v.push_str(" Mississippi")); /// iter.next().map(|(_, v)| v.push_str(" Mississippi")); /// /// // It is fused iterator /// assert_eq!(iter.next(), None); /// assert_eq!(iter.next(), None); /// /// assert_eq!(map.get(&1).unwrap(), &"One Mississippi".to_owned()); /// assert_eq!(map.get(&2).unwrap(), &"Two Mississippi".to_owned()); /// ``` pubstruct IterMut<'a, K, V> {
inner: RawIter<(K, V)>, // To ensure invariance with respect to V
marker: PhantomData<(&'a K, &'a mut V)>,
}
// We override the default Send impl which has K: Sync instead of K: Send. Both // are correct, but this one is more general since it allows keys which // implement Send but not Sync. unsafeimpl<K: Send, V: Send> Send for IterMut<'_, K, V> {}
impl<K, V> IterMut<'_, K, V> { /// Returns a iterator of references over the remaining items. #[cfg_attr(feature = "inline-more", inline)] pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.clone(),
marker: PhantomData,
}
}
}
/// An owning iterator over the entries of a `HashMap` in arbitrary order. /// The iterator element type is `(K, V)`. /// /// This `struct` is created by the [`into_iter`] method on [`HashMap`] /// (provided by the [`IntoIterator`] trait). See its documentation for more. /// The map cannot be used after calling that method. /// /// [`into_iter`]: HashMap::into_iter /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut iter = map.into_iter(); /// let mut vec = vec![iter.next(), iter.next(), iter.next()]; /// /// // The `IntoIter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some((1, "a")), Some((2, "b")), Some((3, "c"))]); /// /// // It is fused iterator /// assert_eq!(iter.next(), None); /// assert_eq!(iter.next(), None); /// ``` pubstruct IntoIter<K, V, A: Allocator = Global> {
inner: RawIntoIter<(K, V), A>,
}
impl<K, V, A: Allocator> IntoIter<K, V, A> { /// Returns a iterator of references over the remaining items. #[cfg_attr(feature = "inline-more", inline)] pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.iter(),
marker: PhantomData,
}
}
}
/// An owning iterator over the keys of a `HashMap` in arbitrary order. /// The iterator element type is `K`. /// /// This `struct` is created by the [`into_keys`] method on [`HashMap`]. /// See its documentation for more. /// The map cannot be used after calling that method. /// /// [`into_keys`]: HashMap::into_keys /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut keys = map.into_keys(); /// let mut vec = vec![keys.next(), keys.next(), keys.next()]; /// /// // The `IntoKeys` iterator produces keys in arbitrary order, so the /// // keys must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some(1), Some(2), Some(3)]); /// /// // It is fused iterator /// assert_eq!(keys.next(), None); /// assert_eq!(keys.next(), None); /// ``` pubstruct IntoKeys<K, V, A: Allocator = Global> {
inner: IntoIter<K, V, A>,
}
impl<K, V, A: Allocator> Default for IntoKeys<K, V, A> { #[cfg_attr(feature = "inline-more", inline)] fn default() -> Self { Self {
inner: Default::default(),
}
}
} impl<K, V, A: Allocator> Iterator for IntoKeys<K, V, A> { type Item = K;
/// An owning iterator over the values of a `HashMap` in arbitrary order. /// The iterator element type is `V`. /// /// This `struct` is created by the [`into_values`] method on [`HashMap`]. /// See its documentation for more. The map cannot be used after calling that method. /// /// [`into_values`]: HashMap::into_values /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut values = map.into_values(); /// let mut vec = vec![values.next(), values.next(), values.next()]; /// /// // The `IntoValues` iterator produces values in arbitrary order, so /// // the values must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some("a"), Some("b"), Some("c")]); /// /// // It is fused iterator /// assert_eq!(values.next(), None); /// assert_eq!(values.next(), None); /// ``` pubstruct IntoValues<K, V, A: Allocator = Global> {
inner: IntoIter<K, V, A>,
}
impl<K, V, A: Allocator> Default for IntoValues<K, V, A> { #[cfg_attr(feature = "inline-more", inline)] fn default() -> Self { Self {
inner: Default::default(),
}
}
} impl<K, V, A: Allocator> Iterator for IntoValues<K, V, A> { type Item = V;
/// An iterator over the keys of a `HashMap` in arbitrary order. /// The iterator element type is `&'a K`. /// /// This `struct` is created by the [`keys`] method on [`HashMap`]. See its /// documentation for more. /// /// [`keys`]: HashMap::keys /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut keys = map.keys(); /// let mut vec = vec![keys.next(), keys.next(), keys.next()]; /// /// // The `Keys` iterator produces keys in arbitrary order, so the /// // keys must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some(&1), Some(&2), Some(&3)]); /// /// // It is fused iterator /// assert_eq!(keys.next(), None); /// assert_eq!(keys.next(), None); /// ``` pubstruct Keys<'a, K, V> {
inner: Iter<'a, K, V>,
}
/// An iterator over the values of a `HashMap` in arbitrary order. /// The iterator element type is `&'a V`. /// /// This `struct` is created by the [`values`] method on [`HashMap`]. See its /// documentation for more. /// /// [`values`]: HashMap::values /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut values = map.values(); /// let mut vec = vec![values.next(), values.next(), values.next()]; /// /// // The `Values` iterator produces values in arbitrary order, so the /// // values must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some(&"a"), Some(&"b"), Some(&"c")]); /// /// // It is fused iterator /// assert_eq!(values.next(), None); /// assert_eq!(values.next(), None); /// ``` pubstruct Values<'a, K, V> {
inner: Iter<'a, K, V>,
}
/// A draining iterator over the entries of a `HashMap` in arbitrary /// order. The iterator element type is `(K, V)`. /// /// This `struct` is created by the [`drain`] method on [`HashMap`]. See its /// documentation for more. /// /// [`drain`]: HashMap::drain /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut drain_iter = map.drain(); /// let mut vec = vec![drain_iter.next(), drain_iter.next(), drain_iter.next()]; /// /// // The `Drain` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some((1, "a")), Some((2, "b")), Some((3, "c"))]); /// /// // It is fused iterator /// assert_eq!(drain_iter.next(), None); /// assert_eq!(drain_iter.next(), None); /// ``` pubstruct Drain<'a, K, V, A: Allocator = Global> {
inner: RawDrain<'a, (K, V), A>,
}
impl<K, V, A: Allocator> Drain<'_, K, V, A> { /// Returns a iterator of references over the remaining items. #[cfg_attr(feature = "inline-more", inline)] pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.iter(),
marker: PhantomData,
}
}
}
/// A draining iterator over entries of a `HashMap` which don't satisfy the predicate /// `f(&k, &mut v)` in arbitrary order. The iterator element type is `(K, V)`. /// /// This `struct` is created by the [`extract_if`] method on [`HashMap`]. See its /// documentation for more. /// /// [`extract_if`]: HashMap::extract_if /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<i32, &str> = [(1, "a"), (2, "b"), (3, "c")].into(); /// /// let mut extract_if = map.extract_if(|k, _v| k % 2 != 0); /// let mut vec = vec![extract_if.next(), extract_if.next()]; /// /// // The `ExtractIf` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [Some((1, "a")),Some((3, "c"))]); /// /// // It is fused iterator /// assert_eq!(extract_if.next(), None); /// assert_eq!(extract_if.next(), None); /// drop(extract_if); /// /// assert_eq!(map.len(), 1); /// ``` #[must_use = "Iterators are lazy unless consumed"] pubstruct ExtractIf<'a, K, V, F, A: Allocator = Global> {
f: F,
inner: RawExtractIf<'a, (K, V), A>,
}
impl<K, V, F, A> Iterator for ExtractIf<'_, K, V, F, A> where
F: FnMut(&K, &mut V) -> bool,
A: Allocator,
{ type Item = (K, V);
impl<K, V, F> FusedIterator for ExtractIf<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
/// A mutable iterator over the values of a `HashMap` in arbitrary order. /// The iterator element type is `&'a mut V`. /// /// This `struct` is created by the [`values_mut`] method on [`HashMap`]. See its /// documentation for more. /// /// [`values_mut`]: HashMap::values_mut /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<_, _> = [(1, "One".to_owned()), (2, "Two".into())].into(); /// /// let mut values = map.values_mut(); /// values.next().map(|v| v.push_str(" Mississippi")); /// values.next().map(|v| v.push_str(" Mississippi")); /// /// // It is fused iterator /// assert_eq!(values.next(), None); /// assert_eq!(values.next(), None); /// /// assert_eq!(map.get(&1).unwrap(), &"One Mississippi".to_owned()); /// assert_eq!(map.get(&2).unwrap(), &"Two Mississippi".to_owned()); /// ``` pubstruct ValuesMut<'a, K, V> {
inner: IterMut<'a, K, V>,
}
/// A view into a single entry in a map, which may either be vacant or occupied. /// /// This `enum` is constructed from the [`entry`] method on [`HashMap`]. /// /// [`entry`]: HashMap::entry /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{Entry, HashMap, OccupiedEntry}; /// /// let mut map = HashMap::new(); /// map.extend([("a", 10), ("b", 20), ("c", 30)]); /// assert_eq!(map.len(), 3); /// /// // Existing key (insert) /// let entry: Entry<_, _, _> = map.entry("a"); /// let _raw_o: OccupiedEntry<_, _, _> = entry.insert(1); /// assert_eq!(map.len(), 3); /// // Nonexistent key (insert) /// map.entry("d").insert(4); /// /// // Existing key (or_insert) /// let v = map.entry("b").or_insert(2); /// assert_eq!(std::mem::replace(v, 2), 20); /// // Nonexistent key (or_insert) /// map.entry("e").or_insert(5); /// /// // Existing key (or_insert_with) /// let v = map.entry("c").or_insert_with(|| 3); /// assert_eq!(std::mem::replace(v, 3), 30); /// // Nonexistent key (or_insert_with) /// map.entry("f").or_insert_with(|| 6); /// /// println!("Our HashMap: {:?}", map); /// /// let mut vec: Vec<_> = map.iter().map(|(&k, &v)| (k, v)).collect(); /// // The `Iter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3), ("d", 4), ("e", 5), ("f", 6)]); /// ``` pubenum Entry<'a, K, V, S, A = Global> where
A: Allocator,
{ /// An occupied entry. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{Entry, HashMap}; /// let mut map: HashMap<_, _> = [("a", 100), ("b", 200)].into(); /// /// match map.entry("a") { /// Entry::Vacant(_) => unreachable!(), /// Entry::Occupied(_) => { } /// } /// ```
Occupied(OccupiedEntry<'a, K, V, S, A>),
/// A view into a single entry in a map, which may either be vacant or occupied, /// with any borrowed form of the map's key type. /// /// /// This `enum` is constructed from the [`entry_ref`] method on [`HashMap`]. /// /// [`Hash`] and [`Eq`] on the borrowed form of the map's key type *must* match those /// for the key type. It also require that key may be constructed from the borrowed /// form through the [`ToOwned`] trait. /// /// [`entry_ref`]: HashMap::entry_ref /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{EntryRef, HashMap, OccupiedEntry}; /// /// let mut map = HashMap::new(); /// map.extend([("a".to_owned(), 10), ("b".into(), 20), ("c".into(), 30)]); /// assert_eq!(map.len(), 3); /// /// // Existing key (insert) /// let key = String::from("a"); /// let entry: EntryRef<_, _, _, _> = map.entry_ref(&key); /// let _raw_o: OccupiedEntry<_, _, _, _> = entry.insert(1); /// assert_eq!(map.len(), 3); /// // Nonexistent key (insert) /// map.entry_ref("d").insert(4); /// /// // Existing key (or_insert) /// let v = map.entry_ref("b").or_insert(2); /// assert_eq!(std::mem::replace(v, 2), 20); /// // Nonexistent key (or_insert) /// map.entry_ref("e").or_insert(5); /// /// // Existing key (or_insert_with) /// let v = map.entry_ref("c").or_insert_with(|| 3); /// assert_eq!(std::mem::replace(v, 3), 30); /// // Nonexistent key (or_insert_with) /// map.entry_ref("f").or_insert_with(|| 6); /// /// println!("Our HashMap: {:?}", map); /// /// for (key, value) in ["a", "b", "c", "d", "e", "f"].into_iter().zip(1..=6) { /// assert_eq!(map[key], value) /// } /// assert_eq!(map.len(), 6); /// ``` pubenum EntryRef<'a, 'b, K, Q: ?Sized, V, S, A = Global> where
A: Allocator,
{ /// An occupied entry. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{EntryRef, HashMap}; /// let mut map: HashMap<_, _> = [("a".to_owned(), 100), ("b".into(), 200)].into(); /// /// match map.entry_ref("a") { /// EntryRef::Vacant(_) => unreachable!(), /// EntryRef::Occupied(_) => { } /// } /// ```
Occupied(OccupiedEntry<'a, K, V, S, A>),
/// A vacant entry. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{EntryRef, HashMap}; /// let mut map: HashMap<String, i32> = HashMap::new(); /// /// match map.entry_ref("a") { /// EntryRef::Occupied(_) => unreachable!(), /// EntryRef::Vacant(_) => { } /// } /// ```
Vacant(VacantEntryRef<'a, 'b, K, Q, V, S, A>),
}
impl<K, Q, V, S, A> Debug for EntryRef<'_, '_, K, Q, V, S, A> where
K: Debug + Borrow<Q>,
Q: Debug + ?Sized,
V: Debug,
A: Allocator,
{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self {
EntryRef::Vacant(ref v) => f.debug_tuple("EntryRef").field(v).finish(),
EntryRef::Occupied(ref o) => f.debug_tuple("EntryRef").field(o).finish(),
}
}
}
/// A view into a vacant entry in a `HashMap`. /// It is part of the [`EntryRef`] enum. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{EntryRef, HashMap, VacantEntryRef}; /// /// let mut map = HashMap::<String, i32>::new(); /// /// let entry_v: VacantEntryRef<_, _, _, _> = match map.entry_ref("a") { /// EntryRef::Vacant(view) => view, /// EntryRef::Occupied(_) => unreachable!(), /// }; /// entry_v.insert(10); /// assert!(map["a"] == 10 && map.len() == 1); /// /// // Nonexistent key (insert and update) /// match map.entry_ref("b") { /// EntryRef::Occupied(_) => unreachable!(), /// EntryRef::Vacant(view) => { /// let value = view.insert(2); /// assert_eq!(*value, 2); /// *value = 20; /// } /// } /// assert!(map["b"] == 20 && map.len() == 2); /// ``` pubstruct VacantEntryRef<'map, 'key, K, Q: ?Sized, V, S, A: Allocator = Global> {
hash: u64,
key: &'key Q,
table: &'map mut HashMap<K, V, S, A>,
}
impl<K, Q, V, S, A> Debug for VacantEntryRef<'_, '_, K, Q, V, S, A> where
K: Borrow<Q>,
Q: Debug + ?Sized,
A: Allocator,
{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("VacantEntryRef").field(&self.key()).finish()
}
}
/// The error returned by [`try_insert`](HashMap::try_insert) when the key already exists. /// /// Contains the occupied entry, and the value that was not inserted. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{HashMap, OccupiedError}; /// /// let mut map: HashMap<_, _> = [("a", 10), ("b", 20)].into(); /// /// // try_insert method returns mutable reference to the value if keys are vacant, /// // but if the map did have key present, nothing is updated, and the provided /// // value is returned inside `Err(_)` variant /// match map.try_insert("a", 100) { /// Err(OccupiedError { mut entry, value }) => { /// assert_eq!(entry.key(), &"a"); /// assert_eq!(value, 100); /// assert_eq!(entry.insert(100), 10) /// } /// _ => unreachable!(), /// } /// assert_eq!(map[&"a"], 100); /// ``` pubstruct OccupiedError<'a, K, V, S, A: Allocator = Global> { /// The entry in the map that was already occupied. pub entry: OccupiedEntry<'a, K, V, S, A>, /// The value which was not inserted, because the entry was already occupied. pub value: V,
}
impl<K: Debug, V: Debug, S, A: Allocator> fmt::Display for OccupiedError<'_, K, V, S, A> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f, "failed to insert {:?}, key {:?} already exists with value {:?}", self.value, self.entry.key(), self.entry.get(),
)
}
}
impl<'a, K, V, S, A: Allocator> IntoIterator for &'a HashMap<K, V, S, A> { type Item = (&'a K, &'a V); type IntoIter = Iter<'a, K, V>;
/// Creates an iterator over the entries of a `HashMap` in arbitrary order. /// The iterator element type is `(&'a K, &'a V)`. /// /// Return the same `Iter` struct as by the [`iter`] method on [`HashMap`]. /// /// [`iter`]: HashMap::iter /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// let map_one: HashMap<_, _> = [(1, "a"), (2, "b"), (3, "c")].into(); /// let mut map_two = HashMap::new(); /// /// for (key, value) in &map_one { /// println!("Key: {}, Value: {}", key, value); /// map_two.insert(*key, *value); /// } /// /// assert_eq!(map_one, map_two); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn into_iter(self) -> Iter<'a, K, V> { self.iter()
}
}
impl<'a, K, V, S, A: Allocator> IntoIterator for &'a mut HashMap<K, V, S, A> { type Item = (&'a K, &'a mut V); type IntoIter = IterMut<'a, K, V>;
/// Creates an iterator over the entries of a `HashMap` in arbitrary order /// with mutable references to the values. The iterator element type is /// `(&'a K, &'a mut V)`. /// /// Return the same `IterMut` struct as by the [`iter_mut`] method on /// [`HashMap`]. /// /// [`iter_mut`]: HashMap::iter_mut /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// let mut map: HashMap<_, _> = [("a", 1), ("b", 2), ("c", 3)].into(); /// /// for (key, value) in &mut map { /// println!("Key: {}, Value: {}", key, value); /// *value *= 2; /// } /// /// let mut vec = map.iter().collect::<Vec<_>>(); /// // The `Iter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [(&"a", &2), (&"b", &4), (&"c", &6)]); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn into_iter(self) -> IterMut<'a, K, V> { self.iter_mut()
}
}
impl<K, V, S, A: Allocator> IntoIterator for HashMap<K, V, S, A> { type Item = (K, V); type IntoIter = IntoIter<K, V, A>;
/// Creates a consuming iterator, that is, one that moves each key-value /// pair out of the map in arbitrary order. The map cannot be used after /// calling this. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let map: HashMap<_, _> = [("a", 1), ("b", 2), ("c", 3)].into(); /// /// // Not possible with .iter() /// let mut vec: Vec<(&str, i32)> = map.into_iter().collect(); /// // The `IntoIter` iterator produces items in arbitrary order, so /// // the items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn into_iter(self) -> IntoIter<K, V, A> {
IntoIter {
inner: self.table.into_iter(),
}
}
}
impl<K, V, A> fmt::Debug for Drain<'_, K, V, A> where
K: fmt::Debug,
V: fmt::Debug,
A: Allocator,
{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<'a, K, V, S, A: Allocator> Entry<'a, K, V, S, A> { /// Sets the value of the entry, and returns an `OccupiedEntry`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// let entry = map.entry("horseyland").insert(37); /// /// assert_eq!(entry.key(), &"horseyland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(self, value: V) -> OccupiedEntry<'a, K, V, S, A> where
K: Hash,
S: BuildHasher,
{ matchself {
Entry::Occupied(mut entry) => {
entry.insert(value);
entry
}
Entry::Vacant(entry) => entry.insert_entry(value),
}
}
/// Ensures a value is in the entry by inserting the default if empty, and returns /// a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// // nonexistent key /// map.entry("poneyland").or_insert(3); /// assert_eq!(map["poneyland"], 3); /// /// // existing key /// *map.entry("poneyland").or_insert(10) *= 2; /// assert_eq!(map["poneyland"], 6); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert(self, default: V) -> &'a mut V where
K: Hash,
S: BuildHasher,
{ matchself {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default),
}
}
/// Ensures a value is in the entry by inserting the default if empty, /// and returns an [`OccupiedEntry`]. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// // nonexistent key /// let entry = map.entry("poneyland").or_insert_entry(3); /// assert_eq!(entry.key(), &"poneyland"); /// assert_eq!(entry.get(), &3); /// /// // existing key /// let mut entry = map.entry("poneyland").or_insert_entry(10); /// assert_eq!(entry.key(), &"poneyland"); /// assert_eq!(entry.get(), &3); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert_entry(self, default: V) -> OccupiedEntry<'a, K, V, S, A> where
K: Hash,
S: BuildHasher,
{ matchself {
Entry::Occupied(entry) => entry,
Entry::Vacant(entry) => entry.insert_entry(default),
}
}
/// Ensures a value is in the entry by inserting the result of the default function if empty, /// and returns a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// // nonexistent key /// map.entry("poneyland").or_insert_with(|| 3); /// assert_eq!(map["poneyland"], 3); /// /// // existing key /// *map.entry("poneyland").or_insert_with(|| 10) *= 2; /// assert_eq!(map["poneyland"], 6); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V where
K: Hash,
S: BuildHasher,
{ matchself {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default()),
}
}
/// Ensures a value is in the entry by inserting, if empty, the result of the default function. /// This method allows for generating key-derived values for insertion by providing the default /// function a reference to the key that was moved during the `.entry(key)` method call. /// /// The reference to the moved key is provided so that cloning or copying the key is /// unnecessary, unlike with `.or_insert_with(|| ... )`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, usize> = HashMap::new(); /// /// // nonexistent key /// map.entry("poneyland").or_insert_with_key(|key| key.chars().count()); /// assert_eq!(map["poneyland"], 9); /// /// // existing key /// *map.entry("poneyland").or_insert_with_key(|key| key.chars().count() * 10) *= 2; /// assert_eq!(map["poneyland"], 18); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V where
K: Hash,
S: BuildHasher,
{ matchself {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => { let value = default(entry.key());
entry.insert(value)
}
}
}
/// Provides shared access to the key and owned access to the value of /// an occupied entry and allows to replace or remove it based on the /// value of the returned option. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::Entry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// let entry = map /// .entry("poneyland") /// .and_replace_entry_with(|_k, _v| panic!()); /// /// match entry { /// Entry::Vacant(e) => { /// assert_eq!(e.key(), &"poneyland"); /// } /// Entry::Occupied(_) => panic!(), /// } /// /// map.insert("poneyland", 42); /// /// let entry = map /// .entry("poneyland") /// .and_replace_entry_with(|k, v| { /// assert_eq!(k, &"poneyland"); /// assert_eq!(v, 42); /// Some(v + 1) /// }); /// /// match entry { /// Entry::Occupied(e) => { /// assert_eq!(e.key(), &"poneyland"); /// assert_eq!(e.get(), &43); /// } /// Entry::Vacant(_) => panic!(), /// } /// /// assert_eq!(map["poneyland"], 43); /// /// let entry = map /// .entry("poneyland") /// .and_replace_entry_with(|_k, _v| None); /// /// match entry { /// Entry::Vacant(e) => assert_eq!(e.key(), &"poneyland"), /// Entry::Occupied(_) => panic!(), /// } /// /// assert!(!map.contains_key("poneyland")); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn and_replace_entry_with<F>(self, f: F) -> Self where
F: FnOnce(&K, V) -> Option<V>,
{ matchself {
Entry::Occupied(entry) => entry.replace_entry_with(f),
Entry::Vacant(_) => self,
}
}
/// Converts the `Entry` into a mutable reference to the underlying map. pubfn into_map(self) -> &'a mut HashMap<K, V, S, A> { matchself {
Entry::Occupied(entry) => entry.table,
Entry::Vacant(entry) => entry.table,
}
}
}
impl<'a, K, V: Default, S, A: Allocator> Entry<'a, K, V, S, A> { /// Ensures a value is in the entry by inserting the default value if empty, /// and returns a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, Option<u32>> = HashMap::new(); /// /// // nonexistent key /// map.entry("poneyland").or_default(); /// assert_eq!(map["poneyland"], None); /// /// map.insert("horseland", Some(3)); /// /// // existing key /// assert_eq!(map.entry("horseland").or_default(), &mut Some(3)); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_default(self) -> &'a mut V where
K: Hash,
S: BuildHasher,
{ matchself {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(Default::default()),
}
}
}
impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> { /// Gets a reference to the key in the entry. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{Entry, HashMap}; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.entry("poneyland").or_insert(12); /// /// match map.entry("poneyland") { /// Entry::Vacant(_) => panic!(), /// Entry::Occupied(entry) => assert_eq!(entry.key(), &"poneyland"), /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn key(&self) -> &K { unsafe { &self.elem.as_ref().0 }
}
/// Replaces the key in the entry with a new one. /// /// # Panics /// /// This method panics if `key` is not equivalent to the key in the entry. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{Entry, HashMap}; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// let old_key = "poneyland"; /// let new_key = Box::leak(old_key.to_owned().into_boxed_str()); /// map.entry(old_key).or_insert(12); /// match map.entry("poneyland") { /// Entry::Vacant(_) => panic!(), /// Entry::Occupied(mut entry) => { /// let replaced = entry.replace_key(new_key); /// assert!(std::ptr::eq(replaced, old_key)); /// assert!(std::ptr::eq(*entry.key(), new_key)); /// }, /// } /// /// # // appease miri; no memory leaks here! /// # drop(map); /// # unsafe { /// # Box::from_raw(new_key); /// # } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn replace_key(&mutself, key: K) -> K where
K: Equivalent<K>,
{
assert!( self.key().equivalent(&key), "replaced key is not equivalent to the one in the entry"
);
// SAFETY: We verified that the keys were equivalent. unsafe { self.replace_key_unchecked(key) }
}
/// Replaces the key in the entry with a new one, without checking the /// equivalence of the key. /// /// # Safety /// /// This operation is safe if you replace the key with an equivalent one. /// /// Additionally, this operation (and following operations) are guaranteed /// to not violate memory safety. /// /// However this operation is still unsafe because the resulting `HashMap` /// may be passed to unsafe code which does expect the map to behave /// correctly. If the map has keys at unexpected positions inside it, /// future operations may panic, loop forever, or return unexpected results, /// potentially violating memory safety. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{Entry, HashMap}; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// let old_key = "poneyland"; /// let new_key = Box::leak(old_key.to_owned().into_boxed_str()); /// map.entry(old_key).or_insert(12); /// match map.entry("poneyland") { /// Entry::Vacant(_) => panic!(), /// Entry::Occupied(mut entry) => { /// let replaced = unsafe { entry.replace_key_unchecked(new_key) }; /// assert!(std::ptr::eq(replaced, old_key)); /// assert!(std::ptr::eq(*entry.key(), new_key)); /// }, /// } /// /// # // appease miri; no memory leaks here! /// # drop(map); /// # unsafe { /// # Box::from_raw(new_key); /// # } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubunsafefn replace_key_unchecked(&mutself, key: K) -> K {
mem::replace(unsafe { &mutself.elem.as_mut().0 }, key)
}
/// Take the ownership of the key and value from the map. /// Keeps the allocated memory for reuse. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::Entry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// // The map is empty /// assert!(map.is_empty() && map.capacity() == 0); /// /// map.entry("poneyland").or_insert(12); /// /// if let Entry::Occupied(o) = map.entry("poneyland") { /// // We delete the entry from the map. /// assert_eq!(o.remove_entry(), ("poneyland", 12)); /// } /// /// assert_eq!(map.contains_key("poneyland"), false); /// // Now map hold none elements /// assert!(map.is_empty()); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn remove_entry(self) -> (K//! [https://courses.cs.ut.ee/MTAT.07.022/2015_fall/uploads/Main/dmitri-report-f15-16.pdf](https://courses.cs.ut.ee/MTAT.07.022/2015_fall/uploads/Main/dmitri-report-f15-16.pdf)
java.lang.StringIndexOutOfBoundsException: Range [13, 8) out of bounds for length 55
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
/// Gets a reference to the value in the entry. /// /// # Examples /// /// ```
Map;
/java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39 ///
/ /// map.entry("poneyland").or_insert(12);
}
.entry"poneyland") java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 38
} /// Entry::Occupied(entry) => assert_eq!(entry.get(), &12),
}
/// ``` #cfeature =java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
:u16= (self.as) | Wrapping(3); unsafe &elfa)1 java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
/// Gets a mutable reference to the value in the entry. ///
needareference he `OccupiedEntry` which outlivetjava.lang.StringIndexOutOfBoundsException: Index 76 out of bounds for length 76
/
pub structZipCryptoReader<R> {{
PkzipCrc32(, /// /// # Examples/// does not specify password encoding (see function `update_keys` in the specification). /// /// ```
/// use hashbrown::hash_map::Entry;
/ /// let mut map: HashMap<&str, u32> = HashMap::new();=keys*yte); /// map.entry("poneyland").or_insert(12);
java.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 7 /// assert_eq!(map["poneyland"], 12); // it uses high byte of 16-bit File Time.
/// *o.get_mut() += 10; /// assert_eq!(*o.get(), 22); /// /// // We can use the same Entry multiple times. /// *o.get_mut() += 2;(rate)keys: ZipCryptoKeys, /// } /// // assert_eq!(map["poneyland"], 24); /// ``` #cfg_attr(feature="inlinemore,inline] pubfn get_mut(&mutself) -> &mut V {
{ &mutself.lemas_mut().1 }
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
/
// https://github.com/kornelski/7z/blob/master/CPP/7zip/Crypto/ZipCrypto.cpp to the `OccupiedEntry`, see [`get_mut`]. /// /// [`get_mut`]: #method.get_mut /// /// # Examples
java.lang.StringIndexOutOfBoundsException: Range [28, 4) out of bounds for length 7 /// ```064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, // use hashbrown::hash_map::{Entry, HashMap}; /// // let mut map: HashMap<&str, u32> = HashMap::new(); /// map.entry("poneyland").or_insert(12); /// /// assert_eq!(map["poneyland"], 12); /// /// let value: &mut u32; /// match map.entry("poneyland") {
/ /// Entry::Vacant(_) => panic!(), /// }
java.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21 /// /// assert_eq!(map["poneyland"], 22); /// ``` #[cfg_attr(feature = "inline-more", inline)] fninto_mut( ' utV java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40 unsafe { &mut 0x9b64c2b0, 0xec63f226java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 99
}
/ /// mutable reference to the value in the entry with a lifetime bound to the0xd9d65adc x40df0b66,0x37d83bf0xa9bcae53,0xdebb9ec5,x47b2cf7f, 0x30b5ffe9,
/ /// /// If you need multiple references to the `OccupiedEntry`, see [`key`] and /// [`get_mut`]. /// /// [`key`]: Self::key /// [`get_mut`]: Self::get_mut /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{Entry, HashMap}; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.entry("poneyland").or_insert(12); /// /// assert_eq!(map["poneyland"], 12); /// /// let key_val: (&&str, &mut u32); /// match map.entry("poneyland") { /// Entry::Occupied(entry) => key_val = entry.into_entry(), /// Entry::Vacant(_) => panic!(), /// } /// *key_val.1 += 10; /// /// assert_eq!(key_val, (&"poneyland", &mut 22)); /// assert_eq!(map["poneyland"], 22); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn into_entry(self) -> (&'a K, &'a mut V) { let (key, val) = unsafe { self.elem.as_mut() };
(key, val)
}
/// Sets the value of the entry, and returns the entry's old value. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::Entry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.entry("poneyland").or_insert(12); /// /// if let Entry::Occupied(mut o) = map.entry("poneyland") { /// assert_eq!(o.insert(15), 12); /// } /// /// assert_eq!(map["poneyland"], 15); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(&mutself, value: V) -> V {
mem::replace(self.get_mut(), value)
}
/// Takes the value out of the entry, and returns it. /// Keeps the allocated memory for reuse. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::Entry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// // The map is empty /// assert!(map.is_empty() && map.capacity() == 0); /// /// map.entry("poneyland").or_insert(12); /// /// if let Entry::Occupied(o) = map.entry("poneyland") { /// assert_eq!(o.remove(), 12); /// } /// /// assert_eq!(map.contains_key("poneyland"), false); /// // Now map hold none elements /// assert!(map.is_empty()); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn remove(self) -> V { self.remove_entry().1
}
/// Provides shared access to the key and owned access to the value of /// the entry and allows to replace or remove it based on the /// value of the returned option. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::Entry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.insert("poneyland", 42); /// /// let entry = match map.entry("poneyland") { /// Entry::Occupied(e) => { /// e.replace_entry_with(|k, v| { /// assert_eq!(k, &"poneyland"); /// assert_eq!(v, 42); /// Some(v + 1) /// }) /// } /// Entry::Vacant(_) => panic!(), /// }; /// /// match entry { /// Entry::Occupied(e) => { /// assert_eq!(e.key(), &"poneyland"); /// assert_eq!(e.get(), &43); /// } /// Entry::Vacant(_) => panic!(), /// } /// /// assert_eq!(map["poneyland"], 43); /// /// let entry = match map.entry("poneyland") { /// Entry::Occupied(e) => e.replace_entry_with(|_k, _v| None), /// Entry::Vacant(_) => panic!(), /// }; /// /// match entry { /// Entry::Vacant(e) => { /// assert_eq!(e.key(), &"poneyland"); /// } /// Entry::Occupied(_) => panic!(), /// } /// /// assert!(!map.contains_key("poneyland")); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn replace_entry_with<F>(self, f: F) -> Entry<'a, K, V, S, A> where
F: FnOnce(&K, V) -> Option<V>,
{ unsafe { letmut spare_key = None;
/// Converts the `OccupiedEntry` into a mutable reference to the underlying map. pubfn into_map(self) -> &'a mut HashMap<K, V, S, A> { self.table
}
}
impl<'a, K, V, S, A: Allocator> VacantEntry<'a, K, V, S, A> { /// Gets a reference to the key that would be used when inserting a value /// through the `VacantEntry`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// assert_eq!(map.entry("poneyland").key(), &"poneyland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn key(&self) -> &K {
&self.key
}
/// Take ownership of the key. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::{Entry, HashMap}; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// match map.entry("poneyland") { /// Entry::Occupied(_) => panic!(), /// Entry::Vacant(v) => assert_eq!(v.into_key(), "poneyland"), /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn into_key(self) -> K { self.key
}
/// Sets the value of the entry with the [`VacantEntry`]'s key, /// and returns a mutable reference to it. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::Entry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// if let Entry::Vacant(o) = map.entry("poneyland") { /// o.insert(37); /// } /// assert_eq!(map["poneyland"], 37); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(self, value: V) -> &'a mut V where
K: Hash,
S: BuildHasher,
{ let table = &mutself.table.table; let entry = table.insert_entry( self.hash,
(self.key, value),
make_hasher::<_, V, S>(&self.table.hash_builder),
);
&mut entry.1
}
/// Sets the value of the entry with the [`VacantEntry`]'s key, /// and returns an [`OccupiedEntry`]. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::Entry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// if let Entry::Vacant(v) = map.entry("poneyland") { /// let o = v.insert_entry(37); /// assert_eq!(o.get(), &37); /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S, A> where
K: Hash,
S: BuildHasher,
{ let elem = self.table.table.insert( self.hash,
(self.key, value),
make_hasher::<_, V, S>(&self.table.hash_builder),
);
OccupiedEntry {
hash: self.hash,
elem,
table: self.table,
}
}
/// Converts the `VacantEntry` into a mutable reference to the underlying map. pubfn into_map(self) -> &'a mut HashMap<K, V, S, A> { self.table
}
}
impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> { /// Sets the value of the entry, and returns an `OccupiedEntry`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, u32> = HashMap::new(); /// let entry = map.entry_ref("horseyland").insert(37); /// /// assert_eq!(entry.key(), "horseyland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(self, value: V) -> OccupiedEntry<'a, K, V, S, A> where
K: Hash,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ matchself {
EntryRef::Occupied(mut entry) => {
entry.insert(value);
entry
}
EntryRef::Vacant(entry) => entry.insert_entry(value),
}
}
/// Ensures a value is in the entry by inserting the default if empty, and returns /// a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, u32> = HashMap::new(); /// /// // nonexistent key /// map.entry_ref("poneyland").or_insert(3); /// assert_eq!(map["poneyland"], 3); /// /// // existing key /// *map.entry_ref("poneyland").or_insert(10) *= 2; /// assert_eq!(map["poneyland"], 6); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert(self, default: V) -> &'a mut V where
K: Hash,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ matchself {
EntryRef::Occupied(entry) => entry.into_mut(),
EntryRef::Vacant(entry) => entry.insert(default),
}
}
/// Ensures a value is in the entry by inserting the result of the default function if empty, /// and returns a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, u32> = HashMap::new(); /// /// // nonexistent key /// map.entry_ref("poneyland").or_insert_with(|| 3); /// assert_eq!(map["poneyland"], 3); /// /// // existing key /// *map.entry_ref("poneyland").or_insert_with(|| 10) *= 2; /// assert_eq!(map["poneyland"], 6); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V where
K: Hash,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ matchself {
EntryRef::Occupied(entry) => entry.into_mut(),
EntryRef::Vacant(entry) => entry.insert(default()),
}
}
/// Ensures a value is in the entry by inserting, if empty, the result of the default function. /// This method allows for generating key-derived values for insertion by providing the default /// function an access to the borrower form of the key. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, usize> = HashMap::new(); /// /// // nonexistent key /// map.entry_ref("poneyland").or_insert_with_key(|key| key.chars().count()); /// assert_eq!(map["poneyland"], 9); /// /// // existing key /// *map.entry_ref("poneyland").or_insert_with_key(|key| key.chars().count() * 10) *= 2; /// assert_eq!(map["poneyland"], 18); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert_with_key<F: FnOnce(&Q) -> V>(self, default: F) -> &'a mut V where
K: Hash + Borrow<Q>,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ matchself {
EntryRef::Occupied(entry) => entry.into_mut(),
EntryRef::Vacant(entry) => { let value = default(entry.key);
entry.insert(value)
}
}
}
/// Returns a reference to this entry's key. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, u32> = HashMap::new(); /// map.entry_ref("poneyland").or_insert(3); /// // existing key /// assert_eq!(map.entry_ref("poneyland").key(), "poneyland"); /// // nonexistent key /// assert_eq!(map.entry_ref("horseland").key(), "horseland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn key(&self) -> &Q where
K: Borrow<Q>,
{ match *self {
EntryRef::Occupied(ref entry) => entry.key().borrow(),
EntryRef::Vacant(ref entry) => entry.key(),
}
}
/// Converts the `EntryRef` into a mutable reference to the underlying map. pubfn into_map(self) -> &'a mut HashMap<K, V, S, A> { matchself {
EntryRef::Occupied(entry) => entry.table,
EntryRef::Vacant(entry) => entry.table,
}
}
}
impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> { /// Ensures a value is in the entry by inserting the default value if empty, /// and returns a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, Option<u32>> = HashMap::new(); /// /// // nonexistent key /// map.entry_ref("poneyland").or_default(); /// assert_eq!(map["poneyland"], None); /// /// map.insert("horseland".to_string(), Some(3)); /// /// // existing key /// assert_eq!(map.entry_ref("horseland").or_default(), &mut Some(3)); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_default(self) -> &'a mut V where
K: Hash,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ matchself {
EntryRef::Occupied(entry) => entry.into_mut(),
EntryRef::Vacant(entry) => entry.insert(Default::default()),
}
}
/// Ensures a value is in the entry by inserting the default value if empty, /// and returns an [`OccupiedEntry`]. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, Option<u32>> = HashMap::new(); /// /// // nonexistent key /// let entry = map.entry_ref("poneyland").or_default_entry(); /// assert_eq!(entry.key(), &"poneyland"); /// assert_eq!(entry.get(), &None); /// /// // existing key /// map.insert("horseland".to_string(), Some(3)); /// let entry = map.entry_ref("horseland").or_default_entry(); /// assert_eq!(entry.key(), &"horseland"); /// assert_eq!(entry.get(), &Some(3)); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_default_entry(self) -> OccupiedEntry<'a, K, V, S, A> where
K: Hash,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ matchself {
EntryRef::Occupied(entry) => entry,
EntryRef::Vacant(entry) => entry.insert_entry(Default::default()),
}
}
}
impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K, Q, V, S, A> { /// Gets a reference to the key that would be used when inserting a value /// through the `VacantEntryRef`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<String, u32> = HashMap::new(); /// let key: &str = "poneyland"; /// assert_eq!(map.entry_ref(key).key(), "poneyland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn key(&self) -> &'key Q { self.key
}
/// Sets the value of the entry with the `VacantEntryRef`'s key, /// and returns a mutable reference to it. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::EntryRef; /// /// let mut map: HashMap<String, u32> = HashMap::new(); /// let key: &str = "poneyland"; /// /// if let EntryRef::Vacant(o) = map.entry_ref(key) { /// o.insert(37); /// } /// assert_eq!(map["poneyland"], 37); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(self, value: V) -> &'map mut V where
K: Hash,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ let table = &mutself.table.table; let entry = table.insert_entry( self.hash,
(self.key.to_owned(), value),
make_hasher::<_, V, S>(&self.table.hash_builder),
);
&mut entry.1
}
/// Sets the key and value of the entry and returns a mutable reference to /// the inserted value. /// /// Unlike [`VacantEntryRef::insert`], this method allows the key to be /// explicitly specified, which is useful for key types that don't implement /// `ToOwned`. /// /// # Panics /// /// This method panics if `key` is not equivalent to the key used to create /// the `VacantEntryRef`. /// /// # Example /// /// ``` /// use hashbrown::hash_map::EntryRef; /// use hashbrown::HashMap; /// /// let mut map = HashMap::<(String, String), char>::new(); /// let k = ("c".to_string(), "C".to_string()); /// let v = match map.entry_ref(&k) { /// // Insert cannot be used here because tuples do not implement ToOwned. /// // However this works because we can manually clone instead. /// EntryRef::Vacant(r) => r.insert_with_key(k.clone(), 'c'), /// // In this branch we avoid the clone. /// EntryRef::Occupied(r) => r.into_mut(), /// }; /// assert_eq!(*v, 'c'); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert_with_key(self, key: K, value: V) -> &'map mut V where
K: Hash,
Q: Equivalent<K>,
S: BuildHasher,
{ self.insert_entry_with_key(key, value).into_mut()
}
/// Sets the key and value of the entry and returns a mutable reference to /// the inserted value, without checking the equivalence of the key. /// /// See [`insert_with_key`](Self::insert_with_key) for more information. /// /// # Safety /// /// This operation is safe if the keys are equivalent. /// /// Additionally, this operation (and following operations) are guaranteed /// to not violate memory safety. /// /// However this operation is still unsafe because the resulting `HashMap` /// may be passed to unsafe code which does expect the map to behave /// correctly. If the map has keys at unexpected positions inside it, /// future operations may panic, loop forever, or return unexpected results, /// potentially violating memory safety. /// /// # Example /// /// ``` /// use hashbrown::hash_map::EntryRef; /// use hashbrown::HashMap; /// /// let mut map = HashMap::<(String, String), char>::new(); /// let k = ("c".to_string(), "C".to_string()); /// let v = match map.entry_ref(&k) { /// // SAFETY: We trust the `Clone` implementation to return an equivalent value /// EntryRef::Vacant(r) => unsafe { r.insert_with_key_unchecked(k.clone(), 'c') }, /// // In this branch we avoid the clone. /// EntryRef::Occupied(r) => r.into_mut(), /// }; /// assert_eq!(*v, 'c'); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubunsafefn insert_with_key_unchecked(self, key: K, value: V) -> &>'map mut V where
K: Hash,
S: BuildHasher,
{ // SAFETY: Guaranteed by caller. unsafe { self.insert_entry_with_key_unchecked(key, value) }.into_mut()
}
/// Sets the value of the entry with the [`VacantEntryRef`]'s key, /// and returns an [`OccupiedEntry`]. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::EntryRef; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// if let EntryRef::Vacant(v) = map.entry_ref(&"poneyland") { /// let o = v.insert_entry(37); /// assert_eq!(o.get(), &37); /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert_entry(self, value: V) -> OccupiedEntry<'map, K, V, S, A> where
K: Hash,
Q: ToOwned<Owned = K>,
S: BuildHasher,
{ let elem = self.table.table.insert( self.hash,
(self.key.to_owned(), value),
make_hasher::<_, V, S>(&self.table.hash_builder),
);
OccupiedEntry {
hash: self.hash,
elem,
table: self.table,
}
}
/// Sets the key and value of the entry and returns an [`OccupiedEntry`]. /// /// Unlike [`VacantEntryRef::insert_entry`], this method allows the key to /// be explicitly specified, which is useful for key types that don't /// implement `ToOwned`. /// /// # Panics /// /// This method panics if `key` is not equivalent to the key used to create /// the `VacantEntryRef`. /// /// # Example /// /// ``` /// use hashbrown::hash_map::EntryRef; /// use hashbrown::HashMap; /// /// let mut map = HashMap::<(String, String), char>::new(); /// let k = ("c".to_string(), "C".to_string()); /// let r = match map.entry_ref(&k) { /// // Insert cannot be used here because tuples do not implement ToOwned. /// // However this works because we can manually clone instead. /// EntryRef::Vacant(r) => r.insert_entry_with_key(k.clone(), 'c'), /// // In this branch we avoid the clone. /// EntryRef::Occupied(r) => r, /// }; /// assert_eq!(r.get(), &'c'); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert_entry_with_key(self, key: K, value: V) -> OccupiedEntry<'map, K, V, S, A> where
K: Hash,
Q: Equivalent<K>,
S: BuildHasher,
{
assert!(
(self.key).equivalent(&key), "key used for Entry creation is not equivalent to the one used for insertion"
); // SAFETY: We checked equivalence first. unsafe { self.insert_entry_with_key_unchecked(key, value) }
}
/// Sets the key and value of the entry and returns an [`OccupiedEntry`], /// without checking the equivalence of the key. /// /// See [`insert_entry_with_key`](Self::insert_entry_with_key) for more information. /// /// # Safety /// /// This operation is safe if the keys are equivalent. /// /// Additionally, this operation (and following operations) are guaranteed /// to not violate memory safety. /// /// However this operation is still unsafe because the resulting `HashMap` /// may be passed to unsafe code which does expect the map to behave /// correctly. If the map has keys at unexpected positions inside it, /// future operations may panic, loop forever, or return unexpected results, /// potentially violating memory safety. /// /// # Example /// /// ``` /// use hashbrown::hash_map::EntryRef; /// use hashbrown::HashMap; /// /// let mut map = HashMap::<(String, String), char>::new(); /// let k = ("c".to_string(), "C".to_string()); /// let r = match map.entry_ref(&k) { /// // SAFETY: We trust the `Clone` implementation to return an equivalent key /// EntryRef::Vacant(r) => unsafe { r.insert_entry_with_key_unchecked(k.clone(), 'c') }, /// // In this branch we avoid the clone. /// EntryRef::Occupied(r) => r, /// }; /// assert_eq!(r.get(), &'c'); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubunsafefn insert_entry_with_key_unchecked( self,
key: K,
value: V,
) -> OccupiedEntry<'map, K, V, S, A> where
K: Hash,
S: BuildHasher,
{ let elem = self.table.table.insert( self.hash,
(key, value),
make_hasher::<_, V, S>(&self.table.hash_builder),
);
OccupiedEntry {
hash: self.hash,
elem,
table: self.table,
}
}
/// Converts the `VacantEntryRef` into a mutable reference to the underlying map. pubfn into_map(self) -> &'map mut HashMap<K, V, S, A> { self.table
}
}
impl<K, V, S, A> FromIterator<(K, V)> for HashMap<K, V, S, A> where
K: Eq + Hash,
S: BuildHasher + Default,
A: Default + Allocator,
{ #[cfg_attr(feature = "inline-more", inline)] fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self { let iter = iter.into_iter(); letmut map = Self::with_capacity_and_hasher_in(iter.size_hint().0, S::default(), A::default());
iter.for_each(|(k, v)| {
map.insert(k, v);
});
map
}
}
/// Inserts all new key-values from the iterator and replaces values with existing /// keys with new values returned from the iterator. impl<K, V, S, A> Extend<(K, V)> for HashMap<K, V, S, A> where
K: Eq + Hash,
S: BuildHasher,
A: Allocator,
{ /// Inserts all new key-values from the iterator to existing `HashMap<K, V, S, A>`. /// Replace values with existing keys with new values returned from the iterator. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, 100); /// /// let some_iter = [(1, 1), (2, 2)].into_iter(); /// map.extend(some_iter); /// // Replace values with existing keys with new values returned from the iterator. /// // So that the map.get(&1) doesn't return Some(&100). /// assert_eq!(map.get(&1), Some(&1)); /// /// let some_vec: Vec<_> = vec![(3, 3), (4, 4)]; /// map.extend(some_vec); /// /// let some_arr = [(5, 5), (6, 6)]; /// map.extend(some_arr); /// let old_map_len = map.len(); /// /// // You can also extend from another HashMap /// let mut new_map = HashMap::new(); /// new_map.extend(map); /// assert_eq!(new_map.len(), old_map_len); /// /// let mut vec: Vec<_> = new_map.into_iter().collect(); /// // The `IntoIter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn extend<T: IntoIterator<Item = (K, V)>>(&mutself, iter: T) { // Keys may be already present or show multiple times in the iterator. // Reserve the entire hint lower bound if the map is empty. // Otherwise reserve half the hint (rounded up), so the map // will only resize twice in the worst case. let iter = iter.into_iter(); let reserve = ifself.is_empty() {
iter.size_hint().0
} else {
iter.size_hint().0.div_ceil(2)
}; self.reserve(reserve);
iter.for_each(move |(k, v)| { self.insert(k, v);
});
}
#[inline] #[cfg(feature = "nightly")] fn extend_reserve(&mutself, additional: usize) { // Keys may be already present or show multiple times in the iterator. // Reserve the entire hint lower bound if the map is empty. // Otherwise reserve half the hint (rounded up), so the map // will only resize twice in the worst case. let reserve = ifself.is_empty() {
additional
} else {
additional.div_ceil(2)
}; self.reserve(reserve);
}
}
/// Inserts all new key-values from the iterator and replaces values with existing /// keys with new values returned from the iterator. impl<'a, K, V, S, A> Extend<(&'a K, &'a V)> for HashMap<K, V, S, A> where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
A: Allocator,
{ /// Inserts all new key-values from the iterator to existing `HashMap<K, V, S, A>`. /// Replace values with existing keys with new values returned from the iterator. /// The keys and values must implement [`Copy`] trait. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, 100); /// /// let arr = [(1, 1), (2, 2)]; /// let some_iter = arr.iter().map(|(k, v)| (k, v)); /// map.extend(some_iter); /// // Replace values with existing keys with new values returned from the iterator. /// // So that the map.get(&1) doesn't return Some(&100). /// assert_eq!(map.get(&1), Some(&1)); /// /// let some_vec: Vec<_> = vec![(3, 3), (4, 4)]; /// map.extend(some_vec.iter().map(|(k, v)| (k, v))); /// /// let some_arr = [(5, 5), (6, 6)]; /// map.extend(some_arr.iter().map(|(k, v)| (k, v))); /// /// // You can also extend from another HashMap /// let mut new_map = HashMap::new(); /// new_map.extend(&map); /// assert_eq!(new_map, map); /// /// let mut vec: Vec<_> = new_map.into_iter().collect(); /// // The `IntoIter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mutself, iter: T) { self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
}
/// Inserts all new key-values from the iterator and replaces values with existing /// keys with new values returned from the iterator. impl<'a, K, V, S, A> Extend<&'a (K, V)> for HashMap<K, V, S, A> where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
A: Allocator,
{ /// Inserts all new key-values from the iterator to existing `HashMap<K, V, S, A>`. /// Replace values with existing keys with new values returned from the iterator. /// The keys and values must implement [`Copy`] trait. /// /// # Examples /// /// ``` /// use hashbrown::hash_map::HashMap; /// /// let mut map = HashMap::new(); /// map.insert(1, 100); /// /// let arr = [(1, 1), (2, 2)]; /// let some_iter = arr.iter(); /// map.extend(some_iter); /// // Replace values with existing keys with new values returned from the iterator. /// // So that the map.get(&1) doesn't return Some(&100). /// assert_eq!(map.get(&1), Some(&1)); /// /// let some_vec: Vec<_> = vec![(3, 3), (4, 4)]; /// map.extend(&some_vec); /// /// let some_arr = [(5, 5), (6, 6)]; /// map.extend(&some_arr); /// /// let mut vec: Vec<_> = map.into_iter().collect(); /// // The `IntoIter` iterator produces items in arbitrary order, so the /// // items must be sorted to test them against a sorted array. /// vec.sort_unstable(); /// assert_eq!(vec, [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]); /// ``` #[cfg_attr(feature = "inline-more", inline)] fn extend<T: IntoIterator<Item = &'a (K, V)>>(&mut self, iter: T) { self.extend(iter.into_iter().map(|&(key, value)| (key, value)));
}
#[cfg(test)] mod test_map { usesuper::DefaultHashBuilder; usesuper::Entry::{Occupied, Vacant}; usesuper::EntryRef; usesuper::HashMap; usecrate::alloc::{AllocError, Allocator, Global}; use core::alloc::Layout; use core::ptr::NonNull; use core::sync::atomic::{AtomicI8, Ordering}; use rand::{Rng, SeedableRng, rngs::SmallRng}; use std::borrow::ToOwned; use std::cell::RefCell; use std::vec::Vec; use stdalloc::string::String; use stdalloc::sync::Arc;
#[test] fn test_zero_capacities() { type HM = HashMap<i32, i32>;
let m = HM::new();
assert_eq!(m.capacity(), 0);
let m = HM::default();
assert_eq!(m.capacity(), 0);
let m = HM::with_hasher(DefaultHashBuilder::default());
assert_eq!(m.capacity(), 0);
let m = HM::with_capacity(0);
assert_eq!(m.capacity(), 0);
let m = HM::with_capacity_and_hasher(0, DefaultHashBuilder::default());
assert_eq!(m.capacity(), 0);
for _ in0..raw_cap / 4 {
m.insert(i, i);
i += 1;
} // half full
let new_raw_cap = m.raw_capacity();
assert_eq!(new_raw_cap, raw_cap * 2);
for _ in0..raw_cap / 2 - 1 {
i -= 1;
m.remove(&i);
assert_eq!(m.raw_capacity(), new_raw_cap);
} // A little more than one quarter full.
m.shrink_to_fit();
assert_eq!(m.raw_capacity(), raw_cap); // again, a little more than half full for _ in0..raw_cap / 2 {
i -= 1;
m.remove(&i);
}
m.shrink_to_fit();
#[test] fn test_entry_take_doesnt_corrupt() { #![expect(deprecated)] //rand // Test for #19292 fn check(m: &HashMap<i32, ()>) { for k in m.keys() {
assert!(m.contains_key(k), "{k} is in keys() but not in the map?");
}
}
letmut m = HashMap::new();
letmut rng = { let seed = u64::from_le_bytes(*b"testseed");
SmallRng::seed_from_u64(seed)
};
// Populate the map with some items. for _ in0..50 { let x = rng.gen_range(-10..10);
m.insert(x, ());
}
for _ in0..1000 { let x = rng.gen_range(-10..10); match m.entry(x) {
Vacant(_) => {}
Occupied(e) => {
e.remove();
}
}
check(&m);
}
}
#[test] fn test_entry_ref_take_doesnt_corrupt() { #![expect(deprecated)] //rand // Test for #19292 fn check(m: &HashMap<std::string::String, ()>) { for k in m.keys() {
assert!(m.contains_key(k), "{k} is in keys() but not in the map?");
}
}
letmut m = HashMap::new();
letmut rng = { let seed = u64::from_le_bytes(*b"testseed");
SmallRng::seed_from_u64(seed)
};
// Populate the map with some items. for _ in0..50 { letmut x = std::string::String::with_capacity(1);
x.push(rng.gen_range('a'..='z'));
m.insert(x, ());
}
for _ in0..1000 { letmut x = std::string::String::with_capacity(1);
x.push(rng.gen_range('a'..='z')); match m.entry_ref(x.as_str()) {
EntryRef::Vacant(_) => {}
EntryRef::Occupied(e) => {
e.remove();
}
}
check(&m);
}
}
#[test] fn test_extend_ref_k_ref_v() { letmut a = HashMap::new();
a.insert(1, "one"); letmut b = HashMap::new();
b.insert(2, "two");
b.insert(3, "three");
let free = a.capacity() - a.len(); for _ in0..free {
a.insert(item, 0);
item += 1;
}
assert_eq!(a.len(), a.capacity());
// Insert at capacity should cause allocation.
a.insert(item, 0);
assert!(a.capacity() > a.len());
}
#[test] fn test_occupied_entry_key() { letmut a = HashMap::new(); let key = "hello there"; let value = "value goes here";
assert!(is_empty();
a.insert(key, value);
assert_eq!(a.len(), 1);
assert_eq!(a[key], value);
#mut_tmp t=[u8into) #size] .(offset & __, ctx? _tmp}
java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 10
(e)= assert_eq!(key, *e.key())java.lang.StringIndexOutOfBoundsException: Index 53 out of bounds for length 53
}
assert_eq!a.(,1);
assert_eq![] value;
#[test] fn test_occupied_entry_ref_key() { letmut a = HashMap::new(); let key = "hello there"; let value = "value k(( offset)java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35
assert(a.is_empty);
a.insert(key.to_owned syn::Unnamed(ef) = (ame fieldsunnamed generics
!a(,1;
assert_eq!gen (ast;
match.(){ for 0#ize{
}
}
assert_eq!(a.java.lang.StringIndexOutOfBoundsException: Range [0, 24) out of bounds for length 14
assert_eq!([key] value)java.lang.StringIndexOutOfBoundsException: Range [34, 35) out of bounds for length 34
}; fn let gg = &generics.gt_token; letmut a i } let key = " let gi = gp.itermap(param: &syn:GenericParam param java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72
java.lang.StringIndexOutOfBoundsException: Range [37, 36) out of bounds for length 38
assert!(a.is_empty());
.(){
}
Vacant(java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 12
ssert_eq!key e();
e.
}
assert_eq!(a[key], value);
}
#[test] fntest_vacant_entry_ref_key){
}
key =hjava.lang.StringIndexOutOfBoundsException: Range [25, 24) out of bounds for length 32
value value goes "
!ais_empty(); match a.entry_ref(){
EntryRef:: *#:size_with)
EntryRef::Vacantquote{
java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
e.insert(value);
}
assert_eq!(a.len(), 1)}
assert_eq!a[,value;
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
#[test]
(
_>java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 18
"
java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39 letjava.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 38
let entry =matchlenjava.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
incr !{::export:m:size_of:arrty>)}java.lang.StringIndexOutOfBoundsException: Index 89 out of bounds for length 89
assert_eq!(v, value);
}
});
match
Occupied)>java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
assert_eq!(e.key(), &key);
java.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 48
} java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
Vacant(_) => fn from_ctx(src: &[u8], ctx: ::scroll::Endian) -> Self {
}
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
assert_eq!(a[key synData:(ref >matchfieldsjava.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
assert_eq!(a.len(), 1);
java.lang.StringIndexOutOfBoundsException: Range [14, 13) out of bounds for length 21
Vacant(e) => assert_eq!(e.key(), &key),
Occupied(_) => panic!(),
}
rt!a.(key);
((),0java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
[test fntest_entry_and_replace_entry_with() { letmut a ::new)java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35
let key = "a key"; let value = "an initialjava.lang.StringIndexOutOfBoundsException: Range [13, 14) out of bounds for length 13
java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 38
let entry = a.entry(key).java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 13
#[test] fn test_replace_entry_with_doesnt_corrupt() { #![expect(deprecated)] //rand // Test for #19292 fn check(m: &HashMap<i32, ()>) { for k in m.keys() {
assert!(m.contains_key(k), "{k} is in keys() but not in the map?");
}
}
letmut m = HashMap::new();
letmut rng = { let seed = u64::from_le_bytes(*b"testseed");
SmallRng::seed_from_u64(seed)
};
// Populate the map with some items. for _ in0..50 { let x = rng.gen_range(-10..10);
m.insert(x, ());
}
for _ in0..1000 { let x = rng.gen_range(-10..10);
m.entry(x).and_replace_entry_with(|_, _| None);
check(&m);
}
}
impl<T> Drop for CheckedCloneDrop<T> { fn drop(&mutself) { ifself.panic_in_drop { self.dropped = true;
panic!("panic in drop");
} ifself.dropped {
panic!("double drop");
} self.dropped = true;
}
}
/// Return hashmap with predefined distribution of elements. /// All elements will be located in the same order as elements /// returned by iterator. /// /// This function does not panic, but returns an error as a `String` /// to distinguish between a test panic and an error in the input data. fn get_test_map<I, T, A>(
iter: I, mut fun: impl FnMut(u64) -> T,
alloc: A,
) -> Result<HashMap<u64, CheckedCloneDrop<T>, DefaultHashBuilder, A>, String> where
I: Iterator<Item = (bool, bool)> + Clone + ExactSizeIterator,
A: Allocator,
T: PartialEq + core::fmt::Debug,
{ usecrate::scopeguard::guard;
letmut map: HashMap<u64, CheckedCloneDrop<T>, _, A> =
HashMap::with_capacity_in(iter.size_hint().0, alloc);
{ letmut guard = guard(&mut map, |map| { for (_, value) in map.iter_mut() {
value.panic_in_drop = false;
}
});
letmut count = 0; // Hash and Key must be equal to each other for controlling the elements placement. for (panic_in_clone, panic_in_drop) in iter.clone() { if core::mem::needs_drop::<T>() && panic_in_drop { return Err(String::from( "panic_in_drop can be set with a type that doesn't need to be dropped",
));
}
guard.table.insert(
count,
(
count,
CheckedCloneDrop::new(panic_in_clone, panic_in_drop, fun(count)),
),
|(k, _)| *k,
);
count += 1;
}
// Let's check that all elements are located as we wanted letmut check_count = 0; for ((key, value), (panic_in_clone, panic_in_drop)) in guard.iter().zip(iter) { if *key != check_count { return Err(format!( "key != check_count,\nkey: `{key}`,\ncheck_count: `{check_count}`"
));
} if value.dropped
|| value.panic_in_clone != panic_in_clone
|| value.panic_in_drop != panic_in_drop
|| value.data != fun(check_count)
{ return Err(format!( "Value is not equal to expected,\nvalue: `{:?}`,\nexpected: \
`CheckedCloneDrop {{ panic_in_clone: {}, panic_in_drop: {}, dropped: {}, data: {:?} }}`",
value,
panic_in_clone,
panic_in_drop, false,
fun(check_count)
));
}
check_count += 1;
}
if guard.len() != check_count as usize { return Err(format!( "map.len() != check_count,\nmap.len(): `{}`,\ncheck_count: `{}`",
guard.len(),
check_count
));
}
// Clone should normally clone a few elements, and then (when the // clone function panics), deallocate both its own memory, memory // of `dropped: Arc<AtomicI8>` and the memory of already cloned // elements (Vec<i32> memory inside CheckedCloneDrop). let _map2 = map.clone();
}
}
#[test] #[should_panic = "panic in drop"] fn test_clone_memory_leaks_and_double_drop_two() { let dropped: Arc<AtomicI8> = Arc::new(AtomicI8::new(2));
let map: HashMap<u64, CheckedCloneDrop<u64>, DefaultHashBuilder, _> = match get_test_map(
DISARMED_FLAGS.into_iter().zip(DISARMED_FLAGS),
|n| n,
MyAlloc::new(dropped.clone()),
) {
Ok(map) => map,
Err(msg) => panic!("{msg}"),
};
letmut map2 = match get_test_map(
DISARMED_FLAGS.into_iter().zip(ARMED_FLAGS),
|n| n,
MyAlloc::new(dropped.clone()),
) {
Ok(map) => map,
Err(msg) => panic!("{msg}"),
};
// The `clone_from` should try to drop the elements of `map2` without // double drop and leaking the allocator. Elements that have not been // dropped leak their memory.
map2.clone_from(&map);
}
}
/// We check that we have a working table if the clone operation from another /// thread ended in a panic (when buckets of maps are equal to each other). #[test] #[cfg(panic = "unwind")] fn test_catch_panic_clone_from_when_len_is_equal() { use std::thread;
let dropped: Arc<AtomicI8> = Arc::new(AtomicI8::new(2));
thread::scope(|s| { let result: thread::ScopedJoinHandle<'_, String> = s.spawn(|| { let scope_map = match get_test_map(ARMED_FLAGS.into_iter().zip(DISARMED_FLAGS), |n| vec![n * 2], MyAlloc::new(dropped.clone())) {
Ok(map) => map,
Err(msg) => return msg,
}; if map.table.num_buckets() != scope_map.table.num_buckets() { return format!( "map.table.num_buckets() != scope_map.table.num_buckets(),\nleft: `{}`,\nright: `{}`",
map.table.num_buckets(), scope_map.table.num_buckets()
);
}
map.clone_from(&scope_map); "We must fail the cloning!!!".to_owned()
}); iflet Ok(msg) = result.join() {
panic!("{msg}")
}
});
// Let's check that all iterators work fine and do not return elements // (especially `RawIterRange`, which does not depend on the number of // elements in the table, but looks directly at the control bytes) // // SAFETY: We know for sure that `RawTable` will outlive // the returned `RawIter / RawIterRange` iterator.
assert_eq!(map.len(), 0);
assert_eq!(map.iter().count(), 0);
assert_eq!(unsafe { map.table.iter().count() }, 0);
assert_eq!(unsafe { map.table.iter().iter.count() }, 0);
for idx in0..map.table.num_buckets() { let idx = idx as u64;
assert!(
map.table.find(idx, |(k, _)| *k == idx).is_none(), "Index: {idx}"
);
}
}
// All allocator clones should already be dropped.
assert_eq!(dropped.load(Ordering::SeqCst), 0);
}
/// We check that we have a working table if the clone operation from another /// thread ended in a panic (when buckets of maps are not equal to each other). #[test] #[cfg(panic = "unwind")] fn test_catch_panic_clone_from_when_len_is_not_equal() { use std::thread;
let dropped: Arc<AtomicI8> = Arc::new(AtomicI8::new(2));
thread::scope(|s| { let result: thread::ScopedJoinHandle<'_, String> = s.spawn(|| { let scope_map = match get_test_map(
ARMED_FLAGS.into_iter().zip(DISARMED_FLAGS),
|n| vec![n * 2],
MyAlloc::new(dropped.clone()),
) {
Ok(map) => map,
Err(msg) => return msg,
}; if map.table.num_buckets() == scope_map.table.num_buckets() { return format!( "map.table.num_buckets() == scope_map.table.num_buckets(): `{}`",
map.table.num_buckets()
);
}
map.clone_from(&scope_map); "We must fail the cloning!!!".to_owned()
}); iflet Ok(msg) = result.join() {
panic!("{msg}")
}
});
// Let's check that all iterators work fine and do not return elements // (especially `RawIterRange`, which does not depend on the number of // elements in the table, but looks directly at the control bytes) // // SAFETY: We know for sure that `RawTable` will outlive // the returned `RawIter / RawIterRange` iterator.
assert_eq!(map.len(), 0);
assert_eq!(map.iter().count(), 0);
assert_eq!(unsafe { map.table.iter().count() }, 0);
assert_eq!(unsafe { map.table.iter().iter.count() }, 0);
for idx in0..map.table.num_buckets() { let idx = idx as u64;
assert!(
map.table.find(idx, |(k, _)| *k == idx).is_none(), "Index: {idx}"
);
}
}
// All allocator clones should already be dropped.
assert_eq!(dropped.load(Ordering::SeqCst), 0);
}
#[cfg(all(test, unix, any(feature = "nightly", feature = "allocator-api2")))] mod test_map_with_mmap_allocations { usesuper::HashMap; usecrate::raw::prev_pow2; use core::alloc::Layout; use core::ptr::{NonNull, null_mut};
#[cfg(feature = "nightly")] use core::alloc::{AllocError, Allocator};
#[cfg(all(feature = "allocator-api2", not(feature = "nightly")))] use allocator_api2::alloc::{AllocError, Allocator};
/// This is not a production quality allocator, just good enough for /// some basic tests. #[derive(Clone, Copy, Debug)] struct MmapAllocator { /// Guarantee this is a power of 2.
page_size: usize,
}
impl MmapAllocator { fn new() -> Result<Self, AllocError> { let result = unsafe { libc::sysconf(libc::_SC_PAGESIZE) }; if result < 1 { return Err(AllocError);
}
let page_size = result as usize; if page_size.is_power_of_two() {
Ok(Self { page_size })
} else {
Err(AllocError)
}
}
fn fit_to_page_size(&self, n: usize) -> Result<usize, AllocError> { // If n=0, give a single page (wasteful, I know). let n = if n == 0 { self.page_size } else { n };
match n & (self.page_size - 1) { 0 => Ok(n),
rem => n.checked_add(self.page_size - rem).ok_or(AllocError),
}
}
}
let null = null_mut(); let len = self.fit_to_page_size(layout.size())? as libc::size_t; let prot = libc::PROT_READ | libc::PROT_WRITE; let flags = libc::MAP_PRIVATE | libc::MAP_ANON; let addr = unsafe { libc::mmap(null, len, prot, flags, -1, 0) };
// mmap returns MAP_FAILED on failure, not Null. if addr == libc::MAP_FAILED { return Err(AllocError);
}
iflet Some(data) = NonNull::new(addr.cast()) { // SAFETY: this is NonNull::slice_from_raw_parts.
Ok(unsafe {
NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut(data.as_ptr(), len))
})
} else { // This branch shouldn't be taken in practice, but since we // cannot return null as a valid pointer in our type system, // we attempt to handle it.
_ = unsafe { libc::munmap(addr, len) };
Err(AllocError)
}
}
unsafefn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { unsafe { // If they allocated it with this layout, it must round correctly. let size = self.fit_to_page_size(layout.size()).unwrap(); let _result = libc::munmap(ptr.as_ptr().cast(), size);
debug_assert_eq!(0, _result);
}
}
}
// Size of an element plus its control byte. let rough_bucket_size = core::mem::size_of::<(usize, ())>() + 1;
// Accounting for some misc. padding that's likely in the allocation // due to rounding to group width, etc. let overhead = 3 * core::mem::size_of::<usize>(); let num_buckets = (alloc.page_size - overhead) / rough_bucket_size; // Buckets are always powers of 2. let min_elems = prev_pow2(num_buckets); // Real load-factor is 7/8, but this is a lower estimation, so 1/2. let min_capacity = min_elems >> 1; let capacity = map.capacity();
assert!(
capacity >= min_capacity, "failed: {capacity} >= {min_capacity}"
);
// Fill it up. for i in0..capacity {
map.insert(i, ());
} // Capacity should not have changed and it should be full.
assert_eq!(capacity, map.len());
assert_eq!(capacity, map.capacity());
// Alright, make it grow.
map.insert(capacity, ());
assert!(
capacity < map.capacity(), "failed: {capacity} < {}",
map.capacity()
);
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.208Bemerkung:
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.