From 589108baf644ea44a10a7258d67da254e1e09fae Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Wed, 30 Mar 2016 21:57:31 -0500 Subject: Test that HashMap, HashSet, and their iterators are properly covariant --- src/libstd/collections/hash/map.rs | 14 ++++++++++++++ src/libstd/collections/hash/set.rs | 15 +++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 77ce27476fd..80b5448800e 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1670,6 +1670,20 @@ impl super::Recover for HashMap } } +#[allow(dead_code)] +fn assert_covariance() { + fn map_key<'new>(v: HashMap<&'static str, u8>) -> HashMap<&'new str, u8> { v } + fn map_val<'new>(v: HashMap) -> HashMap { v } + fn iter_key<'a, 'new>(v: Iter<'a, &'static str, u8>) -> Iter<'a, &'new str, u8> { v } + fn iter_val<'a, 'new>(v: Iter<'a, u8, &'static str>) -> Iter<'a, u8, &'new str> { v } + fn into_iter_key<'new>(v: IntoIter<&'static str, u8>) -> IntoIter<&'new str, u8> { v } + fn into_iter_val<'new>(v: IntoIter) -> IntoIter { v } + fn keys_key<'a, 'new>(v: Keys<'a, &'static str, u8>) -> Keys<'a, &'new str, u8> { v } + fn keys_val<'a, 'new>(v: Keys<'a, u8, &'static str>) -> Keys<'a, u8, &'new str> { v } + fn values_key<'a, 'new>(v: Values<'a, &'static str, u8>) -> Values<'a, &'new str, u8> { v } + fn values_val<'a, 'new>(v: Values<'a, u8, &'static str>) -> Values<'a, u8, &'new str> { v } +} + #[cfg(test)] mod test_map { use prelude::v1::*; diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 7e79aef300b..954adf313be 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -1024,6 +1024,21 @@ impl<'a, T, S> Iterator for Union<'a, T, S> fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } +#[allow(dead_code)] +fn assert_covariance() { + fn set<'new>(v: HashSet<&'static str>) -> HashSet<&'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 difference<'a, 'new>(v: Difference<'a, &'static str, RandomState>) + -> Difference<'a, &'new str, RandomState> { v } + fn symmetric_difference<'a, 'new>(v: SymmetricDifference<'a, &'static str, RandomState>) + -> SymmetricDifference<'a, &'new str, RandomState> { v } + fn intersection<'a, 'new>(v: Intersection<'a, &'static str, RandomState>) + -> Intersection<'a, &'new str, RandomState> { v } + fn union<'a, 'new>(v: Union<'a, &'static str, RandomState>) + -> Union<'a, &'new str, RandomState> { v } +} + #[cfg(test)] mod test_set { use prelude::v1::*; -- cgit 1.4.1-3-g733a5