Skip to content

Commit 023f14a

Browse files
CoAlloc: tidy
1 parent 5a1f3bd commit 023f14a

32 files changed

+517
-397
lines changed

library/alloc/src/boxed.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
147147
#![stable(feature = "rust1", since = "1.0.0")]
148148

149+
use crate::co_alloc::CoAllocPref;
149150
use core::any::Any;
150151
use core::async_iter::AsyncIterator;
151152
use core::borrow;
@@ -167,7 +168,6 @@ use core::ops::{
167168
use core::pin::Pin;
168169
use core::ptr::{self, Unique};
169170
use core::task::{Context, Poll};
170-
use crate::co_alloc::CoAllocPref;
171171

172172
#[cfg(not(no_global_oom_handling))]
173173
use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
@@ -643,7 +643,9 @@ impl<T> Box<[T]> {
643643
#[must_use]
644644
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
645645
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
646-
unsafe { RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::with_capacity(len).into_box(len) }
646+
unsafe {
647+
RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::with_capacity(len).into_box(len)
648+
}
647649
}
648650

649651
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -669,7 +671,10 @@ impl<T> Box<[T]> {
669671
#[must_use]
670672
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
671673
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
672-
unsafe { RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::with_capacity_zeroed(len).into_box(len) }
674+
unsafe {
675+
RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::with_capacity_zeroed(len)
676+
.into_box(len)
677+
}
673678
}
674679

675680
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -701,7 +706,7 @@ impl<T> Box<[T]> {
701706
Err(_) => return Err(AllocError),
702707
};
703708
let ptr = Global.allocate(layout)?;
704-
Ok(RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::from_raw_parts_in(
709+
Ok(RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::from_raw_parts_in(
705710
ptr.as_mut_ptr() as *mut _,
706711
len,
707712
Global,
@@ -738,7 +743,7 @@ impl<T> Box<[T]> {
738743
Err(_) => return Err(AllocError),
739744
};
740745
let ptr = Global.allocate_zeroed(layout)?;
741-
Ok(RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::from_raw_parts_in(
746+
Ok(RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::from_raw_parts_in(
742747
ptr.as_mut_ptr() as *mut _,
743748
len,
744749
Global,
@@ -751,7 +756,7 @@ impl<T> Box<[T]> {
751756
#[allow(unused_braces)]
752757
impl<T, A: Allocator> Box<[T], A>
753758
where
754-
[(); {crate::meta_num_slots!(A, crate::CO_ALLOC_PREF_META_NO!())}]:,
759+
[(); { crate::meta_num_slots!(A, crate::CO_ALLOC_PREF_META_NO!()) }]:,
755760
{
756761
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
757762
///
@@ -781,7 +786,9 @@ where
781786
#[must_use]
782787
#[allow(unused_braces)]
783788
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
784-
unsafe { RawVec::<T, A, {CO_ALLOC_PREF_META_NO!()}>::with_capacity_in(len, alloc).into_box(len) }
789+
unsafe {
790+
RawVec::<T, A, { CO_ALLOC_PREF_META_NO!() }>::with_capacity_in(len, alloc).into_box(len)
791+
}
785792
}
786793

787794
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -810,7 +817,10 @@ where
810817
#[must_use]
811818
#[allow(unused_braces)]
812819
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
813-
unsafe { RawVec::<T, A, {CO_ALLOC_PREF_META_NO!()}>::with_capacity_zeroed_in(len, alloc).into_box(len) }
820+
unsafe {
821+
RawVec::<T, A, { CO_ALLOC_PREF_META_NO!() }>::with_capacity_zeroed_in(len, alloc)
822+
.into_box(len)
823+
}
814824
}
815825
}
816826

@@ -1516,7 +1526,7 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
15161526
fn from(slice: &[T]) -> Box<[T]> {
15171527
let len = slice.len();
15181528
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
1519-
let buf = RawVec::<T, Global, {CO_ALLOC_PREF_META_NO!()}>::with_capacity(len);
1529+
let buf = RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::with_capacity(len);
15201530
unsafe {
15211531
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
15221532
buf.into_box(slice.len()).assume_init()
@@ -1682,9 +1692,10 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
16821692
#[cfg(not(no_global_oom_handling))]
16831693
#[stable(feature = "boxed_array_try_from_vec", since = "1.66.0")]
16841694
#[allow(unused_braces)]
1685-
impl<T, const N: usize, const CO_ALLOC_PREF: CoAllocPref> TryFrom<Vec<T, Global, CO_ALLOC_PREF>> for Box<[T; N]>
1695+
impl<T, const N: usize, const CO_ALLOC_PREF: CoAllocPref> TryFrom<Vec<T, Global, CO_ALLOC_PREF>>
1696+
for Box<[T; N]>
16861697
where
1687-
[(); {meta_num_slots_global!(CO_ALLOC_PREF)}]:,
1698+
[(); { meta_num_slots_global!(CO_ALLOC_PREF) }]:,
16881699
{
16891700
type Error = Vec<T, Global, CO_ALLOC_PREF>;
16901701

@@ -2046,12 +2057,12 @@ impl<I> FromIterator<I> for Box<[I]> {
20462057
#[allow(unused_braces)]
20472058
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A>
20482059
where
2049-
[(); {crate::meta_num_slots!(A, crate::CO_ALLOC_PREF_META_NO!())}]:,
2060+
[(); { crate::meta_num_slots!(A, crate::CO_ALLOC_PREF_META_NO!()) }]:,
20502061
{
20512062
fn clone(&self) -> Self {
20522063
let alloc = Box::allocator(self).clone();
20532064
// false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2054-
self.to_vec_in_co::<A, {CO_ALLOC_PREF_META_NO!()}>(alloc).into_boxed_slice()
2065+
self.to_vec_in_co::<A, { CO_ALLOC_PREF_META_NO!() }>(alloc).into_boxed_slice()
20552066
}
20562067

20572068
fn clone_from(&mut self, other: &Self) {

library/alloc/src/co_alloc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! CoAlloction-specific types that only apply in heap-based applications (hence not a part of
22
//! [::core]).
3-
//!
3+
//!
44
//! Types here have names with `CoAlloc` prefix. Yes, when using a q ualified path (like
55
//! ::alloc::co_alloc::CoAllocPref), that involves "stuttering", which is not recommended.
6-
//!
6+
//!
77
//! However, as per Rust Book the common practice is to import type names fully and access them just
88
//! with their name (except for cases of conflict). And we don't want the type names any shorter
99
//! (such `Pref`), because thoe would be vague/confusing.
@@ -44,4 +44,4 @@ pub type CoAllocPref = u8;
4444
/// vales, or in place of a result of `meta_num_slots` macro. That also prevents mixing up with
4545
/// [core::alloc::CoAllocatorMetaNumSlots].
4646
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
47-
pub type CoAllocMetaNumSlotsPref = u16;
47+
pub type CoAllocMetaNumSlotsPref = u16;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@
143143
#![allow(missing_docs)]
144144
#![stable(feature = "rust1", since = "1.0.0")]
145145

146+
use crate::co_alloc::CoAllocPref;
146147
use core::fmt;
147148
use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen};
148-
use crate::co_alloc::CoAllocPref;
149149
use core::mem::{self, swap, ManuallyDrop};
150150
use core::num::NonZeroUsize;
151151
use core::ops::{Deref, DerefMut};
@@ -1529,7 +1529,7 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
15291529
#[allow(unused_braces)]
15301530
pub struct Drain<'a, T: 'a, const CO_ALLOC_PREF: CoAllocPref>
15311531
where
1532-
[(); {meta_num_slots_global!(CO_ALLOC_PREF)}]:,
1532+
[(); { meta_num_slots_global!(CO_ALLOC_PREF) }]:,
15331533
{
15341534
iter: vec::Drain<'a, T, Global, CO_ALLOC_PREF>,
15351535
}
@@ -1538,7 +1538,7 @@ where
15381538
#[allow(unused_braces)]
15391539
impl<T, const CO_ALLOC_PREF: CoAllocPref> Iterator for Drain<'_, T, CO_ALLOC_PREF>
15401540
where
1541-
[(); {meta_num_slots_global!(CO_ALLOC_PREF)}]:,
1541+
[(); { meta_num_slots_global!(CO_ALLOC_PREF) }]:,
15421542
{
15431543
type Item = T;
15441544

@@ -1557,7 +1557,7 @@ where
15571557
#[allow(unused_braces)]
15581558
impl<T, const CO_ALLOC_PREF: CoAllocPref> DoubleEndedIterator for Drain<'_, T, CO_ALLOC_PREF>
15591559
where
1560-
[(); {meta_num_slots_global!(CO_ALLOC_PREF)}]:,
1560+
[(); { meta_num_slots_global!(CO_ALLOC_PREF) }]:,
15611561
{
15621562
#[inline]
15631563
fn next_back(&mut self) -> Option<T> {
@@ -1569,7 +1569,7 @@ where
15691569
#[allow(unused_braces)]
15701570
impl<T, const CO_ALLOC_PREF: CoAllocPref> ExactSizeIterator for Drain<'_, T, CO_ALLOC_PREF>
15711571
where
1572-
[(); {meta_num_slots_global!(CO_ALLOC_PREF)}]:,
1572+
[(); { meta_num_slots_global!(CO_ALLOC_PREF) }]:,
15731573
{
15741574
fn is_empty(&self) -> bool {
15751575
self.iter.is_empty()
@@ -1579,7 +1579,7 @@ where
15791579
#[stable(feature = "fused", since = "1.26.0")]
15801580
#[allow(unused_braces)]
15811581
impl<T, const CO_ALLOC_PREF: CoAllocPref> FusedIterator for Drain<'_, T, CO_ALLOC_PREF> where
1582-
[(); {meta_num_slots_global!(CO_ALLOC_PREF)}]:
1582+
[(); { meta_num_slots_global!(CO_ALLOC_PREF) }]:
15831583
{
15841584
}
15851585

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

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use core::iter::FusedIterator;
21
use crate::co_alloc::CoAllocPref;
2+
use core::iter::FusedIterator;
33
use core::marker::PhantomData;
44
use core::mem::{self, SizedTypeProperties};
55
use core::ptr::NonNull;
@@ -23,10 +23,8 @@ pub struct Drain<
2323
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
2424
const CO_ALLOC_PREF: CoAllocPref = { SHORT_TERM_VEC_CO_ALLOC_PREF!() },
2525
> where
26-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
26+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
2727
{
28-
// We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T
29-
// and we want it to be covariant instead
3028
deque: NonNull<VecDeque<T, A, CO_ALLOC_PREF>>,
3129
// drain_start is stored in deque.len
3230
drain_len: usize,
@@ -42,7 +40,7 @@ pub struct Drain<
4240
#[allow(unused_braces)]
4341
impl<'a, T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> Drain<'a, T, A, CO_ALLOC_PREF>
4442
where
45-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
43+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
4644
{
4745
pub(super) unsafe fn new(
4846
deque: &'a mut VecDeque<T, A, CO_ALLOC_PREF>,
@@ -98,9 +96,10 @@ where
9896

9997
#[stable(feature = "collection_debug", since = "1.17.0")]
10098
#[allow(unused_braces)]
101-
impl<T: fmt::Debug, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> fmt::Debug for Drain<'_, T, A, CO_ALLOC_PREF>
99+
impl<T: fmt::Debug, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> fmt::Debug
100+
for Drain<'_, T, A, CO_ALLOC_PREF>
102101
where
103-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
102+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
104103
{
105104
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106105
f.debug_tuple("Drain")
@@ -114,33 +113,38 @@ where
114113

115114
#[stable(feature = "drain", since = "1.6.0")]
116115
#[allow(unused_braces)]
117-
unsafe impl<T: Sync, A: Allocator + Sync, const CO_ALLOC_PREF: CoAllocPref> Sync for Drain<'_, T, A, CO_ALLOC_PREF> where
118-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:
116+
unsafe impl<T: Sync, A: Allocator + Sync, const CO_ALLOC_PREF: CoAllocPref> Sync
117+
for Drain<'_, T, A, CO_ALLOC_PREF>
118+
where
119+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
119120
{
120121
}
121122
#[stable(feature = "drain", since = "1.6.0")]
122123
#[allow(unused_braces)]
123-
unsafe impl<T: Send, A: Allocator + Send, const CO_ALLOC_PREF: CoAllocPref> Send for Drain<'_, T, A, CO_ALLOC_PREF> where
124-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:
124+
unsafe impl<T: Send, A: Allocator + Send, const CO_ALLOC_PREF: CoAllocPref> Send
125+
for Drain<'_, T, A, CO_ALLOC_PREF>
126+
where
127+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
125128
{
126129
}
127130

128131
#[stable(feature = "drain", since = "1.6.0")]
129132
#[allow(unused_braces)]
130133
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> Drop for Drain<'_, T, A, CO_ALLOC_PREF>
131134
where
132-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
135+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
133136
{
134137
fn drop(&mut self) {
135138
struct DropGuard<'r, 'a, T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref>(
136139
&'r mut Drain<'a, T, A, CO_ALLOC_PREF>,
137140
)
138141
where
139-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:;
142+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:;
140143

141-
impl<'r, 'a, T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> Drop for DropGuard<'r, 'a, T, A, CO_ALLOC_PREF>
144+
impl<'r, 'a, T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> Drop
145+
for DropGuard<'r, 'a, T, A, CO_ALLOC_PREF>
142146
where
143-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
147+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
144148
{
145149
fn drop(&mut self) {
146150
if self.0.remaining != 0 {
@@ -225,7 +229,7 @@ where
225229
#[allow(unused_braces)]
226230
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> Iterator for Drain<'_, T, A, CO_ALLOC_PREF>
227231
where
228-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
232+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
229233
{
230234
type Item = T;
231235

@@ -249,9 +253,10 @@ where
249253

250254
#[stable(feature = "drain", since = "1.6.0")]
251255
#[allow(unused_braces)]
252-
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> DoubleEndedIterator for Drain<'_, T, A, CO_ALLOC_PREF>
256+
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> DoubleEndedIterator
257+
for Drain<'_, T, A, CO_ALLOC_PREF>
253258
where
254-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
259+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
255260
{
256261
#[inline]
257262
fn next_back(&mut self) -> Option<T> {
@@ -266,14 +271,18 @@ where
266271

267272
#[stable(feature = "drain", since = "1.6.0")]
268273
#[allow(unused_braces)]
269-
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> ExactSizeIterator for Drain<'_, T, A, CO_ALLOC_PREF> where
270-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:
274+
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> ExactSizeIterator
275+
for Drain<'_, T, A, CO_ALLOC_PREF>
276+
where
277+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
271278
{
272279
}
273280

274281
#[stable(feature = "fused", since = "1.26.0")]
275282
#[allow(unused_braces)]
276-
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> FusedIterator for Drain<'_, T, A, CO_ALLOC_PREF> where
277-
[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:
283+
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> FusedIterator
284+
for Drain<'_, T, A, CO_ALLOC_PREF>
285+
where
286+
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
278287
{
279288
}

0 commit comments

Comments
 (0)