From 7e148cd06292d125f925323fda6fabbc625a42c8 Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Tue, 16 Aug 2016 20:45:07 -0400 Subject: [PATCH] Make `vec::IntoIter` covariant again Closes #35721 --- src/libcollections/vec.rs | 15 +++++++++------ src/libcollectionstest/vec.rs | 3 ++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index a6f817a89624c..3aefcc7d4cfa4 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1453,10 +1453,11 @@ impl IntoIterator for Vec { } else { begin.offset(self.len() as isize) as *const T }; - let buf = ptr::read(&self.buf); + let cap = self.buf.cap(); mem::forget(self); IntoIter { - _buf: buf, + buf: Shared::new(begin), + cap: cap, ptr: begin, end: end, } @@ -1708,8 +1709,9 @@ impl<'a, T> FromIterator for Cow<'a, [T]> where T: Clone { /// [`IntoIterator`]: ../../std/iter/trait.IntoIterator.html #[stable(feature = "rust1", since = "1.0.0")] pub struct IntoIter { - _buf: RawVec, - ptr: *mut T, + buf: Shared, + cap: usize, + ptr: *const T, end: *const T, } @@ -1750,7 +1752,7 @@ impl IntoIter { #[unstable(feature = "vec_into_iter_as_slice", issue = "35601")] pub fn as_mut_slice(&self) -> &mut [T] { unsafe { - slice::from_raw_parts_mut(self.ptr, self.len()) + slice::from_raw_parts_mut(self.ptr as *mut T, self.len()) } } } @@ -1846,9 +1848,10 @@ impl Drop for IntoIter { #[unsafe_destructor_blind_to_params] fn drop(&mut self) { // destroy the remaining elements - for _x in self {} + for _x in self.by_ref() {} // RawVec handles deallocation + let _ = unsafe { RawVec::from_raw_parts(*self.buf, self.cap) }; } } diff --git a/src/libcollectionstest/vec.rs b/src/libcollectionstest/vec.rs index 9556174bd2294..537fabf8ab69b 100644 --- a/src/libcollectionstest/vec.rs +++ b/src/libcollectionstest/vec.rs @@ -11,7 +11,7 @@ use std::borrow::Cow; use std::iter::{FromIterator, repeat}; use std::mem::size_of; -use std::vec::Drain; +use std::vec::{Drain, IntoIter}; use test::Bencher; @@ -537,6 +537,7 @@ fn test_cow_from() { #[allow(dead_code)] fn assert_covariance() { fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { d } + fn into_iter<'new>(i: IntoIter<&'static str>) -> IntoIter<&'new str> { i } } #[bench]