Skip to content

Commit 8c30d3b

Browse files
Vec to use COOP_PREFERRED
1 parent cd24c06 commit 8c30d3b

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.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,15 +1480,15 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
14801480
#[derive(Debug)]
14811481
pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool>
14821482
where
1483-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1483+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
14841484
{
14851485
iter: vec::Drain<'a, T, Global, COOP_PREFERRED>,
14861486
}
14871487

14881488
#[stable(feature = "drain", since = "1.6.0")]
14891489
impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED>
14901490
where
1491-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1491+
[(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
14921492
{
14931493
type Item = T;
14941494

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
@@ -461,10 +461,13 @@ impl<T> [T] {
461461
#[rustc_allow_incoherent_impl]
462462
#[inline]
463463
#[unstable(feature = "allocator_api", issue = "32838")]
464-
pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A>
464+
pub fn to_vec_in<A: Allocator, const COOP_PREFERRED: bool>(
465+
&self,
466+
alloc: A,
467+
) -> Vec<T, A, COOP_PREFERRED>
465468
where
466469
T: Clone,
467-
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
470+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
468471
{
469472
// N.B., see the `hack` module in this file for more details.
470473
hack::to_vec(self, alloc)
@@ -487,9 +490,11 @@ impl<T> [T] {
487490
#[rustc_allow_incoherent_impl]
488491
#[stable(feature = "rust1", since = "1.0.0")]
489492
#[inline]
490-
pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A>
493+
pub fn into_vec<A: Allocator, const COOP_PREFERRED: bool>(
494+
self: Box<Self, A>,
495+
) -> Vec<T, A, COOP_PREFERRED>
491496
where
492-
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
497+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
493498
{
494499
// N.B., see the `hack` module in this file for more details.
495500
hack::into_vec(self)
@@ -734,6 +739,7 @@ pub trait Join<Separator> {
734739
fn join(slice: &Self, sep: Separator) -> Self::Output;
735740
}
736741

742+
// COOP_NOT_POSSIBLE
737743
#[cfg(not(no_global_oom_handling))]
738744
#[unstable(feature = "slice_concat_ext", issue = "27747")]
739745
impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] {
@@ -749,6 +755,7 @@ impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] {
749755
}
750756
}
751757

758+
// COOP_NOT_POSSIBLE
752759
#[cfg(not(no_global_oom_handling))]
753760
#[unstable(feature = "slice_concat_ext", issue = "27747")]
754761
impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] {
@@ -772,10 +779,11 @@ impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] {
772779
}
773780
}
774781

782+
// COOP_NOT_POSSIBLE
775783
#[cfg(not(no_global_oom_handling))]
776784
#[unstable(feature = "slice_concat_ext", issue = "27747")]
777785
impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
778-
type Output = Vec<T>;
786+
type Output = Vec<T, Global>;
779787

780788
fn join(slice: &Self, sep: &[T]) -> Vec<T> {
781789
let mut iter = slice.iter();
@@ -820,6 +828,7 @@ where
820828
}
821829
}
822830

831+
// COOP_NOT_POSSIBLE
823832
#[cfg(not(no_global_oom_handling))]
824833
#[stable(feature = "rust1", since = "1.0.0")]
825834
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)