Skip to content

Commit a253a36

Browse files
committed
Assert covariance of BTree{Map,Set} and associated iterators
1 parent a59e885 commit a253a36

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

library/alloc/src/collections/btree/map.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(test)]
2+
mod tests;
3+
14
use core::borrow::Borrow;
25
use core::cmp::Ordering;
36
use core::fmt::{self, Debug};
@@ -2220,5 +2223,52 @@ impl<K, V> BTreeMap<K, V> {
22202223
}
22212224
}
22222225

2223-
#[cfg(test)]
2224-
mod tests;
2226+
#[allow(dead_code)]
2227+
fn assert_covariance() {
2228+
// Lifetime and both type parameters should be covariant for these types.
2229+
fn map<'new>(v: BTreeMap<&'static str, &'static str>) -> BTreeMap<&'new str, &'new str> {
2230+
v
2231+
}
2232+
fn iter<'new>(
2233+
v: Iter<'static, &'static str, &'static str>,
2234+
) -> Iter<'new, &'new str, &'new str> {
2235+
v
2236+
}
2237+
fn into_iter<'new>(v: IntoIter<&'static str, &'static str>) -> IntoIter<&'new str, &'new str> {
2238+
v
2239+
}
2240+
fn keys<'new>(
2241+
v: Keys<'static, &'static str, &'static str>,
2242+
) -> Keys<'new, &'new str, &'new str> {
2243+
v
2244+
}
2245+
fn values<'new>(
2246+
v: Values<'static, &'static str, &'static str>,
2247+
) -> Values<'new, &'new str, &'new str> {
2248+
v
2249+
}
2250+
fn into_keys<'new>(v: IntoKeys<&'static str, &'static str>) -> IntoKeys<&'new str, &'new str> {
2251+
v
2252+
}
2253+
fn into_values<'new>(
2254+
v: IntoValues<&'static str, &'static str>,
2255+
) -> IntoValues<&'new str, &'new str> {
2256+
v
2257+
}
2258+
fn range<'new>(
2259+
v: Range<'static, &'static str, &'static str>,
2260+
) -> Range<'new, &'new str, &'new str> {
2261+
v
2262+
}
2263+
2264+
// Lifetime should be covariant for these types.
2265+
fn iter_mut<'new>(v: IterMut<'static, u8, u8>) -> IterMut<'new, u8, u8> {
2266+
v
2267+
}
2268+
fn values_mut<'new>(v: ValuesMut<'static, u8, u8>) -> ValuesMut<'new, u8, u8> {
2269+
v
2270+
}
2271+
fn range_mut<'new>(v: RangeMut<'static, u8, u8>) -> RangeMut<'new, u8, u8> {
2272+
v
2273+
}
2274+
}

library/alloc/src/collections/btree/set.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// This is pretty much entirely stolen from TreeSet, since BTreeMap has an identical interface
22
// to TreeMap
33

4+
#[cfg(test)]
5+
mod tests;
6+
47
use core::borrow::Borrow;
58
use core::cmp::Ordering::{Equal, Greater, Less};
69
use core::cmp::{max, min};
@@ -1601,5 +1604,18 @@ impl<'a, T: Ord> Iterator for Union<'a, T> {
16011604
#[stable(feature = "fused", since = "1.26.0")]
16021605
impl<T: Ord> FusedIterator for Union<'_, T> {}
16031606

1604-
#[cfg(test)]
1605-
mod tests;
1607+
#[allow(dead_code)]
1608+
fn assert_covariance() {
1609+
fn set<'new>(v: BTreeSet<&'static str>) -> BTreeSet<&'new str> {
1610+
v
1611+
}
1612+
fn iter<'new>(v: Iter<'static, &'static str>) -> Iter<'new, &'new str> {
1613+
v
1614+
}
1615+
fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> {
1616+
v
1617+
}
1618+
fn range<'new>(v: Range<'static, &'static str>) -> Range<'new, &'new str> {
1619+
v
1620+
}
1621+
}

0 commit comments

Comments
 (0)