Skip to content

Commit 9510b3d

Browse files
committed
One more update
1 parent be11593 commit 9510b3d

File tree

1 file changed

+135
-4
lines changed

1 file changed

+135
-4
lines changed

src/map.rs

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,12 +2990,53 @@ pub struct RawVacantEntryMut<'a, K, V, S, A: Allocator + Clone = Global> {
29902990
/// See the [`HashMap::raw_entry`] docs for usage examples.
29912991
///
29922992
/// [`HashMap::raw_entry`]: struct.HashMap.html#method.raw_entry
2993+
///
2994+
/// # Examples
2995+
///
2996+
/// ```
2997+
/// use hashbrown::hash_map::{HashMap, RawEntryBuilder};
2998+
/// use core::hash::{BuildHasher, Hash};
2999+
///
3000+
/// let mut map = HashMap::new();
3001+
/// map.extend([(1, 10), (2, 20), (3, 30)]);
3002+
///
3003+
/// fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
3004+
/// use core::hash::Hasher;
3005+
/// let mut state = hash_builder.build_hasher();
3006+
/// key.hash(&mut state);
3007+
/// state.finish()
3008+
/// }
3009+
///
3010+
/// for k in 0..6 {
3011+
/// let hash = compute_hash(map.hasher(), &k);
3012+
/// let v = map.get(&k).cloned();
3013+
/// let kv = v.as_ref().map(|v| (&k, v));
3014+
///
3015+
/// println!("Key: {} and value: {:?}", k, v);
3016+
/// let builder: RawEntryBuilder<_, _, _> = map.raw_entry();
3017+
/// assert_eq!(builder.from_key(&k), kv);
3018+
/// assert_eq!(map.raw_entry().from_hash(hash, |q| *q == k), kv);
3019+
/// assert_eq!(map.raw_entry().from_key_hashed_nocheck(hash, &k), kv);
3020+
/// }
3021+
/// ```
29933022
pub struct RawEntryBuilder<'a, K, V, S, A: Allocator + Clone = Global> {
29943023
map: &'a HashMap<K, V, S, A>,
29953024
}
29963025

29973026
impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilderMut<'a, K, V, S, A> {
29983027
/// Creates a `RawEntryMut` from the given key.
3028+
///
3029+
/// # Examples
3030+
///
3031+
/// ```
3032+
/// use hashbrown::hash_map::{HashMap, RawEntryMut};
3033+
///
3034+
/// let mut map: HashMap<&str, u32> = HashMap::new();
3035+
/// let key = "a";
3036+
/// let entry: RawEntryMut<&str, u32, _> = map.raw_entry_mut().from_key(&key);
3037+
/// entry.insert(key, 100);
3038+
/// assert_eq!(map[&"a"], 100);
3039+
/// ```
29993040
#[cfg_attr(feature = "inline-more", inline)]
30003041
#[allow(clippy::wrong_self_convention)]
30013042
pub fn from_key<Q: ?Sized>(self, k: &Q) -> RawEntryMut<'a, K, V, S, A>
@@ -3009,6 +3050,27 @@ impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilderMut<'a, K, V, S, A> {
30093050
}
30103051

30113052
/// Creates a `RawEntryMut` from the given key and its hash.
3053+
///
3054+
/// # Examples
3055+
///
3056+
/// ```
3057+
/// use core::hash::{BuildHasher, Hash};
3058+
/// use hashbrown::hash_map::{HashMap, RawEntryMut};
3059+
///
3060+
/// fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
3061+
/// use core::hash::Hasher;
3062+
/// let mut state = hash_builder.build_hasher();
3063+
/// key.hash(&mut state);
3064+
/// state.finish()
3065+
/// }
3066+
///
3067+
/// let mut map: HashMap<&str, u32> = HashMap::new();
3068+
/// let key = "a";
3069+
/// let hash = compute_hash(map.hasher(), &key);
3070+
/// let entry: RawEntryMut<&str, u32, _> = map.raw_entry_mut().from_key_hashed_nocheck(hash, &key);
3071+
/// entry.insert(key, 100);
3072+
/// assert_eq!(map[&"a"], 100);
3073+
/// ```
30123074
#[inline]
30133075
#[allow(clippy::wrong_self_convention)]
30143076
pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> RawEntryMut<'a, K, V, S, A>
@@ -3021,7 +3083,28 @@ impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilderMut<'a, K, V, S, A> {
30213083
}
30223084

30233085
impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilderMut<'a, K, V, S, A> {
3024-
/// Creates a `RawEntryMut` from the given hash.
3086+
/// Creates a `RawEntryMut` from the given hash and matching function.
3087+
///
3088+
/// # Examples
3089+
///
3090+
/// ```
3091+
/// use core::hash::{BuildHasher, Hash};
3092+
/// use hashbrown::hash_map::{HashMap, RawEntryMut};
3093+
///
3094+
/// fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
3095+
/// use core::hash::Hasher;
3096+
/// let mut state = hash_builder.build_hasher();
3097+
/// key.hash(&mut state);
3098+
/// state.finish()
3099+
/// }
3100+
///
3101+
/// let mut map: HashMap<&str, u32> = HashMap::new();
3102+
/// let key = "a";
3103+
/// let hash = compute_hash(map.hasher(), &key);
3104+
/// let entry: RawEntryMut<&str, u32, _> = map.raw_entry_mut().from_hash(hash, |k| k == &key);
3105+
/// entry.insert(key, 100);
3106+
/// assert_eq!(map[&"a"], 100);
3107+
/// ```
30253108
#[cfg_attr(feature = "inline-more", inline)]
30263109
#[allow(clippy::wrong_self_convention)]
30273110
pub fn from_hash<F>(self, hash: u64, is_match: F) -> RawEntryMut<'a, K, V, S, A>
@@ -3051,7 +3134,17 @@ impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilderMut<'a, K, V, S, A> {
30513134
}
30523135

30533136
impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilder<'a, K, V, S, A> {
3054-
/// Access an entry by key.
3137+
/// Access an immutable entry by key.
3138+
///
3139+
/// # Examples
3140+
///
3141+
/// ```
3142+
/// use hashbrown::HashMap;
3143+
///
3144+
/// let map: HashMap<&str, u32> = [("a", 100), ("b", 200)].into();
3145+
/// let key = "a";
3146+
/// assert_eq!(map.raw_entry().from_key(&key), Some((&"a", &100)));
3147+
/// ```
30553148
#[cfg_attr(feature = "inline-more", inline)]
30563149
#[allow(clippy::wrong_self_convention)]
30573150
pub fn from_key<Q: ?Sized>(self, k: &Q) -> Option<(&'a K, &'a V)>
@@ -3064,7 +3157,26 @@ impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilder<'a, K, V, S, A> {
30643157
self.from_key_hashed_nocheck(hash, k)
30653158
}
30663159

3067-
/// Access an entry by a key and its hash.
3160+
/// Access an immutable entry by a key and its hash.
3161+
///
3162+
/// # Examples
3163+
///
3164+
/// ```
3165+
/// use core::hash::{BuildHasher, Hash};
3166+
/// use hashbrown::HashMap;
3167+
///
3168+
/// fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
3169+
/// use core::hash::Hasher;
3170+
/// let mut state = hash_builder.build_hasher();
3171+
/// key.hash(&mut state);
3172+
/// state.finish()
3173+
/// }
3174+
///
3175+
/// let map: HashMap<&str, u32> = [("a", 100), ("b", 200)].into();
3176+
/// let key = "a";
3177+
/// let hash = compute_hash(map.hasher(), &key);
3178+
/// assert_eq!(map.raw_entry().from_key_hashed_nocheck(hash, &key), Some((&"a", &100)));
3179+
/// ```
30683180
#[cfg_attr(feature = "inline-more", inline)]
30693181
#[allow(clippy::wrong_self_convention)]
30703182
pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> Option<(&'a K, &'a V)>
@@ -3086,7 +3198,26 @@ impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilder<'a, K, V, S, A> {
30863198
}
30873199
}
30883200

3089-
/// Access an entry by hash.
3201+
/// Access an immutable entry by hash and matching function.
3202+
///
3203+
/// # Examples
3204+
///
3205+
/// ```
3206+
/// use core::hash::{BuildHasher, Hash};
3207+
/// use hashbrown::HashMap;
3208+
///
3209+
/// fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
3210+
/// use core::hash::Hasher;
3211+
/// let mut state = hash_builder.build_hasher();
3212+
/// key.hash(&mut state);
3213+
/// state.finish()
3214+
/// }
3215+
///
3216+
/// let map: HashMap<&str, u32> = [("a", 100), ("b", 200)].into();
3217+
/// let key = "a";
3218+
/// let hash = compute_hash(map.hasher(), &key);
3219+
/// assert_eq!(map.raw_entry().from_hash(hash, |k| k == &key), Some((&"a", &100)));
3220+
/// ```
30903221
#[cfg_attr(feature = "inline-more", inline)]
30913222
#[allow(clippy::wrong_self_convention)]
30923223
pub fn from_hash<F>(self, hash: u64, is_match: F) -> Option<(&'a K, &'a V)>

0 commit comments

Comments
 (0)