1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use super::{IdHashItem, IdHashMap, RefMut};
use crate::{
DefaultHashBuilder,
support::{
alloc::{Allocator, Global},
borrow::DormantMutRef,
map_hash::MapHash,
},
};
use core::{fmt, hash::BuildHasher};
/// An implementation of the Entry API for [`IdHashMap`].
pub enum Entry<'a, T: IdHashItem, S = DefaultHashBuilder, A: Allocator = Global>
{
/// A vacant entry.
Vacant(VacantEntry<'a, T, S, A>),
/// An occupied entry.
Occupied(OccupiedEntry<'a, T, S, A>),
}
impl<'a, T: IdHashItem, S, A: Allocator> fmt::Debug for Entry<'a, T, S, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Entry::Vacant(entry) => {
f.debug_tuple("Vacant").field(entry).finish()
}
Entry::Occupied(entry) => {
f.debug_tuple("Occupied").field(entry).finish()
}
}
}
}
impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator>
Entry<'a, T, S, A>
{
/// Ensures a value is in the entry by inserting the default if empty, and
/// returns a mutable reference to the value in the entry.
///
/// # Panics
///
/// Panics if the key hashes to a different value than the one passed
/// into [`IdHashMap::entry`].
#[inline]
pub fn or_insert(self, default: T) -> RefMut<'a, T, S> {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::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.
///
/// # Panics
///
/// Panics if the key hashes to a different value than the one passed
/// into [`IdHashMap::entry`].
#[inline]
pub fn or_insert_with<F: FnOnce() -> T>(
self,
default: F,
) -> RefMut<'a, T, S> {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default()),
}
}
/// Provides in-place mutable access to an occupied entry before any
/// potential inserts into the map.
#[inline]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(RefMut<'_, T, S>),
{
match self {
Entry::Occupied(mut entry) => {
f(entry.get_mut());
Entry::Occupied(entry)
}
Entry::Vacant(entry) => Entry::Vacant(entry),
}
}
}
/// A vacant entry.
pub struct VacantEntry<
'a,
T: IdHashItem,
S = DefaultHashBuilder,
A: Allocator = Global,
> {
map: DormantMutRef<'a, IdHashMap<T, S, A>>,
hash: MapHash,
}
impl<'a, T: IdHashItem, S, A: Allocator> fmt::Debug
for VacantEntry<'a, T, S, A>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VacantEntry")
.field("hash", &self.hash)
.finish_non_exhaustive()
}
}
impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator>
VacantEntry<'a, T, S, A>
{
pub(super) unsafe fn new(
map: DormantMutRef<'a, IdHashMap<T, S, A>>,
hash: MapHash,
) -> Self {
VacantEntry { map, hash }
}
/// Sets the entry to a new value, returning a mutable reference to the
/// value.
pub fn insert(self, value: T) -> RefMut<'a, T, S> {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
let map = unsafe { self.map.awaken() };
let state = &map.tables.state;
if !self.hash.is_same_hash(state, value.key()) {
panic!("key hashes do not match");
}
let Ok(index) = map.insert_unique_impl(value) else {
panic!("key already present in map");
};
map.get_by_index_mut(index).expect("index is known to be valid")
}
/// Sets the value of the entry, and returns an `OccupiedEntry`.
#[inline]
pub fn insert_entry(mut self, value: T) -> OccupiedEntry<'a, T, S, A> {
let index = {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
let map = unsafe { self.map.reborrow() };
let state = &map.tables.state;
if !self.hash.is_same_hash(state, value.key()) {
panic!("key hashes do not match");
}
let Ok(index) = map.insert_unique_impl(value) else {
panic!("key already present in map");
};
index
};
// SAFETY: map, as well as anything that was borrowed from it, is
// dropped once the above block exits.
unsafe { OccupiedEntry::new(self.map, index) }
}
}
/// A view into an occupied entry in an [`IdHashMap`]. Part of the [`Entry`]
/// enum.
pub struct OccupiedEntry<
'a,
T: IdHashItem,
S = DefaultHashBuilder,
A: Allocator = Global,
> {
map: DormantMutRef<'a, IdHashMap<T, S, A>>,
// index is a valid index into the map's internal hash table.
index: usize,
}
impl<'a, T: IdHashItem, S, A: Allocator> fmt::Debug
for OccupiedEntry<'a, T, S, A>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntry")
.field("index", &self.index)
.finish_non_exhaustive()
}
}
impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator>
OccupiedEntry<'a, T, S, A>
{
/// # Safety
///
/// After self is created, the original reference created by
/// `DormantMutRef::new` must not be used.
pub(super) unsafe fn new(
map: DormantMutRef<'a, IdHashMap<T, S, A>>,
index: usize,
) -> Self {
OccupiedEntry { map, index }
}
/// Gets a reference to the value.
///
/// If you need a reference to `T` that may outlive the destruction of the
/// `Entry` value, see [`into_ref`](Self::into_ref).
pub fn get(&self) -> &T {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
unsafe { self.map.reborrow_shared() }
.get_by_index(self.index)
.expect("index is known to be valid")
}
/// Gets a mutable reference to the value.
///
/// If you need a reference to `T` that may outlive the destruction of the
/// `Entry` value, see [`into_mut`](Self::into_mut).
pub fn get_mut(&mut self) -> RefMut<'_, T, S> {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
unsafe { self.map.reborrow() }
.get_by_index_mut(self.index)
.expect("index is known to be valid")
}
/// Converts self into a reference to the value.
///
/// If you need multiple references to the `OccupiedEntry`, see
/// [`get`](Self::get).
pub fn into_ref(self) -> &'a T {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
unsafe { self.map.awaken() }
.get_by_index(self.index)
.expect("index is known to be valid")
}
/// Converts self into a mutable reference to the value.
///
/// If you need multiple references to the `OccupiedEntry`, see
/// [`get_mut`](Self::get_mut).
pub fn into_mut(self) -> RefMut<'a, T, S> {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
unsafe { self.map.awaken() }
.get_by_index_mut(self.index)
.expect("index is known to be valid")
}
/// Sets the entry to a new value, returning the old value.
///
/// # Panics
///
/// Panics if `value.key()` is different from the key of the entry.
pub fn insert(&mut self, value: T) -> T {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
//
// Note that `replace_at_index` panics if the keys don't match.
unsafe { self.map.reborrow() }.replace_at_index(self.index, value)
}
/// Takes ownership of the value from the map.
pub fn remove(mut self) -> T {
// SAFETY: The safety assumption behind `Self::new` guarantees that the
// original reference to the map is not used at this point.
unsafe { self.map.reborrow() }
.remove_by_index(self.index)
.expect("index is known to be valid")
}
}