diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index 4ffb435d1e366..dfa0227dea3b8 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -2,6 +2,8 @@ use core::iter::FusedIterator; use core::ptr::{self, NonNull}; use core::{fmt, mem}; +use crate::alloc::{Allocator, Global}; + use super::{count, Iter, VecDeque}; /// A draining iterator over the elements of a `VecDeque`. @@ -11,15 +13,19 @@ use super::{count, Iter, VecDeque}; /// /// [`drain`]: VecDeque::drain #[stable(feature = "drain", since = "1.6.0")] -pub struct Drain<'a, T: 'a> { +pub struct Drain< + 'a, + T: 'a, + #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, +> { pub(crate) after_tail: usize, pub(crate) after_head: usize, pub(crate) iter: Iter<'a, T>, - pub(crate) deque: NonNull>, + pub(crate) deque: NonNull>, } #[stable(feature = "collection_debug", since = "1.17.0")] -impl fmt::Debug for Drain<'_, T> { +impl fmt::Debug for Drain<'_, T, A> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain") .field(&self.after_tail) @@ -30,16 +36,16 @@ impl fmt::Debug for Drain<'_, T> { } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl Sync for Drain<'_, T> {} +unsafe impl Sync for Drain<'_, T, A> {} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl Send for Drain<'_, T> {} +unsafe impl Send for Drain<'_, T, A> {} #[stable(feature = "drain", since = "1.6.0")] -impl Drop for Drain<'_, T> { +impl Drop for Drain<'_, T, A> { fn drop(&mut self) { - struct DropGuard<'r, 'a, T>(&'r mut Drain<'a, T>); + struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>); - impl<'r, 'a, T> Drop for DropGuard<'r, 'a, T> { + impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> { fn drop(&mut self) { self.0.for_each(drop); @@ -96,7 +102,7 @@ impl Drop for Drain<'_, T> { } #[stable(feature = "drain", since = "1.6.0")] -impl Iterator for Drain<'_, T> { +impl Iterator for Drain<'_, T, A> { type Item = T; #[inline] @@ -111,7 +117,7 @@ impl Iterator for Drain<'_, T> { } #[stable(feature = "drain", since = "1.6.0")] -impl DoubleEndedIterator for Drain<'_, T> { +impl DoubleEndedIterator for Drain<'_, T, A> { #[inline] fn next_back(&mut self) -> Option { self.iter.next_back().map(|elt| unsafe { ptr::read(elt) }) @@ -119,7 +125,7 @@ impl DoubleEndedIterator for Drain<'_, T> { } #[stable(feature = "drain", since = "1.6.0")] -impl ExactSizeIterator for Drain<'_, T> {} +impl ExactSizeIterator for Drain<'_, T, A> {} #[stable(feature = "fused", since = "1.26.0")] -impl FusedIterator for Drain<'_, T> {} +impl FusedIterator for Drain<'_, T, A> {} diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index 612f7e6eb4da8..5f13c3bf30387 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -1,6 +1,8 @@ use core::fmt; use core::iter::{FusedIterator, TrustedLen}; +use crate::alloc::{Allocator, Global}; + use super::VecDeque; /// An owning iterator over the elements of a `VecDeque`. @@ -11,19 +13,22 @@ use super::VecDeque; /// [`into_iter`]: VecDeque::into_iter #[derive(Clone)] #[stable(feature = "rust1", since = "1.0.0")] -pub struct IntoIter { - pub(crate) inner: VecDeque, +pub struct IntoIter< + T, + #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, +> { + pub(crate) inner: VecDeque, } #[stable(feature = "collection_debug", since = "1.17.0")] -impl fmt::Debug for IntoIter { +impl fmt::Debug for IntoIter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.inner).finish() } } #[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for IntoIter { +impl Iterator for IntoIter { type Item = T; #[inline] @@ -39,7 +44,7 @@ impl Iterator for IntoIter { } #[stable(feature = "rust1", since = "1.0.0")] -impl DoubleEndedIterator for IntoIter { +impl DoubleEndedIterator for IntoIter { #[inline] fn next_back(&mut self) -> Option { self.inner.pop_back() @@ -47,14 +52,14 @@ impl DoubleEndedIterator for IntoIter { } #[stable(feature = "rust1", since = "1.0.0")] -impl ExactSizeIterator for IntoIter { +impl ExactSizeIterator for IntoIter { fn is_empty(&self) -> bool { self.inner.is_empty() } } #[stable(feature = "fused", since = "1.26.0")] -impl FusedIterator for IntoIter {} +impl FusedIterator for IntoIter {} #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl TrustedLen for IntoIter {} +unsafe impl TrustedLen for IntoIter {} diff --git a/library/alloc/src/collections/vec_deque/macros.rs b/library/alloc/src/collections/vec_deque/macros.rs index 0d59d312cf406..5c7913073fe87 100644 --- a/library/alloc/src/collections/vec_deque/macros.rs +++ b/library/alloc/src/collections/vec_deque/macros.rs @@ -1,9 +1,9 @@ macro_rules! __impl_slice_eq1 { ([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => { #[stable(feature = "vec_deque_partial_eq_slice", since = "1.17.0")] - impl PartialEq<$rhs> for $lhs + impl PartialEq<$rhs> for $lhs where - A: PartialEq, + T: PartialEq, $($constraints)* { fn eq(&self, other: &$rhs) -> bool { diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 461e701be054e..677c27b815005 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -17,6 +17,7 @@ use core::ops::{Index, IndexMut, Range, RangeBounds}; use core::ptr::{self, NonNull}; use core::slice; +use crate::alloc::{Allocator, Global}; use crate::collections::TryReserveError; use crate::raw_vec::RawVec; use crate::vec::Vec; @@ -80,7 +81,10 @@ const MAXIMUM_ZST_CAPACITY: usize = 1 << (usize::BITS - 1); // Largest possible /// [`make_contiguous`]: VecDeque::make_contiguous #[cfg_attr(not(test), rustc_diagnostic_item = "vecdeque_type")] #[stable(feature = "rust1", since = "1.0.0")] -pub struct VecDeque { +pub struct VecDeque< + T, + #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, +> { // tail and head are pointers into the buffer. Tail always points // to the first element that could be read, Head always points // to where data should be written. @@ -88,13 +92,15 @@ pub struct VecDeque { // is defined as the distance between the two. tail: usize, head: usize, - buf: RawVec, + buf: RawVec, } #[stable(feature = "rust1", since = "1.0.0")] -impl Clone for VecDeque { - fn clone(&self) -> VecDeque { - self.iter().cloned().collect() +impl Clone for VecDeque { + fn clone(&self) -> Self { + let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone()); + deq.extend(self.iter().cloned()); + deq } fn clone_from(&mut self, other: &Self) { @@ -114,7 +120,7 @@ impl Clone for VecDeque { } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T> Drop for VecDeque { +unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque { fn drop(&mut self) { /// Runs the destructor for all items in the slice when it gets dropped (normally or /// during unwinding). @@ -147,7 +153,7 @@ impl Default for VecDeque { } } -impl VecDeque { +impl VecDeque { /// Marginally more convenient #[inline] fn ptr(&self) -> *mut T { @@ -457,9 +463,10 @@ impl VecDeque { /// /// let vector: VecDeque = VecDeque::new(); /// ``` + #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn new() -> VecDeque { - VecDeque::with_capacity(INITIAL_CAPACITY) + VecDeque::new_in(Global) } /// Creates an empty `VecDeque` with space for at least `capacity` elements. @@ -471,13 +478,45 @@ impl VecDeque { /// /// let vector: VecDeque = VecDeque::with_capacity(10); /// ``` + #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn with_capacity(capacity: usize) -> VecDeque { + Self::with_capacity_in(capacity, Global) + } +} + +impl VecDeque { + /// Creates an empty `VecDeque`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::VecDeque; + /// + /// let vector: VecDeque = VecDeque::new(); + /// ``` + #[inline] + #[unstable(feature = "allocator_api", issue = "32838")] + pub fn new_in(alloc: A) -> VecDeque { + VecDeque::with_capacity_in(INITIAL_CAPACITY, alloc) + } + + /// Creates an empty `VecDeque` with space for at least `capacity` elements. + /// + /// # Examples + /// + /// ``` + /// use std::collections::VecDeque; + /// + /// let vector: VecDeque = VecDeque::with_capacity(10); + /// ``` + #[unstable(feature = "allocator_api", issue = "32838")] + pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque { // +1 since the ringbuffer always leaves one space empty let cap = cmp::max(capacity + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); assert!(cap > capacity, "capacity overflow"); - VecDeque { tail: 0, head: 0, buf: RawVec::with_capacity(cap) } + VecDeque { tail: 0, head: 0, buf: RawVec::with_capacity_in(cap, alloc) } } /// Provides a reference to the element at the given index. @@ -904,6 +943,13 @@ impl VecDeque { } } + /// Returns a reference to the underlying allocator. + #[unstable(feature = "allocator_api", issue = "32838")] + #[inline] + pub fn allocator(&self) -> &A { + self.buf.allocator() + } + /// Returns a front-to-back iterator. /// /// # Examples @@ -1176,7 +1222,7 @@ impl VecDeque { /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] - pub fn drain(&mut self, range: R) -> Drain<'_, T> + pub fn drain(&mut self, range: R) -> Drain<'_, T, A> where R: RangeBounds, { @@ -1965,12 +2011,15 @@ impl VecDeque { #[inline] #[must_use = "use `.truncate()` if you don't need the other half"] #[stable(feature = "split_off", since = "1.4.0")] - pub fn split_off(&mut self, at: usize) -> Self { + pub fn split_off(&mut self, at: usize) -> Self + where + A: Clone, + { let len = self.len(); assert!(at <= len, "`at` out of bounds"); let other_len = len - at; - let mut other = VecDeque::with_capacity(other_len); + let mut other = VecDeque::with_capacity_in(other_len, self.allocator().clone()); unsafe { let (first_half, second_half) = self.as_slices(); @@ -2593,7 +2642,7 @@ impl VecDeque { } } -impl VecDeque { +impl VecDeque { /// Modifies the `VecDeque` in-place so that `len()` is equal to new_len, /// either by removing excess elements from the back or by appending clones of `value` /// to the back. @@ -2637,8 +2686,8 @@ fn count(tail: usize, head: usize, size: usize) -> usize { } #[stable(feature = "rust1", since = "1.0.0")] -impl PartialEq for VecDeque { - fn eq(&self, other: &VecDeque) -> bool { +impl PartialEq for VecDeque { + fn eq(&self, other: &Self) -> bool { if self.len() != other.len() { return false; } @@ -2676,32 +2725,32 @@ impl PartialEq for VecDeque { } #[stable(feature = "rust1", since = "1.0.0")] -impl Eq for VecDeque {} +impl Eq for VecDeque {} -__impl_slice_eq1! { [] VecDeque, Vec, } -__impl_slice_eq1! { [] VecDeque, &[B], } -__impl_slice_eq1! { [] VecDeque, &mut [B], } -__impl_slice_eq1! { [const N: usize] VecDeque, [B; N], } -__impl_slice_eq1! { [const N: usize] VecDeque, &[B; N], } -__impl_slice_eq1! { [const N: usize] VecDeque, &mut [B; N], } +__impl_slice_eq1! { [] VecDeque, Vec, } +__impl_slice_eq1! { [] VecDeque, &[U], } +__impl_slice_eq1! { [] VecDeque, &mut [U], } +__impl_slice_eq1! { [const N: usize] VecDeque, [U; N], } +__impl_slice_eq1! { [const N: usize] VecDeque, &[U; N], } +__impl_slice_eq1! { [const N: usize] VecDeque, &mut [U; N], } #[stable(feature = "rust1", since = "1.0.0")] -impl PartialOrd for VecDeque { - fn partial_cmp(&self, other: &VecDeque) -> Option { +impl PartialOrd for VecDeque { + fn partial_cmp(&self, other: &Self) -> Option { self.iter().partial_cmp(other.iter()) } } #[stable(feature = "rust1", since = "1.0.0")] -impl Ord for VecDeque { +impl Ord for VecDeque { #[inline] - fn cmp(&self, other: &VecDeque) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { self.iter().cmp(other.iter()) } } #[stable(feature = "rust1", since = "1.0.0")] -impl Hash for VecDeque { +impl Hash for VecDeque { fn hash(&self, state: &mut H) { self.len().hash(state); // It's not possible to use Hash::hash_slice on slices @@ -2715,26 +2764,26 @@ impl Hash for VecDeque { } #[stable(feature = "rust1", since = "1.0.0")] -impl Index for VecDeque { - type Output = A; +impl Index for VecDeque { + type Output = T; #[inline] - fn index(&self, index: usize) -> &A { + fn index(&self, index: usize) -> &T { self.get(index).expect("Out of bounds access") } } #[stable(feature = "rust1", since = "1.0.0")] -impl IndexMut for VecDeque { +impl IndexMut for VecDeque { #[inline] - fn index_mut(&mut self, index: usize) -> &mut A { + fn index_mut(&mut self, index: usize) -> &mut T { self.get_mut(index).expect("Out of bounds access") } } #[stable(feature = "rust1", since = "1.0.0")] -impl FromIterator for VecDeque { - fn from_iter>(iter: T) -> VecDeque { +impl FromIterator for VecDeque { + fn from_iter>(iter: I) -> VecDeque { let iterator = iter.into_iter(); let (lower, _) = iterator.size_hint(); let mut deq = VecDeque::with_capacity(lower); @@ -2744,19 +2793,19 @@ impl FromIterator for VecDeque { } #[stable(feature = "rust1", since = "1.0.0")] -impl IntoIterator for VecDeque { +impl IntoIterator for VecDeque { type Item = T; - type IntoIter = IntoIter; + type IntoIter = IntoIter; /// Consumes the `VecDeque` into a front-to-back iterator yielding elements by /// value. - fn into_iter(self) -> IntoIter { + fn into_iter(self) -> IntoIter { IntoIter { inner: self } } } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T> IntoIterator for &'a VecDeque { +impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -2766,7 +2815,7 @@ impl<'a, T> IntoIterator for &'a VecDeque { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T> IntoIterator for &'a mut VecDeque { +impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; @@ -2776,8 +2825,8 @@ impl<'a, T> IntoIterator for &'a mut VecDeque { } #[stable(feature = "rust1", since = "1.0.0")] -impl Extend for VecDeque { - fn extend>(&mut self, iter: T) { +impl Extend for VecDeque { + fn extend>(&mut self, iter: I) { // This function should be the moral equivalent of: // // for item in iter.into_iter() { @@ -2799,7 +2848,7 @@ impl Extend for VecDeque { } #[inline] - fn extend_one(&mut self, elem: A) { + fn extend_one(&mut self, elem: T) { self.push_back(elem); } @@ -2810,7 +2859,7 @@ impl Extend for VecDeque { } #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a, T: 'a + Copy> Extend<&'a T> for VecDeque { +impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque { fn extend>(&mut self, iter: I) { self.extend(iter.into_iter().cloned()); } @@ -2827,14 +2876,14 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for VecDeque { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Debug for VecDeque { +impl fmt::Debug for VecDeque { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self).finish() } } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl From> for VecDeque { +impl From> for VecDeque { /// Turn a [`Vec`] into a [`VecDeque`]. /// /// [`Vec`]: crate::vec::Vec @@ -2843,7 +2892,7 @@ impl From> for VecDeque { /// This avoids reallocating where possible, but the conditions for that are /// strict, and subject to change, and so shouldn't be relied upon unless the /// `Vec` came from `From>` and hasn't been reallocated. - fn from(mut other: Vec) -> Self { + fn from(mut other: Vec) -> Self { let len = other.len(); if mem::size_of::() == 0 { // There's no actual allocation for ZSTs to worry about capacity, @@ -2861,15 +2910,15 @@ impl From> for VecDeque { } unsafe { - let (other_buf, len, capacity) = other.into_raw_parts(); - let buf = RawVec::from_raw_parts(other_buf, capacity); + let (other_buf, len, capacity, alloc) = other.into_raw_parts_with_alloc(); + let buf = RawVec::from_raw_parts_in(other_buf, capacity, alloc); VecDeque { tail: 0, head: len, buf } } } } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl From> for Vec { +impl From> for Vec { /// Turn a [`VecDeque`] into a [`Vec`]. /// /// [`Vec`]: crate::vec::Vec @@ -2899,7 +2948,7 @@ impl From> for Vec { /// assert_eq!(vec, [8, 9, 1, 2, 3, 4]); /// assert_eq!(vec.as_ptr(), ptr); /// ``` - fn from(mut other: VecDeque) -> Self { + fn from(mut other: VecDeque) -> Self { other.make_contiguous(); unsafe { @@ -2907,11 +2956,12 @@ impl From> for Vec { let buf = other.buf.ptr(); let len = other.len(); let cap = other.cap(); + let alloc = ptr::read(other.allocator()); if other.tail != 0 { ptr::copy(buf.add(other.tail), buf, len); } - Vec::from_raw_parts(buf, len, cap) + Vec::from_raw_parts_in(buf, len, cap, alloc) } } } diff --git a/library/alloc/src/collections/vec_deque/pair_slices.rs b/library/alloc/src/collections/vec_deque/pair_slices.rs index 7b87090fb0713..8e3ac9cfd1d87 100644 --- a/library/alloc/src/collections/vec_deque/pair_slices.rs +++ b/library/alloc/src/collections/vec_deque/pair_slices.rs @@ -1,6 +1,8 @@ use core::cmp::{self}; use core::mem::replace; +use crate::alloc::Allocator; + use super::VecDeque; /// PairSlices pairs up equal length slice parts of two deques @@ -25,7 +27,7 @@ pub struct PairSlices<'a, 'b, T> { } impl<'a, 'b, T> PairSlices<'a, 'b, T> { - pub fn from(to: &'a mut VecDeque, from: &'b VecDeque) -> Self { + pub fn from(to: &'a mut VecDeque, from: &'b VecDeque) -> Self { let (a0, a1) = to.as_mut_slices(); let (b0, b1) = from.as_slices(); PairSlices { a0, a1, b0, b1 } diff --git a/src/test/debuginfo/pretty-std.rs b/src/test/debuginfo/pretty-std.rs index a190a29eec2a5..baeada69d1c9a 100644 --- a/src/test/debuginfo/pretty-std.rs +++ b/src/test/debuginfo/pretty-std.rs @@ -137,8 +137,8 @@ // cdb-check: [0x1] : 42 [Type: int] // cdb-command: dx vecdeque -// cdb-check:vecdeque : { len=0x2 } [Type: alloc::collections::vec_deque::VecDeque] -// cdb-check: [] [Type: alloc::collections::vec_deque::VecDeque] +// cdb-check:vecdeque : { len=0x2 } [Type: alloc::collections::vec_deque::VecDeque] +// cdb-check: [] [Type: alloc::collections::vec_deque::VecDeque] // cdb-check: [len] : 0x2 // cdb-check: [capacity] : 0x8 [Type: unsigned [...]] // cdb-check: [0x0] : 90 [Type: int]