Skip to content

Commit cec262e

Browse files
authoredJul 28, 2016
Auto merge of #34951 - tomgarcia:covariant-vec, r=brson
Make vec::Drain and binary_heap::Drain covariant I removed all mutable pointers/references, and added covariance tests similar to the ones in #32635. It builds and passes the tests, but I noticed that there weren't any tests of Drain's behaviour (at least not in libcollectionstest), so I'm not sure if my changes accidently broke Drain's behaviour. Should I add some tests for that (and if so, what should the tests include)?
2 parents 1895bf7 + d1e2a93 commit cec262e

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed
 

‎src/libcollections/vec.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ use core::mem;
7373
use core::ops::{Index, IndexMut};
7474
use core::ops;
7575
use core::ptr;
76+
use core::ptr::Shared;
7677
use core::slice;
7778

7879
use super::SpecExtend;
@@ -899,8 +900,8 @@ impl<T> Vec<T> {
899900
Drain {
900901
tail_start: end,
901902
tail_len: len - end,
902-
iter: range_slice.iter_mut(),
903-
vec: self as *mut _,
903+
iter: range_slice.iter(),
904+
vec: Shared::new(self as *mut _),
904905
}
905906
}
906907
}
@@ -1806,8 +1807,8 @@ pub struct Drain<'a, T: 'a> {
18061807
/// Length of tail
18071808
tail_len: usize,
18081809
/// Current remaining range to remove
1809-
iter: slice::IterMut<'a, T>,
1810-
vec: *mut Vec<T>,
1810+
iter: slice::Iter<'a, T>,
1811+
vec: Shared<Vec<T>>,
18111812
}
18121813

18131814
#[stable(feature = "drain", since = "1.6.0")]
@@ -1845,7 +1846,7 @@ impl<'a, T> Drop for Drain<'a, T> {
18451846

18461847
if self.tail_len > 0 {
18471848
unsafe {
1848-
let source_vec = &mut *self.vec;
1849+
let source_vec = &mut **self.vec;
18491850
// memmove back untouched tail, update to new length
18501851
let start = source_vec.len();
18511852
let tail = self.tail_start;

‎src/libcollectionstest/binary_heap.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use std::collections::BinaryHeap;
12+
use std::collections::binary_heap::Drain;
1213

1314
#[test]
1415
fn test_iterator() {
@@ -292,3 +293,8 @@ fn test_extend_specialization() {
292293

293294
assert_eq!(a.into_sorted_vec(), [-20, -10, 1, 2, 3, 3, 5, 43]);
294295
}
296+
297+
#[allow(dead_code)]
298+
fn assert_covariance() {
299+
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { d }
300+
}

‎src/libcollectionstest/vec.rs

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use std::borrow::Cow;
1212
use std::iter::{FromIterator, repeat};
1313
use std::mem::size_of;
14+
use std::vec::Drain;
1415

1516
use test::Bencher;
1617

@@ -510,6 +511,11 @@ fn test_cow_from() {
510511
}
511512
}
512513

514+
#[allow(dead_code)]
515+
fn assert_covariance() {
516+
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { d }
517+
}
518+
513519
#[bench]
514520
fn bench_new(b: &mut Bencher) {
515521
b.iter(|| {

0 commit comments

Comments
 (0)
Please sign in to comment.