Skip to content

Commit

Permalink
Auto merge of #86595 - a1phyr:allocator_api_for_vecdeque, r=Amanieu
Browse files Browse the repository at this point in the history
Add support for custom allocator in `VecDeque`

This follows the [roadmap](rust-lang/wg-allocators#7) of the allocator WG to add custom allocators to collections.

`@rustbot` modify labels: +A-allocators +T-libs
  • Loading branch information
bors committed Jul 25, 2021
2 parents 478126c + 8987b74 commit 9c25eb7
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 76 deletions.
30 changes: 18 additions & 12 deletions library/alloc/src/collections/vec_deque/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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<VecDeque<T>>,
pub(crate) deque: NonNull<VecDeque<T, A>>,
}

#[stable(feature = "collection_debug", since = "1.17.0")]
impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Drain")
.field(&self.after_tail)
Expand All @@ -30,16 +36,16 @@ impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
}

#[stable(feature = "drain", since = "1.6.0")]
unsafe impl<T: Sync> Sync for Drain<'_, T> {}
unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A> {}
#[stable(feature = "drain", since = "1.6.0")]
unsafe impl<T: Send> Send for Drain<'_, T> {}
unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A> {}

#[stable(feature = "drain", since = "1.6.0")]
impl<T> Drop for Drain<'_, T> {
impl<T, A: Allocator> 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);

Expand Down Expand Up @@ -96,7 +102,7 @@ impl<T> Drop for Drain<'_, T> {
}

#[stable(feature = "drain", since = "1.6.0")]
impl<T> Iterator for Drain<'_, T> {
impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
type Item = T;

#[inline]
Expand All @@ -111,15 +117,15 @@ impl<T> Iterator for Drain<'_, T> {
}

#[stable(feature = "drain", since = "1.6.0")]
impl<T> DoubleEndedIterator for Drain<'_, T> {
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
#[inline]
fn next_back(&mut self) -> Option<T> {
self.iter.next_back().map(|elt| unsafe { ptr::read(elt) })
}
}

#[stable(feature = "drain", since = "1.6.0")]
impl<T> ExactSizeIterator for Drain<'_, T> {}
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {}

#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for Drain<'_, T> {}
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}
21 changes: 13 additions & 8 deletions library/alloc/src/collections/vec_deque/into_iter.rs
Original file line number Diff line number Diff line change
@@ -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`.
Expand All @@ -11,19 +13,22 @@ use super::VecDeque;
/// [`into_iter`]: VecDeque::into_iter
#[derive(Clone)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<T> {
pub(crate) inner: VecDeque<T>,
pub struct IntoIter<
T,
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
> {
pub(crate) inner: VecDeque<T, A>,
}

#[stable(feature = "collection_debug", since = "1.17.0")]
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
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<T> Iterator for IntoIter<T> {
impl<T, A: Allocator> Iterator for IntoIter<T, A> {
type Item = T;

#[inline]
Expand All @@ -39,22 +44,22 @@ impl<T> Iterator for IntoIter<T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> DoubleEndedIterator for IntoIter<T> {
impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
#[inline]
fn next_back(&mut self) -> Option<T> {
self.inner.pop_back()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IntoIter<T> {
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
fn is_empty(&self) -> bool {
self.inner.is_empty()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for IntoIter<T> {}
impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}

#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<T> TrustedLen for IntoIter<T> {}
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}
4 changes: 2 additions & 2 deletions library/alloc/src/collections/vec_deque/macros.rs
Original file line number Diff line number Diff line change
@@ -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<A, B, $($vars)*> PartialEq<$rhs> for $lhs
impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs
where
A: PartialEq<B>,
T: PartialEq<U>,
$($constraints)*
{
fn eq(&self, other: &$rhs) -> bool {
Expand Down
Loading

0 comments on commit 9c25eb7

Please sign in to comment.