Skip to content

Commit 0495a36

Browse files
committed
Update hashbrown and adjust API
Signed-off-by: Heinz N. Gies <[email protected]>
1 parent 650c7b0 commit 0495a36

File tree

7 files changed

+149
-132
lines changed

7 files changed

+149
-132
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ repository = "https://github.com/Licenser/halfbrown"
88
version = "0.2.5"
99

1010
[dependencies]
11-
hashbrown = "0.14"
12-
rustc-hash = { version = "1.1", optional = true }
11+
hashbrown = "0.15"
12+
rustc-hash = { version = "2.1", optional = true }
1313
serde = { version = "1", default-features = false, optional = true }
1414
arrayvec = { version = "0.7", optional = true }
1515
[dev-dependencies]

benches/compare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn bench_group(b: &mut Criterion, name: &str, bench_input: BenchInput) {
230230
|data| {
231231
let mut m = halfbrown::HashMap::with_capacity(bench_input.initial_cap);
232232
for e in data {
233-
m.insert_nocheck(e, e);
233+
unsafe { m.insert_nocheck(e, e) };
234234
}
235235
},
236236
BatchSize::SmallInput,

src/entry.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -438,57 +438,57 @@ impl<'a, K, V, const N: usize, S> OccupiedEntry<'a, K, V, N, S> {
438438
/// # Examples
439439
///
440440
/// ```
441-
/// use hashbrown::hash_map::{Entry, HashMap};
441+
/// use halfbrown::{Entry, HashMap};
442442
/// use std::rc::Rc;
443443
///
444-
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
445-
/// map.insert(Rc::new("Stringthing".to_string()), 15);
446-
///
447-
/// let my_key = Rc::new("Stringthing".to_string());
444+
/// let mut map: HashMap<&str, u32> = HashMap::new();
445+
/// map.insert("poneyland", 42);
446+
///
447+
/// let entry = match map.entry("poneyland") {
448+
/// Entry::Occupied(e) => {
449+
/// e.replace_entry_with(|k, v| {
450+
/// assert_eq!(k, &"poneyland");
451+
/// assert_eq!(v, 42);
452+
/// Some(v + 1)
453+
/// })
454+
/// }
455+
/// Entry::Vacant(_) => panic!(),
456+
/// };
448457
///
449-
/// if let Entry::Occupied(entry) = map.entry(my_key) {
450-
/// // Also replace the key with a handle to our other key.
451-
/// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
458+
/// match entry {
459+
/// Entry::Occupied(e) => {
460+
/// assert_eq!(e.key(), &"poneyland");
461+
/// assert_eq!(e.get(), &43);
462+
/// }
463+
/// Entry::Vacant(_) => panic!(),
452464
/// }
453465
///
454-
/// ```
455-
#[inline]
456-
pub fn replace_entry(self, value: V) -> (K, V) {
457-
match self.0 {
458-
OccupiedEntryInt::Map(m) => m.replace_entry(value),
459-
OccupiedEntryInt::Vec(m) => m.replace_entry(value),
460-
}
461-
}
462-
463-
/// Replaces the key in the hash map with the key used to create this entry.
464-
///
465-
/// # Examples
466-
///
467-
/// ```
468-
/// use hashbrown::hash_map::{Entry, HashMap};
469-
/// use std::rc::Rc;
470-
///
471-
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
472-
/// let mut known_strings: Vec<Rc<String>> = Vec::new();
473-
///
474-
/// // Initialise known strings, run program, etc.
466+
/// assert_eq!(map["poneyland"], 43);
475467
///
476-
/// reclaim_memory(&mut map, &known_strings);
468+
/// let entry = match map.entry("poneyland") {
469+
/// Entry::Occupied(e) => e.replace_entry_with(|_k, _v| None),
470+
/// Entry::Vacant(_) => panic!(),
471+
/// };
477472
///
478-
/// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
479-
/// for s in known_strings {
480-
/// if let Entry::Occupied(entry) = map.entry(s.clone()) {
481-
/// // Replaces the entry's key with our version of it in `known_strings`.
482-
/// entry.replace_key();
483-
/// }
473+
/// match entry {
474+
/// Entry::Vacant(e) => {
475+
/// assert_eq!(e.key(), &"poneyland");
484476
/// }
477+
/// Entry::Occupied(_) => panic!(),
485478
/// }
479+
///
480+
/// assert!(!map.contains_key("poneyland"));
481+
///
486482
/// ```
487483
#[inline]
488-
pub fn replace_key(self) -> K {
484+
pub fn replace_entry_with<F>(self, f: F) -> Entry<'a, K, V, N, S>
485+
where
486+
F: FnOnce(&K, V) -> Option<V>,
487+
V: Clone,
488+
{
489489
match self.0 {
490-
OccupiedEntryInt::Map(m) => m.replace_key(),
491-
OccupiedEntryInt::Vec(m) => m.replace_key(),
490+
OccupiedEntryInt::Map(m) => m.replace_entry_with(f).into(),
491+
OccupiedEntryInt::Vec(m) => m.replace_entry_with(f).into(),
492492
}
493493
}
494494
}

src/iter.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::iter::{FromIterator, FusedIterator, IntoIterator};
88
pub struct Iter<'a, K, V>(IterInt<'a, K, V>);
99

1010
/// Manual implementation so that `Clone` isn't required for `K` nor `V`
11-
impl<'a, K, V> Clone for Iter<'a, K, V> {
11+
impl<K, V> Clone for Iter<'_, K, V> {
1212
fn clone(&self) -> Self {
1313
Iter(self.0.clone())
1414
}
@@ -27,7 +27,7 @@ pub(crate) enum IterInt<'a, K, V> {
2727
}
2828

2929
/// Manual implementation so that `Clone` isn't required for `K` nor `V`
30-
impl<'a, K, V> Clone for IterInt<'a, K, V> {
30+
impl<K, V> Clone for IterInt<'_, K, V> {
3131
fn clone(&self) -> Self {
3232
match self {
3333
IterInt::Map(i) => IterInt::Map(i.clone()),
@@ -60,7 +60,7 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
6060
}
6161
}
6262

63-
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
63+
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
6464
#[inline]
6565
fn len(&self) -> usize {
6666
match &self.0 {
@@ -70,7 +70,7 @@ impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
7070
}
7171
}
7272

73-
impl<'a, K, V> FusedIterator for Iter<'a, K, V> {}
73+
impl<K, V> FusedIterator for Iter<'_, K, V> {}
7474

7575
/// Into iterator for a Halfbrown map
7676
pub struct IntoIter<K, V, const N: usize>(IntoIterInt<K, V, N>);
@@ -149,6 +149,16 @@ impl<'a, K, V, S, const N: usize> IntoIterator for &'a SizedHashMap<K, V, S, N>
149149
}
150150
}
151151

152+
impl<'a, K, V, S, const N: usize> IntoIterator for &'a mut SizedHashMap<K, V, S, N> {
153+
type Item = (&'a K, &'a V);
154+
type IntoIter = Iter<'a, K, V>;
155+
156+
#[inline]
157+
fn into_iter(self) -> Iter<'a, K, V> {
158+
self.iter()
159+
}
160+
}
161+
152162
impl<K, V, S, const N: usize> FromIterator<(K, V)> for SizedHashMap<K, V, S, N>
153163
where
154164
K: Eq + Hash,
@@ -198,7 +208,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
198208
}
199209
}
200210

201-
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
211+
impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
202212
#[inline]
203213
fn len(&self) -> usize {
204214
match &self.0 {
@@ -208,4 +218,4 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
208218
}
209219
}
210220

211-
impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
221+
impl<K, V> FusedIterator for IterMut<'_, K, V> {}

src/lib.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@
1919
//! their copyright.
2020
2121
#![warn(unused_extern_crates)]
22-
#![cfg_attr(
23-
feature = "cargo-clippy",
24-
deny(
25-
clippy::all,
26-
clippy::unwrap_used,
27-
clippy::unnecessary_unwrap,
28-
clippy::pedantic
29-
),
30-
// We might want to revisit inline_always
31-
allow(clippy::module_name_repetitions, clippy::inline_always)
22+
#![deny(
23+
clippy::all,
24+
clippy::unwrap_used,
25+
clippy::unnecessary_unwrap,
26+
clippy::pedantic
3227
)]
28+
// We might want to revisit inline_always
29+
#![allow(clippy::module_name_repetitions, clippy::inline_always)]
3330
#![deny(missing_docs)]
3431

3532
mod entry;
@@ -58,7 +55,7 @@ pub type DefaultHashBuilder = core::hash::BuildHasherDefault<rustc_hash::FxHashe
5855

5956
use crate::vectypes::VecDrain;
6057
#[cfg(not(feature = "fxhash"))]
61-
pub use hashbrown::hash_map::DefaultHashBuilder;
58+
pub use hashbrown::DefaultHashBuilder;
6259

6360
/// `SizedHashMap` implementation that alternates between a vector
6461
/// and a hashmap to improve performance for low key counts.
@@ -67,7 +64,6 @@ pub type HashMap<K, V, S = DefaultHashBuilder> = SizedHashMap<K, V, S, 32>;
6764

6865
/// Maximum nymber of elements before the representaiton is swapped from
6966
/// Vec to `HashMap`
70-
7167
/// `SizedHashMap` implementation that alternates between a vector
7268
/// and a hashmap to improve performance for low key counts. With a configurable upper vector limit
7369
#[derive(Clone)]
@@ -172,8 +168,8 @@ impl<K, V, S, const VEC_LIMIT_UPPER: usize> SizedHashMap<K, V, S, VEC_LIMIT_UPPE
172168
/// # Examples
173169
///
174170
/// ```
175-
/// use hashbrown::HashMap;
176-
/// use hashbrown::hash_map::DefaultHashBuilder;
171+
/// use halfbrown::HashMap;
172+
/// use halfbrown::DefaultHashBuilder;
177173
///
178174
/// let s = DefaultHashBuilder::default();
179175
/// let mut map = HashMap::with_hasher(s);
@@ -198,8 +194,8 @@ impl<K, V, S, const VEC_LIMIT_UPPER: usize> SizedHashMap<K, V, S, VEC_LIMIT_UPPE
198194
/// # Examples
199195
///
200196
/// ```
201-
/// use hashbrown::HashMap;
202-
/// use hashbrown::hash_map::DefaultHashBuilder;
197+
/// use halfbrown::HashMap;
198+
/// use halfbrown::DefaultHashBuilder;
203199
///
204200
/// let s = DefaultHashBuilder::default();
205201
/// let mut map = HashMap::with_capacity_and_hasher(10, s);
@@ -221,8 +217,8 @@ impl<K, V, S, const VEC_LIMIT_UPPER: usize> SizedHashMap<K, V, S, VEC_LIMIT_UPPE
221217
/// # Examples
222218
///
223219
/// ```
224-
/// use hashbrown::HashMap;
225-
/// use hashbrown::hash_map::DefaultHashBuilder;
220+
/// use halfbrown::HashMap;
221+
/// use halfbrown::DefaultHashBuilder;
226222
///
227223
/// let hasher = DefaultHashBuilder::default();
228224
/// let map: HashMap<i32, i32> = HashMap::with_hasher(hasher);
@@ -830,8 +826,13 @@ where
830826
/// Inserts element, this ignores check in the vector
831827
/// map if keys are present - it's a fast way to build
832828
/// a new map when uniqueness is known ahead of time.
829+
///
830+
/// # Safety
831+
///
832+
/// Used for building new hashmaps wher eit is known that
833+
/// the keys are unique.
833834
#[inline]
834-
pub fn insert_nocheck(&mut self, k: K, v: V)
835+
pub unsafe fn insert_nocheck(&mut self, k: K, v: V)
835836
where
836837
S: Default,
837838
{
@@ -1054,7 +1055,7 @@ enum DrainInt<'a, K, V, const N: usize> {
10541055
Vec(VecDrain<'a, (K, V), N>),
10551056
}
10561057

1057-
impl<'a, K, V, const N: usize> Iterator for Drain<'a, K, V, N> {
1058+
impl<K, V, const N: usize> Iterator for Drain<'_, K, V, N> {
10581059
type Item = (K, V);
10591060
#[inline]
10601061
fn next(&mut self) -> Option<Self::Item> {

src/raw_entry.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ where
301301
/// # Examples
302302
///
303303
/// ```
304-
/// use hashbrown::HashMap;
304+
/// use halfbrown::HashMap;
305305
///
306306
/// let mut map: HashMap<&str, u32> = HashMap::new();
307307
/// let entry = map.raw_entry_mut().from_key("horseyland").insert("horseyland", 37);
@@ -339,7 +339,7 @@ where
339339
/// # Examples
340340
///
341341
/// ```
342-
/// use hashbrown::HashMap;
342+
/// use halfbrown::HashMap;
343343
///
344344
/// let mut map: HashMap<&str, u32> = HashMap::new();
345345
///
@@ -367,7 +367,7 @@ where
367367
/// # Examples
368368
///
369369
/// ```
370-
/// use hashbrown::HashMap;
370+
/// use halfbrown::HashMap;
371371
///
372372
/// let mut map: HashMap<&str, String> = HashMap::new();
373373
///
@@ -399,7 +399,7 @@ where
399399
/// # Examples
400400
///
401401
/// ```
402-
/// use hashbrown::HashMap;
402+
/// use halfbrown::HashMap;
403403
///
404404
/// let mut map: HashMap<&str, u32> = HashMap::new();
405405
///

0 commit comments

Comments
 (0)