Skip to content

Commit 89613b0

Browse files
committed
Add allocator parameter to rustc_entry
1 parent 3a97a8a commit 89613b0

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/rustc_entry.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use self::RustcEntry::*;
22
use crate::map::{make_hash, Drain, HashMap, IntoIter, Iter, IterMut};
3-
use crate::raw::{Bucket, Global, RawTable};
3+
use crate::raw::{AllocRef, Bucket, Global, RawTable};
44
use core::fmt::{self, Debug};
55
use core::hash::{BuildHasher, Hash};
66
use core::mem;
77

8-
impl<K, V, S> HashMap<K, V, S>
8+
impl<K, V, S, A> HashMap<K, V, S, A>
99
where
1010
K: Eq + Hash,
1111
S: BuildHasher,
12+
A: AllocRef + Clone,
1213
{
1314
/// Gets the given key's corresponding entry in the map for in-place manipulation.
1415
///
@@ -30,7 +31,7 @@ where
3031
/// assert_eq!(letters.get(&'y'), None);
3132
/// ```
3233
#[cfg_attr(feature = "inline-more", inline)]
33-
pub fn rustc_entry(&mut self, key: K) -> RustcEntry<'_, K, V> {
34+
pub fn rustc_entry(&mut self, key: K) -> RustcEntry<'_, K, V, A> {
3435
let hash = make_hash(&self.hash_builder, &key);
3536
if let Some(elem) = self.table.find(hash, |q| q.0.eq(&key)) {
3637
RustcEntry::Occupied(RustcOccupiedEntry {
@@ -59,15 +60,18 @@ where
5960
///
6061
/// [`HashMap`]: struct.HashMap.html
6162
/// [`entry`]: struct.HashMap.html#method.rustc_entry
62-
pub enum RustcEntry<'a, K, V> {
63+
pub enum RustcEntry<'a, K, V, A = Global>
64+
where
65+
A: AllocRef + Clone,
66+
{
6367
/// An occupied entry.
64-
Occupied(RustcOccupiedEntry<'a, K, V>),
68+
Occupied(RustcOccupiedEntry<'a, K, V, A>),
6569

6670
/// A vacant entry.
67-
Vacant(RustcVacantEntry<'a, K, V>),
71+
Vacant(RustcVacantEntry<'a, K, V, A>),
6872
}
6973

70-
impl<K: Debug, V: Debug> Debug for RustcEntry<'_, K, V> {
74+
impl<K: Debug, V: Debug, A: AllocRef + Clone> Debug for RustcEntry<'_, K, V, A> {
7175
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7276
match *self {
7377
Vacant(ref v) => f.debug_tuple("Entry").field(v).finish(),
@@ -80,26 +84,31 @@ impl<K: Debug, V: Debug> Debug for RustcEntry<'_, K, V> {
8084
/// It is part of the [`RustcEntry`] enum.
8185
///
8286
/// [`RustcEntry`]: enum.RustcEntry.html
83-
pub struct RustcOccupiedEntry<'a, K, V> {
87+
pub struct RustcOccupiedEntry<'a, K, V, A = Global>
88+
where
89+
A: AllocRef + Clone,
90+
{
8491
key: Option<K>,
8592
elem: Bucket<(K, V)>,
86-
table: &'a mut RawTable<(K, V), Global>,
93+
table: &'a mut RawTable<(K, V), A>,
8794
}
8895

89-
unsafe impl<K, V> Send for RustcOccupiedEntry<'_, K, V>
96+
unsafe impl<K, V, A> Send for RustcOccupiedEntry<'_, K, V, A>
9097
where
9198
K: Send,
9299
V: Send,
100+
A: AllocRef + Clone + Send,
93101
{
94102
}
95-
unsafe impl<K, V> Sync for RustcOccupiedEntry<'_, K, V>
103+
unsafe impl<K, V, A> Sync for RustcOccupiedEntry<'_, K, V, A>
96104
where
97105
K: Sync,
98106
V: Sync,
107+
A: AllocRef + Clone + Sync,
99108
{
100109
}
101110

102-
impl<K: Debug, V: Debug> Debug for RustcOccupiedEntry<'_, K, V> {
111+
impl<K: Debug, V: Debug, A: AllocRef + Clone> Debug for RustcOccupiedEntry<'_, K, V, A> {
103112
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104113
f.debug_struct("OccupiedEntry")
105114
.field("key", self.key())
@@ -112,19 +121,22 @@ impl<K: Debug, V: Debug> Debug for RustcOccupiedEntry<'_, K, V> {
112121
/// It is part of the [`RustcEntry`] enum.
113122
///
114123
/// [`RustcEntry`]: enum.RustcEntry.html
115-
pub struct RustcVacantEntry<'a, K, V> {
124+
pub struct RustcVacantEntry<'a, K, V, A = Global>
125+
where
126+
A: AllocRef + Clone,
127+
{
116128
hash: u64,
117129
key: K,
118-
table: &'a mut RawTable<(K, V), Global>,
130+
table: &'a mut RawTable<(K, V), A>,
119131
}
120132

121-
impl<K: Debug, V> Debug for RustcVacantEntry<'_, K, V> {
133+
impl<K: Debug, V, A: AllocRef + Clone> Debug for RustcVacantEntry<'_, K, V, A> {
122134
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123135
f.debug_tuple("VacantEntry").field(self.key()).finish()
124136
}
125137
}
126138

127-
impl<'a, K, V> RustcEntry<'a, K, V> {
139+
impl<'a, K, V, A: AllocRef + Clone> RustcEntry<'a, K, V, A> {
128140
/// Sets the value of the entry, and returns a RustcOccupiedEntry.
129141
///
130142
/// # Examples
@@ -137,7 +149,7 @@ impl<'a, K, V> RustcEntry<'a, K, V> {
137149
///
138150
/// assert_eq!(entry.key(), &"horseyland");
139151
/// ```
140-
pub fn insert(self, value: V) -> RustcOccupiedEntry<'a, K, V> {
152+
pub fn insert(self, value: V) -> RustcOccupiedEntry<'a, K, V, A> {
141153
match self {
142154
Vacant(entry) => entry.insert_entry(value),
143155
Occupied(mut entry) => {
@@ -253,7 +265,7 @@ impl<'a, K, V> RustcEntry<'a, K, V> {
253265
}
254266
}
255267

256-
impl<'a, K, V: Default> RustcEntry<'a, K, V> {
268+
impl<'a, K, V: Default, A: AllocRef + Clone> RustcEntry<'a, K, V, A> {
257269
/// Ensures a value is in the entry by inserting the default value if empty,
258270
/// and returns a mutable reference to the value in the entry.
259271
///
@@ -281,7 +293,7 @@ impl<'a, K, V: Default> RustcEntry<'a, K, V> {
281293
}
282294
}
283295

284-
impl<'a, K, V> RustcOccupiedEntry<'a, K, V> {
296+
impl<'a, K, V, A: AllocRef + Clone> RustcOccupiedEntry<'a, K, V, A> {
285297
/// Gets a reference to the key in the entry.
286298
///
287299
/// # Examples
@@ -508,7 +520,7 @@ impl<'a, K, V> RustcOccupiedEntry<'a, K, V> {
508520
}
509521
}
510522

511-
impl<'a, K, V> RustcVacantEntry<'a, K, V> {
523+
impl<'a, K, V, A: AllocRef + Clone> RustcVacantEntry<'a, K, V, A> {
512524
/// Gets a reference to the key that would be used when inserting a value
513525
/// through the `RustcVacantEntry`.
514526
///
@@ -583,7 +595,7 @@ impl<'a, K, V> RustcVacantEntry<'a, K, V> {
583595
/// }
584596
/// ```
585597
#[cfg_attr(feature = "inline-more", inline)]
586-
pub fn insert_entry(self, value: V) -> RustcOccupiedEntry<'a, K, V> {
598+
pub fn insert_entry(self, value: V) -> RustcOccupiedEntry<'a, K, V, A> {
587599
let bucket = self.table.insert_no_grow(self.hash, (self.key, value));
588600
RustcOccupiedEntry {
589601
key: None,

0 commit comments

Comments
 (0)