From 686be822efd61ec1dc7668bfc18f53dd7b02519c Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Mon, 18 Jan 2016 07:36:39 -0500 Subject: [PATCH] Make `btree_set::{IntoIter, Iter, Range}` covariant CC #30642 --- src/libcollections/btree/set.rs | 28 +++++++++------------------- src/libcollectionstest/btree/set.rs | 10 ++++++++++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index c1381dde762e5..91fc8d8217f53 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -14,7 +14,7 @@ use core::cmp::Ordering::{self, Less, Greater, Equal}; use core::fmt::Debug; use core::fmt; -use core::iter::{Peekable, Map, FromIterator}; +use core::iter::{Peekable, FromIterator}; use core::ops::{BitOr, BitAnd, BitXor, Sub}; use borrow::Borrow; @@ -52,12 +52,12 @@ pub struct Iter<'a, T: 'a> { /// An owning iterator over a BTreeSet's items. #[stable(feature = "rust1", since = "1.0.0")] pub struct IntoIter { - iter: Map<::btree_map::IntoIter, fn((T, ())) -> T>, + iter: ::btree_map::IntoIter, } /// An iterator over a sub-range of BTreeSet's items. pub struct Range<'a, T: 'a> { - iter: Map<::btree_map::Range<'a, T, ()>, fn((&'a T, &'a ())) -> &'a T>, + iter: ::btree_map::Range<'a, T, ()>, } /// A lazy iterator producing elements in the set difference (in-order). @@ -160,12 +160,7 @@ impl BTreeSet { -> Range<'a, T> where T: Borrow + Borrow { - fn first((a, _): (A, B)) -> A { - a - } - let first: fn((&'a T, &'a ())) -> &'a T = first; // coerce to fn pointer - - Range { iter: self.map.range(min, max).map(first) } + Range { iter: self.map.range(min, max) } } } @@ -548,12 +543,7 @@ impl IntoIterator for BTreeSet { /// assert_eq!(v, [1, 2, 3, 4]); /// ``` fn into_iter(self) -> IntoIter { - fn first((a, _): (A, B)) -> A { - a - } - let first: fn((T, ())) -> T = first; // coerce to fn pointer - - IntoIter { iter: self.map.into_iter().map(first) } + IntoIter { iter: self.map.into_iter() } } } @@ -721,7 +711,7 @@ impl Iterator for IntoIter { type Item = T; fn next(&mut self) -> Option { - self.iter.next() + self.iter.next().map(|(k, _)| k) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() @@ -730,7 +720,7 @@ impl Iterator for IntoIter { #[stable(feature = "rust1", since = "1.0.0")] impl DoubleEndedIterator for IntoIter { fn next_back(&mut self) -> Option { - self.iter.next_back() + self.iter.next_back().map(|(k, _)| k) } } #[stable(feature = "rust1", since = "1.0.0")] @@ -746,12 +736,12 @@ impl<'a, T> Iterator for Range<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { - self.iter.next() + self.iter.next().map(|(k, _)| k) } } impl<'a, T> DoubleEndedIterator for Range<'a, T> { fn next_back(&mut self) -> Option<&'a T> { - self.iter.next_back() + self.iter.next_back().map(|(k, _)| k) } } diff --git a/src/libcollectionstest/btree/set.rs b/src/libcollectionstest/btree/set.rs index 8fcfe97f42afc..fee183433285e 100644 --- a/src/libcollectionstest/btree/set.rs +++ b/src/libcollectionstest/btree/set.rs @@ -254,3 +254,13 @@ fn test_recovery() { assert_eq!(s.iter().next(), None); } + +#[test] +fn test_variance() { + use std::collections::btree_set::{IntoIter, Iter, Range}; + + fn set<'new>(v: BTreeSet<&'static str>) -> BTreeSet<&'new str> { v } + fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> { v } + fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> { v } + fn range<'a, 'new>(v: Range<'a, &'static str>) -> Range<'a, &'new str> { v } +}