Skip to content

Commit be8f776

Browse files
Vec to use COOP_PREFERRED
1 parent 313616f commit be8f776

File tree

9 files changed

+61
-33
lines changed

9 files changed

+61
-33
lines changed

library/alloc/src/boxed.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,8 +1683,12 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
16831683

16841684
#[cfg(not(no_global_oom_handling))]
16851685
#[stable(feature = "boxed_array_try_from_vec", since = "1.66.0")]
1686-
impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
1687-
type Error = Vec<T>;
1686+
impl<T, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, Global, COOP_PREFERRED>>
1687+
for Box<[T; N]>
1688+
where
1689+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
1690+
{
1691+
type Error = Vec<T, Global, COOP_PREFERRED>;
16881692

16891693
/// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
16901694
///
@@ -1704,7 +1708,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
17041708
/// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
17051709
/// assert_eq!(state.len(), 100);
17061710
/// ```
1707-
fn try_from(vec: Vec<T>) -> Result<Self, Self::Error> {
1711+
fn try_from(vec: Vec<T, Global, COOP_PREFERRED>) -> Result<Self, Self::Error> {
17081712
if vec.len() == N {
17091713
let boxed_slice = vec.into_boxed_slice();
17101714
Ok(unsafe { boxed_slice_as_array_unchecked(boxed_slice) })

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,15 +1525,15 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
15251525
#[derive(Debug)]
15261526
pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool>
15271527
where
1528-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1528+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
15291529
{
15301530
iter: vec::Drain<'a, T, Global, COOP_PREFERRED>,
15311531
}
15321532

15331533
#[stable(feature = "drain", since = "1.6.0")]
15341534
impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED>
15351535
where
1536-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1536+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
15371537
{
15381538
type Item = T;
15391539

library/alloc/src/raw_vec.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) struct RawVec<
6969

7070
impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED>
7171
where
72-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
72+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
7373
{
7474
/// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so
7575
/// they cannot call `Self::new()`.
@@ -527,20 +527,6 @@ where
527527
}
528528
}
529529

530-
// @FIXME Custom
531-
unsafe impl<#[may_dangle] T, const COOP_PREFERRED: bool> Drop for RawVec<T, Global, COOP_PREFERRED>
532-
where
533-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
534-
{
535-
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
536-
fn drop(&mut self) {
537-
// @TOFIXMEDO
538-
if let Some((ptr, layout)) = self.current_memory() {
539-
unsafe { self.alloc.deallocate(ptr, layout) }
540-
}
541-
}
542-
}
543-
544530
// Central function for reserve error handling.
545531
#[cfg(not(no_global_oom_handling))]
546532
#[inline]

library/alloc/src/rc.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ use crate::boxed::Box;
247247
#[cfg(test)]
248248
use std::boxed::Box;
249249

250+
use crate::alloc;
250251
use core::any::Any;
251252
use core::borrow;
252253
use core::cell::Cell;
@@ -1987,7 +1988,10 @@ impl<T: ?Sized> From<Box<T>> for Rc<T> {
19871988

19881989
#[cfg(not(no_global_oom_handling))]
19891990
#[stable(feature = "shared_from_slice", since = "1.21.0")]
1990-
impl<T> From<Vec<T>> for Rc<[T]> {
1991+
impl<T, const COOP_PREFERRED: bool> From<Vec<T, Global, COOP_PREFERRED>> for Rc<[T]>
1992+
where
1993+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
1994+
{
19911995
/// Allocate a reference-counted slice and move `v`'s items into it.
19921996
///
19931997
/// # Example
@@ -1999,7 +2003,10 @@ impl<T> From<Vec<T>> for Rc<[T]> {
19992003
/// assert_eq!(vec![1, 2, 3], *shared);
20002004
/// ```
20012005
#[inline]
2002-
fn from(mut v: Vec<T>) -> Rc<[T]> {
2006+
fn from(mut v: Vec<T, Global, COOP_PREFERRED>) -> Rc<[T]>
2007+
where
2008+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
2009+
{
20032010
unsafe {
20042011
let rc = Rc::copy_from_slice(&v);
20052012
// Allow the Vec to free its memory, but not destroy its contents
@@ -2120,6 +2127,7 @@ trait ToRcSlice<T>: Iterator<Item = T> + Sized {
21202127
fn to_rc_slice(self) -> Rc<[T]>;
21212128
}
21222129

2130+
// COOP_NOT_POSSIBLE
21232131
#[cfg(not(no_global_oom_handling))]
21242132
impl<T, I: Iterator<Item = T>> ToRcSlice<T> for I {
21252133
default fn to_rc_slice(self) -> Rc<[T]> {

library/alloc/src/slice.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,13 @@ impl<T> [T] {
464464
#[rustc_allow_incoherent_impl]
465465
#[inline]
466466
#[unstable(feature = "allocator_api", issue = "32838")]
467-
pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A>
467+
pub fn to_vec_in<A: Allocator, const COOP_PREFERRED: bool>(
468+
&self,
469+
alloc: A,
470+
) -> Vec<T, A, COOP_PREFERRED>
468471
where
469472
T: Clone,
470-
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
473+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
471474
{
472475
// N.B., see the `hack` module in this file for more details.
473476
hack::to_vec(self, alloc)
@@ -490,9 +493,11 @@ impl<T> [T] {
490493
#[rustc_allow_incoherent_impl]
491494
#[stable(feature = "rust1", since = "1.0.0")]
492495
#[inline]
493-
pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A>
496+
pub fn into_vec<A: Allocator, const COOP_PREFERRED: bool>(
497+
self: Box<Self, A>,
498+
) -> Vec<T, A, COOP_PREFERRED>
494499
where
495-
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
500+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
496501
{
497502
// N.B., see the `hack` module in this file for more details.
498503
hack::into_vec(self)
@@ -737,6 +742,7 @@ pub trait Join<Separator> {
737742
fn join(slice: &Self, sep: Separator) -> Self::Output;
738743
}
739744

745+
// COOP_NOT_POSSIBLE
740746
#[cfg(not(no_global_oom_handling))]
741747
#[unstable(feature = "slice_concat_ext", issue = "27747")]
742748
impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] {
@@ -752,6 +758,7 @@ impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] {
752758
}
753759
}
754760

761+
// COOP_NOT_POSSIBLE
755762
#[cfg(not(no_global_oom_handling))]
756763
#[unstable(feature = "slice_concat_ext", issue = "27747")]
757764
impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] {
@@ -775,10 +782,11 @@ impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] {
775782
}
776783
}
777784

785+
// COOP_NOT_POSSIBLE
778786
#[cfg(not(no_global_oom_handling))]
779787
#[unstable(feature = "slice_concat_ext", issue = "27747")]
780788
impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
781-
type Output = Vec<T>;
789+
type Output = Vec<T, Global>;
782790

783791
fn join(slice: &Self, sep: &[T]) -> Vec<T> {
784792
let mut iter = slice.iter();
@@ -823,6 +831,7 @@ where
823831
}
824832
}
825833

834+
// COOP_NOT_POSSIBLE
826835
#[cfg(not(no_global_oom_handling))]
827836
#[stable(feature = "rust1", since = "1.0.0")]
828837
impl<T: Clone> ToOwned for [T] {

library/alloc/src/str.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use core::ptr;
1414
use core::str::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
1515
use core::unicode::conversions;
1616

17+
use crate::alloc;
18+
use crate::alloc::Global;
1719
use crate::borrow::ToOwned;
1820
use crate::boxed::Box;
1921
use crate::slice::{Concat, Join, SliceIndex};
@@ -126,11 +128,15 @@ macro_rules! copy_slice_and_advance {
126128
// [T] and str both impl AsRef<[T]> for some T
127129
// => s.borrow().as_ref() and we always have slices
128130
#[cfg(not(no_global_oom_handling))]
129-
fn join_generic_copy<B, T, S>(slice: &[S], sep: &[T]) -> Vec<T>
131+
fn join_generic_copy<B, T, S, const COOP_PREFERRED: bool>(
132+
slice: &[S],
133+
sep: &[T],
134+
) -> Vec<T, Global, COOP_PREFERRED>
130135
where
131136
T: Copy,
132137
B: AsRef<[T]> + ?Sized,
133138
S: Borrow<B>,
139+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
134140
{
135141
let sep_len = sep.len();
136142
let mut iter = slice.iter();

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ pub struct Vec<
408408
len: usize,
409409
}
410410

411+
/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter.
412+
#[unstable(feature = "global_co_alloc_def", issue = "none")]
413+
pub const DEFAULT_COOP_PREFERRED: bool = true;
414+
411415
#[unstable(feature = "global_co_alloc_covec", issue = "none")]
412416
pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> =
413417
Vec<T, A, true>;
@@ -417,10 +421,6 @@ pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: All
417421
pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> =
418422
Vec<T, A, false>;
419423

420-
/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter.
421-
#[unstable(feature = "global_co_alloc_def", issue = "none")]
422-
pub const DEFAULT_COOP_PREFERRED: bool = true;
423-
424424
/// "Default" Vec. Either "cooperative" or not - as specified by `DEFAULT_COOP_PREFERRED`. The
425425
/// difference to `Vec` (used without specifying `COOP_PREFERRED`): `DefVec` indicates that the
426426
/// author considered using `CoVec` or `PlVec`, but left it to default instead.

library/core/src/alloc/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>(
105105
if A::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 }
106106
}
107107

108+
#[unstable(feature = "global_co_alloc", issue = "none")]
109+
/// Param `coop_preferred` - if false, then this returns `0`, regardless of
110+
/// whether allocator `A` is cooperative.
111+
pub const fn co_alloc_metadata_num_slots_with_preference_global(_coop_preferred: bool) -> usize {
112+
if true {
113+
panic!("FIXME");
114+
}
115+
// @FIXME Move these functions to :alloc instead.
116+
// Then:
117+
0
118+
//if Global::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 }
119+
}
120+
108121
/// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of
109122
/// data described via [`Layout`][].
110123
///

library/std/src/sys_common/thread_local_dtor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
1313
#![unstable(feature = "thread_local_internals", issue = "none")]
1414
#![allow(dead_code)]
15+
#![feature(global_co_alloc_plvec)]
1516

1617
use crate::ptr;
1718
use crate::sys_common::thread_local_key::StaticKey;
19+
use alloc::vec::PlVec;
1820

1921
pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
2022
// The fallback implementation uses a vanilla OS-based TLS key to track
@@ -28,7 +30,7 @@ pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut
2830
// flagged for destruction.
2931

3032
static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
31-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
33+
type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
3234
if DTORS.get().is_null() {
3335
let v: Box<List> = box Vec::new();
3436
DTORS.set(Box::into_raw(v) as *mut u8);

0 commit comments

Comments
 (0)