Skip to content

Commit 066da2a

Browse files
RawVec includes GlobalCoAllocMeta based on allocator; related bounds
1 parent 6092fff commit 066da2a

File tree

10 files changed

+175
-65
lines changed

10 files changed

+175
-65
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ macro_rules! __impl_slice_eq1 {
44
impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs
55
where
66
T: PartialEq<U>,
7+
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
78
$($constraints)*
89
{
910
fn eq(&self, other: &$rhs) -> bool {

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

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
//! are not required to be copyable, and the queue will be sendable if the
66
//! contained type is sendable.
77
8-
#![stable(feature = "rust1", since = "1.0.0")]
8+
#![feature(global_co_alloc)]
99

10+
#![stable(feature = "rust1", since = "1.0.0")]
11+
use core::alloc;
1012
use core::cmp::{self, Ordering};
1113
use core::fmt;
1214
use core::hash::{Hash, Hasher};
@@ -91,10 +93,13 @@ mod tests;
9193
#[cfg_attr(not(test), rustc_diagnostic_item = "VecDeque")]
9294
#[stable(feature = "rust1", since = "1.0.0")]
9395
#[rustc_insignificant_dtor]
96+
// @TODO
9497
pub struct VecDeque<
9598
T,
9699
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
97-
> {
100+
>
101+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
102+
{
98103
// `self[0]`, if it exists, is `buf[head]`.
99104
// `head < buf.capacity()`, unless `buf.capacity() == 0` when `head == 0`.
100105
head: usize,
@@ -106,7 +111,8 @@ pub struct VecDeque<
106111
}
107112

108113
#[stable(feature = "rust1", since = "1.0.0")]
109-
impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> {
114+
impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A>
115+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
110116
fn clone(&self) -> Self {
111117
let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone());
112118
deq.extend(self.iter().cloned());
@@ -120,7 +126,8 @@ impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> {
120126
}
121127

122128
#[stable(feature = "rust1", since = "1.0.0")]
123-
unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> {
129+
unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A>
130+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
124131
fn drop(&mut self) {
125132
/// Runs the destructor for all items in the slice when it gets dropped (normally or
126133
/// during unwinding).
@@ -153,7 +160,8 @@ impl<T> Default for VecDeque<T> {
153160
}
154161
}
155162

156-
impl<T, A: Allocator> VecDeque<T, A> {
163+
impl<T, A: Allocator> VecDeque<T, A>
164+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
157165
/// Marginally more convenient
158166
#[inline]
159167
fn ptr(&self) -> *mut T {
@@ -442,7 +450,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
442450
mut iter: impl Iterator<Item = T>,
443451
len: usize,
444452
) -> usize {
445-
struct Guard<'a, T, A: Allocator> {
453+
struct Guard<'a, T, A: Allocator>
454+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
446455
deque: &'a mut VecDeque<T, A>,
447456
written: usize,
448457
}
@@ -561,7 +570,8 @@ impl<T> VecDeque<T> {
561570
}
562571
}
563572

564-
impl<T, A: Allocator> VecDeque<T, A> {
573+
impl<T, A: Allocator> VecDeque<T, A>
574+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
565575
/// Creates an empty deque.
566576
///
567577
/// # Examples
@@ -2596,7 +2606,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
25962606
}
25972607
}
25982608

2599-
impl<T: Clone, A: Allocator> VecDeque<T, A> {
2609+
impl<T: Clone, A: Allocator> VecDeque<T, A>
2610+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
26002611
/// Modifies the deque in-place so that `len()` is equal to new_len,
26012612
/// either by removing excess elements from the back or by appending clones of `value`
26022613
/// to the back.
@@ -2641,7 +2652,8 @@ fn wrap_index(logical_index: usize, capacity: usize) -> usize {
26412652
}
26422653

26432654
#[stable(feature = "rust1", since = "1.0.0")]
2644-
impl<T: PartialEq, A: Allocator> PartialEq for VecDeque<T, A> {
2655+
impl<T: PartialEq, A: Allocator> PartialEq for VecDeque<T, A>
2656+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
26452657
fn eq(&self, other: &Self) -> bool {
26462658
if self.len != other.len() {
26472659
return false;
@@ -2680,7 +2692,8 @@ impl<T: PartialEq, A: Allocator> PartialEq for VecDeque<T, A> {
26802692
}
26812693

26822694
#[stable(feature = "rust1", since = "1.0.0")]
2683-
impl<T: Eq, A: Allocator> Eq for VecDeque<T, A> {}
2695+
impl<T: Eq, A: Allocator> Eq for VecDeque<T, A>
2696+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
26842697

26852698
__impl_slice_eq1! { [] VecDeque<T, A>, Vec<U, A>, }
26862699
__impl_slice_eq1! { [] VecDeque<T, A>, &[U], }
@@ -2690,22 +2703,25 @@ __impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &[U; N], }
26902703
__impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &mut [U; N], }
26912704

26922705
#[stable(feature = "rust1", since = "1.0.0")]
2693-
impl<T: PartialOrd, A: Allocator> PartialOrd for VecDeque<T, A> {
2706+
impl<T: PartialOrd, A: Allocator> PartialOrd for VecDeque<T, A>
2707+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
26942708
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
26952709
self.iter().partial_cmp(other.iter())
26962710
}
26972711
}
26982712

26992713
#[stable(feature = "rust1", since = "1.0.0")]
2700-
impl<T: Ord, A: Allocator> Ord for VecDeque<T, A> {
2714+
impl<T: Ord, A: Allocator> Ord for VecDeque<T, A>
2715+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27012716
#[inline]
27022717
fn cmp(&self, other: &Self) -> Ordering {
27032718
self.iter().cmp(other.iter())
27042719
}
27052720
}
27062721

27072722
#[stable(feature = "rust1", since = "1.0.0")]
2708-
impl<T: Hash, A: Allocator> Hash for VecDeque<T, A> {
2723+
impl<T: Hash, A: Allocator> Hash for VecDeque<T, A>
2724+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27092725
fn hash<H: Hasher>(&self, state: &mut H) {
27102726
state.write_length_prefix(self.len);
27112727
// It's not possible to use Hash::hash_slice on slices
@@ -2719,7 +2735,8 @@ impl<T: Hash, A: Allocator> Hash for VecDeque<T, A> {
27192735
}
27202736

27212737
#[stable(feature = "rust1", since = "1.0.0")]
2722-
impl<T, A: Allocator> Index<usize> for VecDeque<T, A> {
2738+
impl<T, A: Allocator> Index<usize> for VecDeque<T, A>
2739+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27232740
type Output = T;
27242741

27252742
#[inline]
@@ -2729,7 +2746,8 @@ impl<T, A: Allocator> Index<usize> for VecDeque<T, A> {
27292746
}
27302747

27312748
#[stable(feature = "rust1", since = "1.0.0")]
2732-
impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> {
2749+
impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A>
2750+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27332751
#[inline]
27342752
fn index_mut(&mut self, index: usize) -> &mut T {
27352753
self.get_mut(index).expect("Out of bounds access")
@@ -2744,7 +2762,8 @@ impl<T> FromIterator<T> for VecDeque<T> {
27442762
}
27452763

27462764
#[stable(feature = "rust1", since = "1.0.0")]
2747-
impl<T, A: Allocator> IntoIterator for VecDeque<T, A> {
2765+
impl<T, A: Allocator> IntoIterator for VecDeque<T, A>
2766+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27482767
type Item = T;
27492768
type IntoIter = IntoIter<T, A>;
27502769

@@ -2756,7 +2775,8 @@ impl<T, A: Allocator> IntoIterator for VecDeque<T, A> {
27562775
}
27572776

27582777
#[stable(feature = "rust1", since = "1.0.0")]
2759-
impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque<T, A> {
2778+
impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque<T, A>
2779+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27602780
type Item = &'a T;
27612781
type IntoIter = Iter<'a, T>;
27622782

@@ -2766,7 +2786,8 @@ impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque<T, A> {
27662786
}
27672787

27682788
#[stable(feature = "rust1", since = "1.0.0")]
2769-
impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A> {
2789+
impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A>
2790+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27702791
type Item = &'a mut T;
27712792
type IntoIter = IterMut<'a, T>;
27722793

@@ -2776,7 +2797,8 @@ impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A> {
27762797
}
27772798

27782799
#[stable(feature = "rust1", since = "1.0.0")]
2779-
impl<T, A: Allocator> Extend<T> for VecDeque<T, A> {
2800+
impl<T, A: Allocator> Extend<T> for VecDeque<T, A>
2801+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27802802
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
27812803
<Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter());
27822804
}
@@ -2793,7 +2815,8 @@ impl<T, A: Allocator> Extend<T> for VecDeque<T, A> {
27932815
}
27942816

27952817
#[stable(feature = "extend_ref", since = "1.2.0")]
2796-
impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A> {
2818+
impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A>
2819+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
27972820
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
27982821
self.spec_extend(iter.into_iter());
27992822
}
@@ -2810,14 +2833,16 @@ impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A> {
28102833
}
28112834

28122835
#[stable(feature = "rust1", since = "1.0.0")]
2813-
impl<T: fmt::Debug, A: Allocator> fmt::Debug for VecDeque<T, A> {
2836+
impl<T: fmt::Debug, A: Allocator> fmt::Debug for VecDeque<T, A>
2837+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
28142838
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28152839
f.debug_list().entries(self.iter()).finish()
28162840
}
28172841
}
28182842

28192843
#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
2820-
impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
2844+
impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A>
2845+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
28212846
/// Turn a [`Vec<T>`] into a [`VecDeque<T>`].
28222847
///
28232848
/// [`Vec<T>`]: crate::vec::Vec
@@ -2834,7 +2859,8 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
28342859
}
28352860

28362861
#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
2837-
impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> {
2862+
impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A>
2863+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
28382864
/// Turn a [`VecDeque<T>`] into a [`Vec<T>`].
28392865
///
28402866
/// [`Vec<T>`]: crate::vec::Vec

library/alloc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#![warn(deprecated_in_future)]
8787
#![warn(missing_debug_implementations)]
8888
#![warn(missing_docs)]
89+
#![allow(incomplete_features)]
8990
#![allow(explicit_outlives_requirements)]
9091
//
9192
// Library features:
@@ -123,6 +124,7 @@
123124
#![feature(extend_one)]
124125
#![feature(fmt_internals)]
125126
#![feature(fn_traits)]
127+
#![feature(generic_const_exprs)]
126128
#![feature(global_co_alloc_meta)]
127129
#![feature(hasher_prefixfree_extras)]
128130
#![feature(inline_const)]

library/alloc/src/raw_vec.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")]
22

3-
use core::alloc::{LayoutError, GlobalCoAllocMeta};
3+
use core::alloc::{self, LayoutError, GlobalCoAllocMeta};
44
use core::cmp;
55
use core::intrinsics;
66
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
@@ -49,12 +49,19 @@ enum AllocInit {
4949
/// `usize::MAX`. This means that you need to be careful when round-tripping this type with a
5050
/// `Box<[T]>`, since `capacity()` won't yield the length.
5151
#[allow(missing_debug_implementations)]
52-
pub(crate) struct RawVec<T, A: Allocator = Global> {
52+
// @TODO
53+
// 1. make const generic _coop come from the target specification
54+
// 2. apply `_coop` with logical && to `A::IsCoAllocator`
55+
pub(crate) struct RawVec<T, A: Allocator = Global, const _coop: bool = true>
56+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
57+
{
5358
ptr: Unique<T>,
5459
cap: usize,
5560
alloc: A,
56-
#[allow(dead_code)]
57-
pub(crate) meta: GlobalCoAllocMeta,
61+
// As of v1.67.0, `cmp` for `TypeId` is not `const`, unfortunately:
62+
//pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}],
63+
//pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()],
64+
pub(crate) meta: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots::<A>()],
5865
}
5966

6067
impl<T> RawVec<T, Global> {
@@ -104,7 +111,9 @@ impl<T> RawVec<T, Global> {
104111
}
105112
}
106113

107-
impl<T, A: Allocator> RawVec<T, A> {
114+
impl<T, A: Allocator> RawVec<T, A>
115+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
116+
{
108117
// Tiny Vecs are dumb. Skip to:
109118
// - 8 if the element size is 1, because any heap allocators is likely
110119
// to round up a request of less than 8 bytes to at least 8 bytes.
@@ -284,7 +293,9 @@ impl<T, A: Allocator> RawVec<T, A> {
284293
slf: &mut RawVec<T, A>,
285294
len: usize,
286295
additional: usize,
287-
) {
296+
)
297+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
298+
{
288299
handle_reserve(slf.grow_amortized(len, additional));
289300
}
290301

@@ -357,14 +368,18 @@ impl<T, A: Allocator> RawVec<T, A> {
357368
}
358369
}
359370

360-
impl<T, A: Allocator> RawVec<T, A> {
371+
impl<T, A: Allocator> RawVec<T, A>
372+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
373+
{
361374
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
362375
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
363-
fn needs_to_grow(&self, len: usize, additional: usize) -> bool {
376+
fn needs_to_grow(&self, len: usize, additional: usize) -> bool
377+
{
364378
additional > self.capacity().wrapping_sub(len)
365379
}
366380

367-
fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) {
381+
fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize)
382+
{
368383
// Allocators currently return a `NonNull<[u8]>` whose length matches
369384
// the size requested. If that ever changes, the capacity here should
370385
// change to `ptr.len() / mem::size_of::<T>()`.
@@ -475,24 +490,27 @@ where
475490
memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
476491
}
477492

478-
unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
493+
unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A>
494+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:
495+
{
479496
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
480497
default fn drop(&mut self) {
481498
if let Some((ptr, layout)) = self.current_memory() {
482-
unsafe { self.alloc.deallocate(ptr, layout) }
499+
unsafe { self.alloc.co_deallocate(ptr, layout) }
483500
}
484501
}
485502
}
486503

487-
unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> {
504+
// @TODO Custom
505+
/*unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> {
488506
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
489507
fn drop(&mut self) {
490508
// @TODO
491509
if let Some((ptr, layout)) = self.current_memory() {
492510
unsafe { self.alloc.deallocate(ptr, layout) }
493511
}
494512
}
495-
}
513+
}*/
496514

497515
// Central function for reserve error handling.
498516
#[cfg(not(no_global_oom_handling))]

0 commit comments

Comments
 (0)