Skip to content

Commit 9c25eb7

Browse files
committed
Auto merge of #86595 - a1phyr:allocator_api_for_vecdeque, r=Amanieu
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
2 parents 478126c + 8987b74 commit 9c25eb7

File tree

6 files changed

+139
-76
lines changed

6 files changed

+139
-76
lines changed

library/alloc/src/collections/vec_deque/drain.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use core::iter::FusedIterator;
22
use core::ptr::{self, NonNull};
33
use core::{fmt, mem};
44

5+
use crate::alloc::{Allocator, Global};
6+
57
use super::{count, Iter, VecDeque};
68

79
/// A draining iterator over the elements of a `VecDeque`.
@@ -11,15 +13,19 @@ use super::{count, Iter, VecDeque};
1113
///
1214
/// [`drain`]: VecDeque::drain
1315
#[stable(feature = "drain", since = "1.6.0")]
14-
pub struct Drain<'a, T: 'a> {
16+
pub struct Drain<
17+
'a,
18+
T: 'a,
19+
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
20+
> {
1521
pub(crate) after_tail: usize,
1622
pub(crate) after_head: usize,
1723
pub(crate) iter: Iter<'a, T>,
18-
pub(crate) deque: NonNull<VecDeque<T>>,
24+
pub(crate) deque: NonNull<VecDeque<T, A>>,
1925
}
2026

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

3238
#[stable(feature = "drain", since = "1.6.0")]
33-
unsafe impl<T: Sync> Sync for Drain<'_, T> {}
39+
unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A> {}
3440
#[stable(feature = "drain", since = "1.6.0")]
35-
unsafe impl<T: Send> Send for Drain<'_, T> {}
41+
unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A> {}
3642

3743
#[stable(feature = "drain", since = "1.6.0")]
38-
impl<T> Drop for Drain<'_, T> {
44+
impl<T, A: Allocator> Drop for Drain<'_, T, A> {
3945
fn drop(&mut self) {
40-
struct DropGuard<'r, 'a, T>(&'r mut Drain<'a, T>);
46+
struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>);
4147

42-
impl<'r, 'a, T> Drop for DropGuard<'r, 'a, T> {
48+
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> {
4349
fn drop(&mut self) {
4450
self.0.for_each(drop);
4551

@@ -96,7 +102,7 @@ impl<T> Drop for Drain<'_, T> {
96102
}
97103

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

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

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

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

124130
#[stable(feature = "fused", since = "1.26.0")]
125-
impl<T> FusedIterator for Drain<'_, T> {}
131+
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}

library/alloc/src/collections/vec_deque/into_iter.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use core::fmt;
22
use core::iter::{FusedIterator, TrustedLen};
33

4+
use crate::alloc::{Allocator, Global};
5+
46
use super::VecDeque;
57

68
/// An owning iterator over the elements of a `VecDeque`.
@@ -11,19 +13,22 @@ use super::VecDeque;
1113
/// [`into_iter`]: VecDeque::into_iter
1214
#[derive(Clone)]
1315
#[stable(feature = "rust1", since = "1.0.0")]
14-
pub struct IntoIter<T> {
15-
pub(crate) inner: VecDeque<T>,
16+
pub struct IntoIter<
17+
T,
18+
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
19+
> {
20+
pub(crate) inner: VecDeque<T, A>,
1621
}
1722

1823
#[stable(feature = "collection_debug", since = "1.17.0")]
19-
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
24+
impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
2025
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2126
f.debug_tuple("IntoIter").field(&self.inner).finish()
2227
}
2328
}
2429

2530
#[stable(feature = "rust1", since = "1.0.0")]
26-
impl<T> Iterator for IntoIter<T> {
31+
impl<T, A: Allocator> Iterator for IntoIter<T, A> {
2732
type Item = T;
2833

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

4146
#[stable(feature = "rust1", since = "1.0.0")]
42-
impl<T> DoubleEndedIterator for IntoIter<T> {
47+
impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
4348
#[inline]
4449
fn next_back(&mut self) -> Option<T> {
4550
self.inner.pop_back()
4651
}
4752
}
4853

4954
#[stable(feature = "rust1", since = "1.0.0")]
50-
impl<T> ExactSizeIterator for IntoIter<T> {
55+
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
5156
fn is_empty(&self) -> bool {
5257
self.inner.is_empty()
5358
}
5459
}
5560

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

5964
#[unstable(feature = "trusted_len", issue = "37572")]
60-
unsafe impl<T> TrustedLen for IntoIter<T> {}
65+
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}

library/alloc/src/collections/vec_deque/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
macro_rules! __impl_slice_eq1 {
22
([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => {
33
#[stable(feature = "vec_deque_partial_eq_slice", since = "1.17.0")]
4-
impl<A, B, $($vars)*> PartialEq<$rhs> for $lhs
4+
impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs
55
where
6-
A: PartialEq<B>,
6+
T: PartialEq<U>,
77
$($constraints)*
88
{
99
fn eq(&self, other: &$rhs) -> bool {

0 commit comments

Comments
 (0)