Skip to content

Commit 611da39

Browse files
CoAlloc: Using macros for config + transformations. WIP
1 parent 84f494d commit 611da39

File tree

9 files changed

+47
-22
lines changed

9 files changed

+47
-22
lines changed

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ mod boxed {
249249
pub use std::boxed::Box;
250250
}
251251
pub mod borrow;
252+
#[macro_use]
252253
#[unstable(feature = "global_co_alloc", issue = "none")]
253254
pub mod co_alloc;
254255
pub mod collections;

library/proc_macro/src/bridge/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![deny(unsafe_code)]
1010

1111
use crate::{Delimiter, Level, LineColumn, Spacing};
12+
use std::alloc::Global;
1213
use std::fmt;
1314
use std::hash::Hash;
1415
use std::marker;
@@ -252,14 +253,14 @@ impl<'a, T, M> Unmark for &'a mut Marked<T, M> {
252253
}
253254
}
254255

255-
impl<T: Mark> Mark for Vec<T, Global, DEFAULT_CO_ALLOC_PREF> {
256+
impl<T: Mark> Mark for Vec<T, Global, {CO_ALLOC_PREF_DEFAULT!()}> {
256257
type Unmarked = Vec<T::Unmarked>;
257258
fn mark(unmarked: Self::Unmarked) -> Self {
258259
// Should be a no-op due to std's in-place collect optimizations.
259260
unmarked.into_iter().map(T::mark).collect()
260261
}
261262
}
262-
impl<T: Unmark> Unmark for Vec<T, Global, DEFAULT_CO_ALLOC_PREF> {
263+
impl<T: Unmark> Unmark for Vec<T, Global, {CO_ALLOC_PREF_DEFAULT!()}> {
263264
type Unmarked = Vec<T::Unmarked>;
264265
fn unmark(self) -> Self::Unmarked {
265266
// Should be a no-op due to std's in-place collect optimizations.

library/proc_macro/src/bridge/rpc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use std::char;
55
use std::io::Write;
66
use std::num::NonZeroU32;
77
use std::str;
8-
//use std::alloc::Global;
9-
use alloc::alloc::Global;
10-
use alloc::DEFAULT_CO_ALLOC_PREF;
8+
use std::alloc::Global;
9+
//use alloc::alloc::Global;
10+
//use std::CO_ALLOC_PREF_DEFAULT;
1111

1212
pub(super) type Writer = super::buffer::Buffer;
1313

@@ -228,7 +228,7 @@ impl<S> DecodeMut<'_, '_, S> for String {
228228
}
229229
}
230230

231-
impl<S, T: Encode<S>> Encode<S> for Vec<T, Global, DEFAULT_CO_ALLOC_PREF!()> {
231+
impl<S, T: Encode<S>> Encode<S> for Vec<T, Global, {CO_ALLOC_PREF_DEFAULT!()}> {
232232
fn encode(self, w: &mut Writer, s: &mut S) {
233233
self.len().encode(w, s);
234234
for x in self {
@@ -238,7 +238,7 @@ impl<S, T: Encode<S>> Encode<S> for Vec<T, Global, DEFAULT_CO_ALLOC_PREF!()> {
238238
}
239239

240240
impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S>
241-
for Vec<T, Global, DEFAULT_CO_ALLOC_PREF>
241+
for Vec<T, Global, {CO_ALLOC_PREF_DEFAULT!()}>
242242
{
243243
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
244244
let len = usize::decode(r, s);

library/proc_macro/src/diagnostic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::Span;
2+
use std::alloc::Global;
23

34
/// An enum representing a diagnostic level.
45
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
@@ -30,7 +31,7 @@ impl MultiSpan for Span {
3031
}
3132

3233
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
33-
impl MultiSpan for Vec<Span, Global, DEFAULT_CO_ALLOC_PREF> {
34+
impl MultiSpan for Vec<Span, Global, {CO_ALLOC_PREF_DEFAULT!()}> {
3435
fn into_spans(self) -> Vec<Span> {
3536
self
3637
}

library/proc_macro/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// to make it compile with rust-analyzer on stable.
2323
#![feature(rustc_allow_const_fn_unstable)]
2424
#![feature(staged_api)]
25+
#![feature(allocator_api)]
2526
#![feature(allow_internal_unstable)]
2627
#![feature(decl_macro)]
2728
#![feature(local_key_cell_methods)]
@@ -33,7 +34,9 @@
3334
#![feature(min_specialization)]
3435
#![feature(strict_provenance)]
3536
#![recursion_limit = "256"]
37+
#![feature(global_co_alloc)]
3638
#![feature(global_co_alloc_default)]
39+
#![feature(global_co_alloc_meta)]
3740

3841
#[unstable(feature = "proc_macro_internals", issue = "27812")]
3942
#[doc(hidden)]

library/std/src/io/cursor.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::io::prelude::*;
66
use crate::alloc::Allocator;
77
use crate::cmp;
88
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
9-
use core::alloc;
9+
use ::alloc::{co_alloc::CoAllocPref, meta_num_slots};
1010

1111
/// A `Cursor` wraps an in-memory buffer and provides it with a
1212
/// [`Seek`] implementation.
@@ -398,13 +398,14 @@ fn slice_write_vectored(
398398
}
399399

400400
/// Reserves the required space, and pads the vec with 0s if necessary.
401+
#[allow(unused_braces)]
401402
fn reserve_and_pad<A: Allocator, const CO_ALLOC_PREF: CoAllocPref>(
402403
pos_mut: &mut u64,
403404
vec: &mut Vec<u8, A, CO_ALLOC_PREF>,
404405
buf_len: usize,
405406
) -> io::Result<usize>
406407
where
407-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(CO_ALLOC_PREF)]:,
408+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
408409
{
409410
let pos: usize = (*pos_mut).try_into().map_err(|_| {
410411
io::const_io_error!(
@@ -444,14 +445,15 @@ where
444445

445446
/// Writes the slice to the vec without allocating
446447
/// # Safety: vec must have buf.len() spare capacity
448+
#[allow(unused_braces)]
447449
unsafe fn vec_write_unchecked<A, const CO_ALLOC_PREF: CoAllocPref>(
448450
pos: usize,
449451
vec: &mut Vec<u8, A, CO_ALLOC_PREF>,
450452
buf: &[u8],
451453
) -> usize
452454
where
453455
A: Allocator,
454-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(CO_ALLOC_PREF)]:,
456+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
455457
{
456458
debug_assert!(vec.capacity() >= pos + buf.len());
457459
vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len());
@@ -467,14 +469,15 @@ where
467469
/// This also allows for the vec body to be empty, but with a position of N.
468470
/// This means that [`Write`] will pad the vec with 0 initially,
469471
/// before writing anything from that point
472+
#[allow(unused_braces)]
470473
fn vec_write<A, const CO_ALLOC_PREF: CoAllocPref>(
471474
pos_mut: &mut u64,
472475
vec: &mut Vec<u8, A, CO_ALLOC_PREF>,
473476
buf: &[u8],
474477
) -> io::Result<usize>
475478
where
476479
A: Allocator,
477-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(CO_ALLOC_PREF)]:,
480+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
478481
{
479482
let buf_len = buf.len();
480483
let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?;
@@ -503,14 +506,15 @@ where
503506
/// This also allows for the vec body to be empty, but with a position of N.
504507
/// This means that [`Write`] will pad the vec with 0 initially,
505508
/// before writing anything from that point
509+
#[allow(unused_braces)]
506510
fn vec_write_vectored<A, const CO_ALLOC_PREF: CoAllocPref>(
507511
pos_mut: &mut u64,
508512
vec: &mut Vec<u8, A, CO_ALLOC_PREF>,
509513
bufs: &[IoSlice<'_>],
510514
) -> io::Result<usize>
511515
where
512516
A: Allocator,
513-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(CO_ALLOC_PREF)]:,
517+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
514518
{
515519
// For safety reasons, we don't want this sum to overflow ever.
516520
// If this saturates, the reserve should panic to avoid any unsound writing.
@@ -558,10 +562,11 @@ impl Write for Cursor<&mut [u8]> {
558562
}
559563

560564
#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
565+
#[allow(unused_braces)]
561566
impl<A, const CO_ALLOC_PREF: CoAllocPref> Write for Cursor<&mut Vec<u8, A, CO_ALLOC_PREF>>
562567
where
563568
A: Allocator,
564-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(CO_ALLOC_PREF)]:,
569+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
565570
{
566571
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
567572
vec_write(&mut self.pos, self.inner, buf)
@@ -583,10 +588,11 @@ where
583588
}
584589

585590
#[stable(feature = "rust1", since = "1.0.0")]
591+
#[allow(unused_braces)]
586592
impl<A, const CO_ALLOC_PREF: CoAllocPref> Write for Cursor<Vec<u8, A, CO_ALLOC_PREF>>
587593
where
588594
A: Allocator,
589-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(CO_ALLOC_PREF)]:,
595+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
590596
{
591597
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
592598
vec_write(&mut self.pos, &mut self.inner, buf)

library/std/src/io/impls.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#[cfg(test)]
22
mod tests;
33

4-
use crate::alloc::{self, Allocator};
4+
use crate::alloc::Allocator;
55
use crate::cmp;
66
use crate::collections::VecDeque;
77
use crate::fmt;
88
use crate::io::{
99
self, BorrowedCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
1010
};
1111
use crate::mem;
12+
use ::alloc::{co_alloc::CoAllocPref, meta_num_slots};
1213

1314
// =============================================================================
1415
// Forwarding implementations
@@ -378,9 +379,10 @@ impl Write for &mut [u8] {
378379
/// Write is implemented for `Vec<u8>` by appending to the vector.
379380
/// The vector will grow as needed.
380381
#[stable(feature = "rust1", since = "1.0.0")]
382+
#[allow(unused_braces)]
381383
impl<A: Allocator, const CO_ALLOC_PREF: CoAllocPref> Write for Vec<u8, A, CO_ALLOC_PREF>
382384
where
383-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(CO_ALLOC_PREF)]:,
385+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
384386
{
385387
#[inline]
386388
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -417,9 +419,10 @@ where
417419

418420
/// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`.
419421
#[stable(feature = "vecdeque_read_write", since = "1.63.0")]
420-
impl<A: Allocator, const _CO_ALLOC_PREF: bool> Read for VecDeque<u8, A, _CO_ALLOC_PREF>
422+
#[allow(unused_braces)]
423+
impl<A: Allocator, const _CO_ALLOC_PREF: CoAllocPref> Read for VecDeque<u8, A, _CO_ALLOC_PREF>
421424
where
422-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(_CO_ALLOC_PREF)]:,
425+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
423426
{
424427
/// Fill `buf` with the contents of the "front" slice as returned by
425428
/// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are
@@ -444,9 +447,10 @@ where
444447

445448
/// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed.
446449
#[stable(feature = "vecdeque_read_write", since = "1.63.0")]
447-
impl<A: Allocator, const _CO_ALLOC_PREF: bool> Write for VecDeque<u8, A, _CO_ALLOC_PREF>
450+
#[allow(unused_braces)]
451+
impl<A: Allocator, const _CO_ALLOC_PREF: CoAllocPref> Write for VecDeque<u8, A, _CO_ALLOC_PREF>
448452
where
449-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(_CO_ALLOC_PREF)]:,
453+
[(); {meta_num_slots!(A, CO_ALLOC_PREF)}]:,
450454
{
451455
#[inline]
452456
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {

library/std/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@
217217
#![allow(incomplete_features)]
218218
#![feature(generic_const_exprs)]
219219
#![feature(global_co_alloc)]
220+
#![feature(global_co_alloc_default)]
220221
#![feature(global_co_alloc_plvec)]
222+
#![feature(global_co_alloc_meta)]
221223
#![warn(deprecated_in_future)]
222224
#![warn(missing_docs)]
223225
#![warn(missing_debug_implementations)]
@@ -420,6 +422,11 @@ pub mod prelude;
420422
pub use alloc_crate::borrow;
421423
#[stable(feature = "rust1", since = "1.0.0")]
422424
pub use alloc_crate::boxed;
425+
#[unstable(feature = "global_co_alloc", issue = "none")]
426+
pub use alloc_crate::co_alloc;
427+
// @FIXME ugly - someone move this to a better place, please
428+
#[unstable(feature = "global_co_alloc", issue = "none")]
429+
pub use alloc_crate::{CO_ALLOC_PREF_DEFAULT};
423430
#[stable(feature = "rust1", since = "1.0.0")]
424431
pub use alloc_crate::fmt;
425432
#[stable(feature = "rust1", since = "1.0.0")]

library/test/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#![unstable(feature = "test", issue = "50297")]
1818
#![doc(test(attr(deny(warnings))))]
19+
#![feature(allocator_api)]
1920
#![feature(internal_output_capture)]
2021
#![feature(is_terminal)]
2122
#![feature(staged_api)]
@@ -52,6 +53,7 @@ pub mod test {
5253
}
5354

5455
use std::{
56+
alloc::Global,
5557
collections::VecDeque,
5658
env, io,
5759
io::prelude::Write,
@@ -346,7 +348,7 @@ where
346348
};
347349

348350
let mut running_tests: TestMap = HashMap::default();
349-
let mut timeout_queue: VecDeque<TimeoutEntry> = VecDeque::new();
351+
let mut timeout_queue: VecDeque<TimeoutEntry, Global> = VecDeque::new();
350352

351353
fn get_timed_out_tests(
352354
running_tests: &TestMap,

0 commit comments

Comments
 (0)