Skip to content

Commit 1639f2d

Browse files
committed
Fix the variances of HashMap and HashSet iterators
1 parent 285a40a commit 1639f2d

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/libstd/collections/hash/map.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use borrow::Borrow;
1515
use cmp::max;
1616
use fmt::{self, Debug};
1717
use hash::{Hash, SipHasher, BuildHasher};
18-
use iter::{self, Map, FromIterator};
18+
use iter::FromIterator;
1919
use mem::{self, replace};
2020
use ops::{Deref, Index};
2121
use rand::{self, Rng};
@@ -836,8 +836,7 @@ impl<K, V, S> HashMap<K, V, S>
836836
/// ```
837837
#[stable(feature = "rust1", since = "1.0.0")]
838838
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
839-
fn first<A, B>((a, _): (A, B)) -> A { a }
840-
Keys { inner: self.iter().map(first) }
839+
Keys { inner: self.iter() }
841840
}
842841

843842
/// An iterator visiting all values in arbitrary order.
@@ -859,8 +858,7 @@ impl<K, V, S> HashMap<K, V, S>
859858
/// ```
860859
#[stable(feature = "rust1", since = "1.0.0")]
861860
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
862-
fn second<A, B>((_, b): (A, B)) -> B { b }
863-
Values { inner: self.iter().map(second) }
861+
Values { inner: self.iter() }
864862
}
865863

866864
/// An iterator visiting all key-value pairs in arbitrary order.
@@ -992,9 +990,8 @@ impl<K, V, S> HashMap<K, V, S>
992990
#[inline]
993991
#[stable(feature = "drain", since = "1.6.0")]
994992
pub fn drain(&mut self) -> Drain<K, V> {
995-
fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
996993
Drain {
997-
inner: self.table.drain().map(last_two),
994+
inner: self.table.drain(),
998995
}
999996
}
1000997

@@ -1224,13 +1221,13 @@ pub struct IterMut<'a, K: 'a, V: 'a> {
12241221
/// HashMap move iterator.
12251222
#[stable(feature = "rust1", since = "1.0.0")]
12261223
pub struct IntoIter<K, V> {
1227-
inner: iter::Map<table::IntoIter<K, V>, fn((SafeHash, K, V)) -> (K, V)>
1224+
inner: table::IntoIter<K, V>
12281225
}
12291226

12301227
/// HashMap keys iterator.
12311228
#[stable(feature = "rust1", since = "1.0.0")]
12321229
pub struct Keys<'a, K: 'a, V: 'a> {
1233-
inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
1230+
inner: Iter<'a, K, V>
12341231
}
12351232

12361233
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
@@ -1246,7 +1243,7 @@ impl<'a, K, V> Clone for Keys<'a, K, V> {
12461243
/// HashMap values iterator.
12471244
#[stable(feature = "rust1", since = "1.0.0")]
12481245
pub struct Values<'a, K: 'a, V: 'a> {
1249-
inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
1246+
inner: Iter<'a, K, V>
12501247
}
12511248

12521249
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
@@ -1262,7 +1259,7 @@ impl<'a, K, V> Clone for Values<'a, K, V> {
12621259
/// HashMap drain iterator.
12631260
#[stable(feature = "drain", since = "1.6.0")]
12641261
pub struct Drain<'a, K: 'a, V: 'a> {
1265-
inner: iter::Map<table::Drain<'a, K, V>, fn((SafeHash, K, V)) -> (K, V)>
1262+
inner: table::Drain<'a, K, V>
12661263
}
12671264

12681265
enum InternalEntry<K, V, M> {
@@ -1397,9 +1394,8 @@ impl<K, V, S> IntoIterator for HashMap<K, V, S>
13971394
/// let vec: Vec<(&str, isize)> = map.into_iter().collect();
13981395
/// ```
13991396
fn into_iter(self) -> IntoIter<K, V> {
1400-
fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
14011397
IntoIter {
1402-
inner: self.table.into_iter().map(last_two)
1398+
inner: self.table.into_iter()
14031399
}
14041400
}
14051401
}
@@ -1432,7 +1428,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
14321428
impl<K, V> Iterator for IntoIter<K, V> {
14331429
type Item = (K, V);
14341430

1435-
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1431+
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next().map(|(_, k, v)| (k, v)) }
14361432
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
14371433
}
14381434
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1444,7 +1440,7 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
14441440
impl<'a, K, V> Iterator for Keys<'a, K, V> {
14451441
type Item = &'a K;
14461442

1447-
#[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1443+
#[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next().map(|(k, _)| k) }
14481444
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
14491445
}
14501446
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1456,7 +1452,7 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
14561452
impl<'a, K, V> Iterator for Values<'a, K, V> {
14571453
type Item = &'a V;
14581454

1459-
#[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1455+
#[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next().map(|(_, v)| v) }
14601456
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
14611457
}
14621458
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1468,7 +1464,7 @@ impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
14681464
impl<'a, K, V> Iterator for Drain<'a, K, V> {
14691465
type Item = (K, V);
14701466

1471-
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1467+
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next().map(|(_, k, v)| (k, v)) }
14721468
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
14731469
}
14741470
#[stable(feature = "rust1", since = "1.0.0")]

src/libstd/collections/hash/set.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use borrow::Borrow;
1212
use fmt;
1313
use hash::{Hash, BuildHasher};
14-
use iter::{Map, Chain, FromIterator};
14+
use iter::{Chain, FromIterator};
1515
use ops::{BitOr, BitAnd, BitXor, Sub};
1616

1717
use super::Recover;
@@ -414,8 +414,7 @@ impl<T, S> HashSet<T, S>
414414
#[inline]
415415
#[stable(feature = "drain", since = "1.6.0")]
416416
pub fn drain(&mut self) -> Drain<T> {
417-
fn first<A, B>((a, _): (A, B)) -> A { a }
418-
Drain { iter: self.map.drain().map(first) }
417+
Drain { iter: self.map.drain() }
419418
}
420419

421420
/// Clears the set, removing all values.
@@ -809,13 +808,13 @@ pub struct Iter<'a, K: 'a> {
809808
/// HashSet move iterator
810809
#[stable(feature = "rust1", since = "1.0.0")]
811810
pub struct IntoIter<K> {
812-
iter: Map<map::IntoIter<K, ()>, fn((K, ())) -> K>
811+
iter: map::IntoIter<K, ()>
813812
}
814813

815814
/// HashSet drain iterator
816815
#[stable(feature = "rust1", since = "1.0.0")]
817816
pub struct Drain<'a, K: 'a> {
818-
iter: Map<map::Drain<'a, K, ()>, fn((K, ())) -> K>,
817+
iter: map::Drain<'a, K, ()>,
819818
}
820819

821820
/// Intersection iterator
@@ -889,8 +888,7 @@ impl<T, S> IntoIterator for HashSet<T, S>
889888
/// }
890889
/// ```
891890
fn into_iter(self) -> IntoIter<T> {
892-
fn first<A, B>((a, _): (A, B)) -> A { a }
893-
IntoIter { iter: self.map.into_iter().map(first) }
891+
IntoIter { iter: self.map.into_iter() }
894892
}
895893
}
896894

@@ -914,7 +912,7 @@ impl<'a, K> ExactSizeIterator for Iter<'a, K> {
914912
impl<K> Iterator for IntoIter<K> {
915913
type Item = K;
916914

917-
fn next(&mut self) -> Option<K> { self.iter.next() }
915+
fn next(&mut self) -> Option<K> { self.iter.next().map(|(k, _)| k) }
918916
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
919917
}
920918
#[stable(feature = "rust1", since = "1.0.0")]
@@ -926,7 +924,7 @@ impl<K> ExactSizeIterator for IntoIter<K> {
926924
impl<'a, K> Iterator for Drain<'a, K> {
927925
type Item = K;
928926

929-
fn next(&mut self) -> Option<K> { self.iter.next() }
927+
fn next(&mut self) -> Option<K> { self.iter.next().map(|(k, _)| k) }
930928
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
931929
}
932930
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)