@@ -2990,12 +2990,53 @@ pub struct RawVacantEntryMut<'a, K, V, S, A: Allocator + Clone = Global> {
2990
2990
/// See the [`HashMap::raw_entry`] docs for usage examples.
2991
2991
///
2992
2992
/// [`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
+ /// ```
2993
3022
pub struct RawEntryBuilder < ' a , K , V , S , A : Allocator + Clone = Global > {
2994
3023
map : & ' a HashMap < K , V , S , A > ,
2995
3024
}
2996
3025
2997
3026
impl < ' a , K , V , S , A : Allocator + Clone > RawEntryBuilderMut < ' a , K , V , S , A > {
2998
3027
/// 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
+ /// ```
2999
3040
#[ cfg_attr( feature = "inline-more" , inline) ]
3000
3041
#[ allow( clippy:: wrong_self_convention) ]
3001
3042
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> {
3009
3050
}
3010
3051
3011
3052
/// 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
+ /// ```
3012
3074
#[ inline]
3013
3075
#[ allow( clippy:: wrong_self_convention) ]
3014
3076
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> {
3021
3083
}
3022
3084
3023
3085
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
+ /// ```
3025
3108
#[ cfg_attr( feature = "inline-more" , inline) ]
3026
3109
#[ allow( clippy:: wrong_self_convention) ]
3027
3110
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> {
3051
3134
}
3052
3135
3053
3136
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
+ /// ```
3055
3148
#[ cfg_attr( feature = "inline-more" , inline) ]
3056
3149
#[ allow( clippy:: wrong_self_convention) ]
3057
3150
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> {
3064
3157
self . from_key_hashed_nocheck ( hash, k)
3065
3158
}
3066
3159
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
+ /// ```
3068
3180
#[ cfg_attr( feature = "inline-more" , inline) ]
3069
3181
#[ allow( clippy:: wrong_self_convention) ]
3070
3182
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> {
3086
3198
}
3087
3199
}
3088
3200
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
+ /// ```
3090
3221
#[ cfg_attr( feature = "inline-more" , inline) ]
3091
3222
#[ allow( clippy:: wrong_self_convention) ]
3092
3223
pub fn from_hash < F > ( self , hash : u64 , is_match : F ) -> Option < ( & ' a K , & ' a V ) >
0 commit comments