Skip to content

Commit 13f3a22

Browse files
committed
Use table iterators (requires rust-lang#667)
1 parent 89c7f05 commit 13f3a22

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

src/map.rs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::raw::{Allocator, Bucket, Global, RawDrain, RawExtractIf, RawIntoIter, RawIter};
2-
use crate::{DefaultHashBuilder, Equivalent, HashTable, TryReserveError};
1+
use crate::raw::{Allocator, Bucket, Global, RawExtractIf};
2+
use crate::{hash_table, DefaultHashBuilder, Equivalent, HashTable, TryReserveError};
33
use core::borrow::Borrow;
44
use core::fmt::{self, Debug};
55
use core::hash::{BuildHasher, Hash};
@@ -750,12 +750,8 @@ impl<K, V, S, A: Allocator> HashMap<K, V, S, A> {
750750
/// ```
751751
#[cfg_attr(feature = "inline-more", inline)]
752752
pub fn iter(&self) -> Iter<'_, K, V> {
753-
// Here we tie the lifetime of self to the iter.
754-
unsafe {
755-
Iter {
756-
inner: self.table.raw.iter(),
757-
marker: PhantomData,
758-
}
753+
Iter {
754+
inner: self.table.iter(),
759755
}
760756
}
761757

@@ -795,10 +791,10 @@ impl<K, V, S, A: Allocator> HashMap<K, V, S, A> {
795791
/// ```
796792
#[cfg_attr(feature = "inline-more", inline)]
797793
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
798-
// Here we tie the lifetime of self to the iter.
794+
// We need iter_unsafe_mut to make the keys variant
799795
unsafe {
800796
IterMut {
801-
inner: self.table.raw.iter(),
797+
inner: self.table.iter_unsafe_mut(),
802798
marker: PhantomData,
803799
}
804800
}
@@ -885,7 +881,7 @@ impl<K, V, S, A: Allocator> HashMap<K, V, S, A> {
885881
#[cfg_attr(feature = "inline-more", inline)]
886882
pub fn drain(&mut self) -> Drain<'_, K, V, A> {
887883
Drain {
888-
inner: self.table.raw.drain(),
884+
inner: self.table.drain(),
889885
}
890886
}
891887

@@ -2196,8 +2192,7 @@ where
21962192
/// assert_eq!(iter.next(), None);
21972193
/// ```
21982194
pub struct Iter<'a, K, V> {
2199-
inner: RawIter<(K, V)>,
2200-
marker: PhantomData<(&'a K, &'a V)>,
2195+
inner: hash_table::Iter<'a, (K, V)>,
22012196
}
22022197

22032198
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
@@ -2206,7 +2201,6 @@ impl<K, V> Clone for Iter<'_, K, V> {
22062201
fn clone(&self) -> Self {
22072202
Iter {
22082203
inner: self.inner.clone(),
2209-
marker: PhantomData,
22102204
}
22112205
}
22122206
}
@@ -2245,7 +2239,7 @@ impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
22452239
/// assert_eq!(map.get(&2).unwrap(), &"Two Mississippi".to_owned());
22462240
/// ```
22472241
pub struct IterMut<'a, K, V> {
2248-
inner: RawIter<(K, V)>,
2242+
inner: hash_table::IterUnsafeMut<'a, (K, V)>,
22492243
// To ensure invariance with respect to V
22502244
marker: PhantomData<(&'a K, &'a mut V)>,
22512245
}
@@ -2260,8 +2254,7 @@ impl<K, V> IterMut<'_, K, V> {
22602254
#[cfg_attr(feature = "inline-more", inline)]
22612255
pub(super) fn iter(&self) -> Iter<'_, K, V> {
22622256
Iter {
2263-
inner: self.inner.clone(),
2264-
marker: PhantomData,
2257+
inner: self.inner.iter(),
22652258
}
22662259
}
22672260
}
@@ -2297,7 +2290,7 @@ impl<K, V> IterMut<'_, K, V> {
22972290
/// assert_eq!(iter.next(), None);
22982291
/// ```
22992292
pub struct IntoIter<K, V, A: Allocator = Global> {
2300-
inner: RawIntoIter<(K, V), A>,
2293+
inner: hash_table::IntoIter<(K, V), A>,
23012294
}
23022295

23032296
impl<K, V, A: Allocator> IntoIter<K, V, A> {
@@ -2306,7 +2299,6 @@ impl<K, V, A: Allocator> IntoIter<K, V, A> {
23062299
pub(super) fn iter(&self) -> Iter<'_, K, V> {
23072300
Iter {
23082301
inner: self.inner.iter(),
2309-
marker: PhantomData,
23102302
}
23112303
}
23122304
}
@@ -2593,7 +2585,7 @@ impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
25932585
/// assert_eq!(drain_iter.next(), None);
25942586
/// ```
25952587
pub struct Drain<'a, K, V, A: Allocator = Global> {
2596-
inner: RawDrain<'a, (K, V), A>,
2588+
inner: hash_table::Drain<'a, (K, V), A>,
25972589
}
25982590

25992591
impl<K, V, A: Allocator> Drain<'_, K, V, A> {
@@ -2602,7 +2594,6 @@ impl<K, V, A: Allocator> Drain<'_, K, V, A> {
26022594
pub(super) fn iter(&self) -> Iter<'_, K, V> {
26032595
Iter {
26042596
inner: self.inner.iter(),
2605-
marker: PhantomData,
26062597
}
26072598
}
26082599
}
@@ -3190,7 +3181,7 @@ impl<K, V, S, A: Allocator> IntoIterator for HashMap<K, V, S, A> {
31903181
#[cfg_attr(feature = "inline-more", inline)]
31913182
fn into_iter(self) -> IntoIter<K, V, A> {
31923183
IntoIter {
3193-
inner: self.table.raw.into_iter(),
3184+
inner: self.table.into_iter(),
31943185
}
31953186
}
31963187
}
@@ -3200,21 +3191,17 @@ impl<K, V> Default for Iter<'_, K, V> {
32003191
fn default() -> Self {
32013192
Self {
32023193
inner: Default::default(),
3203-
marker: PhantomData,
32043194
}
32053195
}
32063196
}
3207-
impl<'a, K, V> Iterator for Iter<'a, K, V> {
3197+
impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
32083198
type Item = (&'a K, &'a V);
32093199

32103200
#[cfg_attr(feature = "inline-more", inline)]
32113201
fn next(&mut self) -> Option<(&'a K, &'a V)> {
32123202
// Avoid `Option::map` because it bloats LLVM IR.
32133203
match self.inner.next() {
3214-
Some(x) => unsafe {
3215-
let r = x.as_ref();
3216-
Some((&r.0, &r.1))
3217-
},
3204+
Some(x) => Some((&x.0, &x.1)),
32183205
None => None,
32193206
}
32203207
}
@@ -3228,20 +3215,17 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
32283215
Self: Sized,
32293216
F: FnMut(B, Self::Item) -> B,
32303217
{
3231-
self.inner.fold(init, |acc, x| unsafe {
3232-
let (k, v) = x.as_ref();
3233-
f(acc, (k, v))
3234-
})
3218+
self.inner.fold(init, |acc, (k, v)| f(acc, (k, v)))
32353219
}
32363220
}
3237-
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
3221+
impl<'a, K: 'a, V: 'a> ExactSizeIterator for Iter<'a, K, V> {
32383222
#[cfg_attr(feature = "inline-more", inline)]
32393223
fn len(&self) -> usize {
32403224
self.inner.len()
32413225
}
32423226
}
32433227

3244-
impl<K, V> FusedIterator for Iter<'_, K, V> {}
3228+
impl<'a, K: 'a, V: 'a> FusedIterator for Iter<'a, K, V> {}
32453229

32463230
impl<K, V> Default for IterMut<'_, K, V> {
32473231
#[cfg_attr(feature = "inline-more", inline)]
@@ -3259,10 +3243,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
32593243
fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
32603244
// Avoid `Option::map` because it bloats LLVM IR.
32613245
match self.inner.next() {
3262-
Some(x) => unsafe {
3263-
let r = x.as_mut();
3264-
Some((&r.0, &mut r.1))
3265-
},
3246+
Some(x) => Some((&x.0, &mut x.1)),
32663247
None => None,
32673248
}
32683249
}
@@ -3276,10 +3257,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
32763257
Self: Sized,
32773258
F: FnMut(B, Self::Item) -> B,
32783259
{
3279-
self.inner.fold(init, |acc, x| unsafe {
3280-
let (k, v) = x.as_mut();
3281-
f(acc, (k, v))
3282-
})
3260+
self.inner.fold(init, |acc, (k, v)| f(acc, (k, v)))
32833261
}
32843262
}
32853263
impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {

0 commit comments

Comments
 (0)