Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make vec::Drain and binary_heap::Drain covariant #34951

Merged
merged 2 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use core::mem;
use core::ops::{Index, IndexMut};
use core::ops;
use core::ptr;
use core::ptr::Shared;
use core::slice;

use super::SpecExtend;
Expand Down Expand Up @@ -854,8 +855,8 @@ impl<T> Vec<T> {
Drain {
tail_start: end,
tail_len: len - end,
iter: range_slice.iter_mut(),
vec: self as *mut _,
iter: range_slice.iter(),
vec: Shared::new(self as *mut _),
}
}
}
Expand Down Expand Up @@ -1761,8 +1762,8 @@ pub struct Drain<'a, T: 'a> {
/// Length of tail
tail_len: usize,
/// Current remaining range to remove
iter: slice::IterMut<'a, T>,
vec: *mut Vec<T>,
iter: slice::Iter<'a, T>,
vec: Shared<Vec<T>>,
}

#[stable(feature = "drain", since = "1.6.0")]
Expand Down Expand Up @@ -1800,7 +1801,7 @@ impl<'a, T> Drop for Drain<'a, T> {

if self.tail_len > 0 {
unsafe {
let source_vec = &mut *self.vec;
let source_vec = &mut **self.vec;
// memmove back untouched tail, update to new length
let start = source_vec.len();
let tail = self.tail_start;
Expand Down
6 changes: 6 additions & 0 deletions src/libcollectionstest/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use std::collections::BinaryHeap;
use std::collections::binary_heap::Drain;

#[test]
fn test_iterator() {
Expand Down Expand Up @@ -292,3 +293,8 @@ fn test_extend_specialization() {

assert_eq!(a.into_sorted_vec(), [-20, -10, 1, 2, 3, 3, 5, 43]);
}

#[allow(dead_code)]
fn assert_covariance() {
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { d }
}
6 changes: 6 additions & 0 deletions src/libcollectionstest/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::borrow::Cow;
use std::iter::{FromIterator, repeat};
use std::mem::size_of;
use std::vec::Drain;

use test::Bencher;

Expand Down Expand Up @@ -510,6 +511,11 @@ fn test_cow_from() {
}
}

#[allow(dead_code)]
fn assert_covariance() {
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { d }
}

#[bench]
fn bench_new(b: &mut Bencher) {
b.iter(|| {
Expand Down