From 74c8a3f523cb2b450238a3bf3554931a174f0a1a Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sat, 10 Dec 2022 00:47:21 -0800 Subject: [PATCH 01/47] Global alloc; GlobalCoAllocMeta; type size tests --- compiler/rustc_ast/src/ast.rs | 24 ++++---- compiler/rustc_ast/src/lib.rs | 1 + compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_middle/src/mir/mod.rs | 3 +- compiler/rustc_middle/src/mir/syntax.rs | 4 +- compiler/rustc_parse/src/lib.rs | 1 + .../rustc_parse/src/parser/attr_wrapper.rs | 4 +- compiler/rustc_parse/src/parser/mod.rs | 3 +- compiler/rustc_trait_selection/src/lib.rs | 1 + .../src/traits/fulfill.rs | 4 +- library/alloc/src/lib.rs | 2 + library/alloc/src/raw_vec.rs | 19 ++++++- library/core/src/alloc/global.rs | 56 ++++++++++++++++++- library/core/src/alloc/mod.rs | 11 ++++ src/librustdoc/clean/types.rs | 10 ++-- src/librustdoc/html/render/context.rs | 4 +- src/librustdoc/lib.rs | 1 + 17 files changed, 123 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 9317579f70dd5..af7a9bd2123b3 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -25,6 +25,7 @@ pub use UnsafeSource::*; use crate::ptr::P; use crate::token::{self, CommentKind, Delimiter}; use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, TokenStream}; +use core::alloc::GlobalCoAllocMeta; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::sync::Lrc; @@ -36,7 +37,6 @@ use rustc_span::{Span, DUMMY_SP}; use std::fmt; use std::mem; use thin_vec::{thin_vec, ThinVec}; - /// A "Label" is an identifier of some point in sources, /// e.g. in the following code: /// @@ -3100,26 +3100,26 @@ mod size_asserts { static_assert_size!(AssocItem, 104); static_assert_size!(AssocItemKind, 32); static_assert_size!(Attribute, 32); - static_assert_size!(Block, 48); - static_assert_size!(Expr, 72); - static_assert_size!(ExprKind, 40); - static_assert_size!(Fn, 184); + static_assert_size!(Block, 48 + mem::size_of::<GlobalCoAllocMeta>()); + static_assert_size!(Expr, 72 + mem::size_of::<GlobalCoAllocMeta>()); + static_assert_size!(ExprKind, 40 + mem::size_of::<GlobalCoAllocMeta>()); + static_assert_size!(Fn, 184 + 2 * mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(ForeignItem, 96); static_assert_size!(ForeignItemKind, 24); static_assert_size!(GenericArg, 24); - static_assert_size!(GenericBound, 72); - static_assert_size!(Generics, 72); - static_assert_size!(Impl, 184); - static_assert_size!(Item, 184); - static_assert_size!(ItemKind, 112); + static_assert_size!(GenericBound, 72 + mem::size_of::<GlobalCoAllocMeta>()); + static_assert_size!(Generics, 72 + 2 * mem::size_of::<GlobalCoAllocMeta>()); + static_assert_size!(Impl, 184 + 3 * mem::size_of::<GlobalCoAllocMeta>()); + static_assert_size!(Item, 184 + 3 * mem::size_of::<GlobalCoAllocMeta>()); + static_assert_size!(ItemKind, 112 + 3 * mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(LitKind, 24); static_assert_size!(Local, 72); static_assert_size!(MetaItemLit, 40); static_assert_size!(Param, 40); - static_assert_size!(Pat, 88); + static_assert_size!(Pat, 88 + mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(Path, 24); static_assert_size!(PathSegment, 24); - static_assert_size!(PatKind, 64); + static_assert_size!(PatKind, 64 + mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(Stmt, 32); static_assert_size!(StmtKind, 16); static_assert_size!(Ty, 64); diff --git a/compiler/rustc_ast/src/lib.rs b/compiler/rustc_ast/src/lib.rs index 9c1dfeb1a6142..cd7242449a72d 100644 --- a/compiler/rustc_ast/src/lib.rs +++ b/compiler/rustc_ast/src/lib.rs @@ -12,6 +12,7 @@ #![feature(box_patterns)] #![feature(const_default_impls)] #![feature(const_trait_impl)] +#![feature(global_co_alloc_meta)] #![feature(if_let_guard)] #![feature(let_chains)] #![feature(min_specialization)] diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 7e4063c2ffd78..7e05e4702d55d 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -32,6 +32,7 @@ #![feature(exhaustive_patterns)] #![feature(generators)] #![feature(get_mut_unchecked)] +#![feature(global_co_alloc_meta)] #![feature(if_let_guard)] #![feature(iter_from_generator)] #![feature(negative_impls)] diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index e52b243ecf635..5cab90c4cc3b5 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2,6 +2,7 @@ //! //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html +use core::alloc::GlobalCoAllocMeta; use crate::mir::interpret::{ AllocRange, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, LitToConstInput, Scalar, }; @@ -3060,7 +3061,7 @@ mod size_asserts { use super::*; use rustc_data_structures::static_assert_size; // tidy-alphabetical-start - static_assert_size!(BasicBlockData<'_>, 144); + static_assert_size!(BasicBlockData<'_>, 144 + mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(LocalDecl<'_>, 56); static_assert_size!(Statement<'_>, 32); static_assert_size!(StatementKind<'_>, 16); diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 52c2b10cbbea9..cfaecf0775ff4 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -3,6 +3,8 @@ //! This is in a dedicated file so that changes to this file can be reviewed more carefully. //! The intention is that this file only contains datatype declarations, no code. +use core::alloc::GlobalCoAllocMeta; +use core::mem; use super::{BasicBlock, Constant, Field, Local, SwitchTargets, UserTypeProjection}; use crate::mir::coverage::{CodeRegion, CoverageKind}; @@ -1278,6 +1280,6 @@ mod size_asserts { static_assert_size!(Operand<'_>, 24); static_assert_size!(Place<'_>, 16); static_assert_size!(PlaceElem<'_>, 24); - static_assert_size!(Rvalue<'_>, 40); + static_assert_size!(Rvalue<'_>, 40 + mem::size_of::<GlobalCoAllocMeta>()); // tidy-alphabetical-end } diff --git a/compiler/rustc_parse/src/lib.rs b/compiler/rustc_parse/src/lib.rs index b49a01d75ed54..1ba1049b0bc80 100644 --- a/compiler/rustc_parse/src/lib.rs +++ b/compiler/rustc_parse/src/lib.rs @@ -2,6 +2,7 @@ #![feature(array_windows)] #![feature(box_patterns)] +#![feature(global_co_alloc_meta)] #![feature(if_let_guard)] #![feature(iter_intersperse)] #![feature(let_chains)] diff --git a/compiler/rustc_parse/src/parser/attr_wrapper.rs b/compiler/rustc_parse/src/parser/attr_wrapper.rs index b97f22417cb7b..a8c2d8c2c59d3 100644 --- a/compiler/rustc_parse/src/parser/attr_wrapper.rs +++ b/compiler/rustc_parse/src/parser/attr_wrapper.rs @@ -1,3 +1,5 @@ +use core::alloc::GlobalCoAllocMeta; +use core::mem; use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCursor, TrailingToken}; use rustc_ast::token::{self, Delimiter, Token, TokenKind}; use rustc_ast::tokenstream::{AttrTokenStream, AttributesData, ToAttrTokenStream}; @@ -469,6 +471,6 @@ mod size_asserts { use rustc_data_structures::static_assert_size; // tidy-alphabetical-start static_assert_size!(AttrWrapper, 16); - static_assert_size!(LazyAttrTokenStreamImpl, 144); + static_assert_size!(LazyAttrTokenStreamImpl, 144 + mem::size_of::<GlobalCoAllocMeta>()); // tidy-alphabetical-end } diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index ffb23b50a160d..e8567a650d85f 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -11,6 +11,7 @@ mod stmt; mod ty; use crate::lexer::UnmatchedBrace; +use core::alloc::GlobalCoAllocMeta; pub use attr_wrapper::AttrWrapper; pub use diagnostics::AttemptLocalParseRecovery; pub(crate) use item::FnParseMode; @@ -168,7 +169,7 @@ pub struct Parser<'a> { // This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure // it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Parser<'_>, 336); +rustc_data_structures::static_assert_size!(Parser<'_>, 336 + 4 * mem::size_of::<GlobalCoAllocMeta>()); /// Stores span information about a closure. #[derive(Clone)] diff --git a/compiler/rustc_trait_selection/src/lib.rs b/compiler/rustc_trait_selection/src/lib.rs index 6fa0941036390..d35cf93347b6e 100644 --- a/compiler/rustc_trait_selection/src/lib.rs +++ b/compiler/rustc_trait_selection/src/lib.rs @@ -15,6 +15,7 @@ #![feature(box_patterns)] #![feature(control_flow_enum)] #![feature(drain_filter)] +#![feature(global_co_alloc_meta)] #![feature(hash_drain_filter)] #![feature(let_chains)] #![feature(if_let_guard)] diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index 76a755ed9e09d..378419868c427 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -1,4 +1,6 @@ use crate::infer::{InferCtxt, TyOrConstInferVar}; +use core::alloc::GlobalCoAllocMeta; +use core::mem; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::obligation_forest::ProcessResult; use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome}; @@ -80,7 +82,7 @@ pub struct PendingPredicateObligation<'tcx> { // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(PendingPredicateObligation<'_>, 72); +static_assert_size!(PendingPredicateObligation<'_>, 72 + mem::size_of::<GlobalCoAllocMeta>()); impl<'a, 'tcx> FulfillmentContext<'tcx> { /// Creates a new fulfillment context. diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index ca75c3895f41f..3e6ab0cfcbd64 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -123,6 +123,7 @@ #![feature(extend_one)] #![feature(fmt_internals)] #![feature(fn_traits)] +#![feature(global_co_alloc_meta)] #![feature(hasher_prefixfree_extras)] #![feature(inline_const)] #![feature(inplace_iteration)] @@ -178,6 +179,7 @@ #![feature(exclusive_range_pattern)] #![feature(fundamental)] #![cfg_attr(not(test), feature(generator_trait))] +#![feature(global_co_alloc)] #![feature(hashmap_internals)] #![feature(lang_items)] #![feature(min_specialization)] diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 5a10121bbbe4b..9129d3f0f7cab 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -1,6 +1,6 @@ #![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")] -use core::alloc::LayoutError; +use core::alloc::{LayoutError, GlobalCoAllocMeta}; use core::cmp; use core::intrinsics; use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; @@ -53,6 +53,8 @@ pub(crate) struct RawVec<T, A: Allocator = Global> { ptr: Unique<T>, cap: usize, alloc: A, + #[allow(dead_code)] + pub(crate) meta: GlobalCoAllocMeta, } impl<T> RawVec<T, Global> { @@ -120,7 +122,7 @@ impl<T, A: Allocator> RawVec<T, A> { /// the returned `RawVec`. pub const fn new_in(alloc: A) -> Self { // `cap: 0` means "unallocated". zero-sized types are ignored. - Self { ptr: Unique::dangling(), cap: 0, alloc } + Self { ptr: Unique::dangling(), cap: 0, alloc, meta: GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/} } } /// Like `with_capacity`, but parameterized over the choice of @@ -197,6 +199,7 @@ impl<T, A: Allocator> RawVec<T, A> { ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) }, cap: capacity, alloc, + meta: GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/} } } } @@ -213,7 +216,7 @@ impl<T, A: Allocator> RawVec<T, A> { /// guaranteed. #[inline] pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { - Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc } + Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc, meta: GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/} } } /// Gets a raw pointer to the start of the allocation. Note that this is @@ -473,8 +476,18 @@ where } unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> { + /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. + default fn drop(&mut self) { + if let Some((ptr, layout)) = self.current_memory() { + unsafe { self.alloc.deallocate(ptr, layout) } + } + } +} + +unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { + // @TODO if let Some((ptr, layout)) = self.current_memory() { unsafe { self.alloc.deallocate(ptr, layout) } } diff --git a/library/core/src/alloc/global.rs b/library/core/src/alloc/global.rs index 1d80b8bf9ec76..030e1bc110c5e 100644 --- a/library/core/src/alloc/global.rs +++ b/library/core/src/alloc/global.rs @@ -1,6 +1,15 @@ use crate::alloc::Layout; use crate::cmp; use crate::ptr; +use crate::alloc::GlobalCoAllocMeta; + +#[unstable(feature = "global_co_alloc_meta", issue = "none")] +#[allow(missing_debug_implementations)] +/// Used for parameters and results (to/from `GlobalCoAllocator`'s functions, where applicable). +pub struct PtrAndMeta { + pub ptr: *mut u8, + pub meta: GlobalCoAllocMeta, +} /// A memory allocator that can be registered as the standard library’s default /// through the `#[global_allocator]` attribute. @@ -156,6 +165,9 @@ pub unsafe trait GlobalAlloc { #[stable(feature = "global_alloc", since = "1.28.0")] unsafe fn alloc(&self, layout: Layout) -> *mut u8; + #[unstable(feature = "global_co_alloc", issue = "none")] + unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut PtrAndMeta) {panic!("TODO")} + /// Deallocate the block of memory at the given `ptr` pointer with the given `layout`. /// /// # Safety @@ -171,6 +183,9 @@ pub unsafe trait GlobalAlloc { #[stable(feature = "global_alloc", since = "1.28.0")] unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout); + #[unstable(feature = "global_co_alloc", issue = "none")] + unsafe fn co_dealloc(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("TODO")} + /// Behaves like `alloc`, but also ensures that the contents /// are set to zero before being returned. /// @@ -198,11 +213,23 @@ pub unsafe trait GlobalAlloc { if !ptr.is_null() { // SAFETY: as allocation succeeded, the region from `ptr` // of size `size` is guaranteed to be valid for writes. - unsafe { ptr::write_bytes(ptr, 0, size) }; + unsafe { ptr::write_bytes(ptr, 0u8, size) }; } ptr } + #[unstable(feature = "global_co_alloc", issue = "none")] + unsafe fn co_alloc_zeroed(&self, layout: Layout, mut result: &mut PtrAndMeta) { + let size = layout.size(); + // SAFETY: the safety contract for `alloc` must be upheld by the caller. + unsafe { self.co_alloc(layout, &mut result) }; + if !result.ptr.is_null() { + // SAFETY: as allocation succeeded, the region from `ptr_and_meta.ptr` of size `size` is + // guaranteed to be valid for writes. + unsafe { ptr::write_bytes(result.ptr, 0u8, size) }; + } + } + /// Shrink or grow a block of memory to the given `new_size`. /// The block is described by the given `ptr` pointer and `layout`. /// @@ -274,4 +301,31 @@ pub unsafe trait GlobalAlloc { } new_ptr } + + #[unstable(feature = "global_co_alloc", issue = "none")] + unsafe fn co_realloc( + &self, + ptr_and_meta: PtrAndMeta, + layout: Layout, + new_size: usize, + mut result: &mut PtrAndMeta + ) { + // SAFETY: the caller must ensure that the `new_size` does not overflow. + // `layout.align()` comes from a `Layout` and is thus guaranteed to be valid. + let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; + // SAFETY: the caller must ensure that `new_layout` is greater than zero. + unsafe { self.co_alloc(new_layout, &mut result) }; + if !result.ptr.is_null() { + // SAFETY: the previously allocated block cannot overlap the newly allocated block. + // The safety contract for `dealloc` must be upheld by the caller. + unsafe { + ptr::copy_nonoverlapping( + ptr_and_meta.ptr, + result.ptr, + cmp::min(layout.size(), new_size), + ); + self.co_dealloc(ptr_and_meta, layout); + } + } + } } diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index a6082455fac8d..1fce32a05cb94 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -3,6 +3,7 @@ #![stable(feature = "alloc_module", since = "1.28.0")] mod global; +//mod global_co; mod layout; #[stable(feature = "global_alloc", since = "1.28.0")] @@ -25,6 +26,16 @@ use crate::error::Error; use crate::fmt; use crate::ptr::{self, NonNull}; +// @TODO Make this target-specific +#[unstable(feature = "global_co_alloc_meta", issue = "none")] +#[allow(missing_debug_implementations)] +pub struct GlobalCoAllocMeta { + //pub one: usize, + /*pub two: usize, + pub three: usize, + pub four: usize,*/ +} + /// The `AllocError` error indicates an allocation failure /// that may be due to resource exhaustion or to /// something wrong when combining the given input arguments with this diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 87de41fde63c5..33fc804968883 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1,6 +1,8 @@ +use std::alloc::GlobalCoAllocMeta; use std::cell::RefCell; use std::default::Default; use std::hash::Hash; +use std::mem; use std::path::PathBuf; use std::rc::Rc; use std::sync::Arc; @@ -2594,13 +2596,13 @@ mod size_asserts { // tidy-alphabetical-start static_assert_size!(Crate, 64); // frequently moved by-value static_assert_size!(DocFragment, 32); - static_assert_size!(GenericArg, 32); + static_assert_size!(GenericArg, 32 + mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(GenericArgs, 32); - static_assert_size!(GenericParamDef, 56); + static_assert_size!(GenericParamDef, 56 + mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(Generics, 16); static_assert_size!(Item, 56); - static_assert_size!(ItemKind, 64); + static_assert_size!(ItemKind, 64 + mem::size_of::<GlobalCoAllocMeta>()); static_assert_size!(PathSegment, 40); - static_assert_size!(Type, 32); + static_assert_size!(Type, 32 + mem::size_of::<GlobalCoAllocMeta>()); // tidy-alphabetical-end } diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 5cefe9475e775..8e2d67eb173d1 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -1,6 +1,8 @@ +use std::alloc::GlobalCoAllocMeta; use std::cell::RefCell; use std::collections::BTreeMap; use std::io; +use std::mem; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::sync::mpsc::{channel, Receiver}; @@ -75,7 +77,7 @@ pub(crate) struct Context<'tcx> { // `Context` is cloned a lot, so we don't want the size to grow unexpectedly. #[cfg(all(not(windows), target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Context<'_>, 160); +rustc_data_structures::static_assert_size!(Context<'_>, 160 + 2 * mem::size_of::<GlobalCoAllocMeta>()); /// Shared mutable state used in [`Context`] and elsewhere. pub(crate) struct SharedContext<'tcx> { diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 86454e1f2eb73..3a2eaab102b6b 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -8,6 +8,7 @@ #![feature(box_patterns)] #![feature(control_flow_enum)] #![feature(drain_filter)] +#![feature(global_co_alloc_meta)] #![feature(is_terminal)] #![feature(let_chains)] #![feature(test)] From 6092fff1432cd52ea2eff23987bc995c545cb3f3 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sat, 10 Dec 2022 10:44:27 -0800 Subject: [PATCH 02/47] Co-allocating functions for GlobalAlloc --- library/core/src/alloc/global.rs | 12 +-- library/core/src/alloc/mod.rs | 129 +++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 6 deletions(-) diff --git a/library/core/src/alloc/global.rs b/library/core/src/alloc/global.rs index 030e1bc110c5e..fd47d6cf88025 100644 --- a/library/core/src/alloc/global.rs +++ b/library/core/src/alloc/global.rs @@ -6,7 +6,7 @@ use crate::alloc::GlobalCoAllocMeta; #[unstable(feature = "global_co_alloc_meta", issue = "none")] #[allow(missing_debug_implementations)] /// Used for parameters and results (to/from `GlobalCoAllocator`'s functions, where applicable). -pub struct PtrAndMeta { +pub struct RawAndMeta { pub ptr: *mut u8, pub meta: GlobalCoAllocMeta, } @@ -166,7 +166,7 @@ pub unsafe trait GlobalAlloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8; #[unstable(feature = "global_co_alloc", issue = "none")] - unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut PtrAndMeta) {panic!("TODO")} + unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) {panic!("TODO")} /// Deallocate the block of memory at the given `ptr` pointer with the given `layout`. /// @@ -184,7 +184,7 @@ pub unsafe trait GlobalAlloc { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout); #[unstable(feature = "global_co_alloc", issue = "none")] - unsafe fn co_dealloc(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("TODO")} + unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) {panic!("TODO")} /// Behaves like `alloc`, but also ensures that the contents /// are set to zero before being returned. @@ -219,7 +219,7 @@ pub unsafe trait GlobalAlloc { } #[unstable(feature = "global_co_alloc", issue = "none")] - unsafe fn co_alloc_zeroed(&self, layout: Layout, mut result: &mut PtrAndMeta) { + unsafe fn co_alloc_zeroed(&self, layout: Layout, mut result: &mut RawAndMeta) { let size = layout.size(); // SAFETY: the safety contract for `alloc` must be upheld by the caller. unsafe { self.co_alloc(layout, &mut result) }; @@ -305,10 +305,10 @@ pub unsafe trait GlobalAlloc { #[unstable(feature = "global_co_alloc", issue = "none")] unsafe fn co_realloc( &self, - ptr_and_meta: PtrAndMeta, + ptr_and_meta: RawAndMeta, layout: Layout, new_size: usize, - mut result: &mut PtrAndMeta + mut result: &mut RawAndMeta ) { // SAFETY: the caller must ensure that the `new_size` does not overflow. // `layout.align()` comes from a `Layout` and is thus guaranteed to be valid. diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 1fce32a05cb94..18bf6a92e9eca 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -59,6 +59,25 @@ impl fmt::Display for AllocError { } } +#[unstable(feature = "global_co_alloc_meta", issue = "none")] +#[allow(missing_debug_implementations)] +pub struct PtrAndMeta { + pub ptr: NonNull<u8>, + pub meta: GlobalCoAllocMeta, +} + +#[unstable(feature = "global_co_alloc_meta", issue = "none")] +#[allow(missing_debug_implementations)] +/// Used for results (from `CoAllocator`'s functions, where applicable). +pub struct SliceAndMeta { + pub slice: NonNull<[u8]>, + pub meta: GlobalCoAllocMeta, +} + +#[unstable(feature = "global_co_alloc_meta", issue = "none")] +#[allow(missing_debug_implementations)] +pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>; + /// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of /// data described via [`Layout`][]. /// @@ -140,6 +159,8 @@ pub unsafe trait Allocator { /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>; + fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) {panic!("TODO")} + /// Behaves like `allocate`, but also ensures that the returned memory is zero-initialized. /// /// # Errors @@ -162,6 +183,19 @@ pub unsafe trait Allocator { Ok(ptr) } + fn co_allocate_zeroed(&self, layout: Layout, mut result: &mut SliceAndMetaResult) { + self.co_allocate(layout, &mut result); + if let Ok(SliceAndMeta{slice, ..}) = result { + // SAFETY: `alloc` returns a valid memory block + unsafe { + slice + .as_non_null_ptr() + .as_ptr() + .write_bytes(0, slice.len()) + } + } + } + /// Deallocates the memory referenced by `ptr`. /// /// # Safety @@ -173,6 +207,8 @@ pub unsafe trait Allocator { /// [*fit*]: #memory-fitting unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout); + unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("TODO")} + /// Attempts to extend the memory block. /// /// Returns a new [`NonNull<[u8]>`][NonNull] containing a pointer and the actual size of the allocated @@ -237,6 +273,37 @@ pub unsafe trait Allocator { Ok(new_ptr) } + unsafe fn co_grow( + &self, + ptr_and_meta: PtrAndMeta, + old_layout: Layout, + new_layout: Layout, + mut result: &mut SliceAndMetaResult + ) { + debug_assert!( + new_layout.size() >= old_layout.size(), + "`new_layout.size()` must be greater than or equal to `old_layout.size()`" + ); + + self.co_allocate(new_layout, &mut result); + + if let Ok(SliceAndMeta {slice, ..}) = result { + // SAFETY: because `new_layout.size()` must be greater than or equal to + // `old_layout.size()`, both the old and new memory allocation are valid for reads and + // writes for `old_layout.size()` bytes. Also, because the old allocation wasn't yet + // deallocated, it cannot overlap `new_slice_and_meta.slice`. Thus, the call to `copy_nonoverlapping` is + // safe. The safety contract for `dealloc` must be upheld by the caller. + unsafe { + ptr::copy_nonoverlapping( + ptr_and_meta.ptr.as_ptr(), + slice.as_mut_ptr(), + old_layout.size(), + ); + self.co_deallocate(ptr_and_meta, old_layout); + } + } + } + /// Behaves like `grow`, but also ensures that the new contents are set to zero before being /// returned. /// @@ -300,6 +367,37 @@ pub unsafe trait Allocator { Ok(new_ptr) } + unsafe fn co_grow_zeroed( + &self, + ptr_and_meta: PtrAndMeta, + old_layout: Layout, + new_layout: Layout, + mut result: &mut SliceAndMetaResult + ) { + debug_assert!( + new_layout.size() >= old_layout.size(), + "`new_layout.size()` must be greater than or equal to `old_layout.size()`" + ); + + self.co_allocate_zeroed(new_layout, &mut result); + + if let Ok(SliceAndMeta{ slice, ..}) = result { + // SAFETY: because `new_layout.size()` must be greater than or equal to + // `old_layout.size()`, both the old and new memory allocation are valid for reads and + // writes for `old_layout.size()` bytes. Also, because the old allocation wasn't yet + // deallocated, it cannot overlap `new_slice_and_meta.slice`. Thus, the call to `copy_nonoverlapping` is + // safe. The safety contract for `dealloc` must be upheld by the caller. + unsafe { + ptr::copy_nonoverlapping( + ptr_and_meta.ptr.as_ptr(), + slice.as_mut_ptr(), + old_layout.size(), + ); + self.co_deallocate(ptr_and_meta, old_layout); + } + } + } + /// Attempts to shrink the memory block. /// /// Returns a new [`NonNull<[u8]>`][NonNull] containing a pointer and the actual size of the allocated @@ -364,6 +462,37 @@ pub unsafe trait Allocator { Ok(new_ptr) } + unsafe fn co_shrink( + &self, + ptr_and_meta: PtrAndMeta, + old_layout: Layout, + new_layout: Layout, + mut result: &mut SliceAndMetaResult + ) { + debug_assert!( + new_layout.size() <= old_layout.size(), + "`new_layout.size()` must be smaller than or equal to `old_layout.size()`" + ); + + self.co_allocate(new_layout, &mut result); + + if let Ok(SliceAndMeta{ slice, ..}) = result { + // SAFETY: because `new_layout.size()` must be lower than or equal to + // `old_layout.size()`, both the old and new memory allocation are valid for reads and + // writes for `new_layout.size()` bytes. Also, because the old allocation wasn't yet + // deallocated, it cannot overlap `new_slice_and_meta.slice`. Thus, the call to `copy_nonoverlapping` is + // safe. The safety contract for `dealloc` must be upheld by the caller. + unsafe { + ptr::copy_nonoverlapping( + ptr_and_meta.ptr.as_ptr(), + slice.as_mut_ptr(), + new_layout.size(), + ); + self.co_deallocate(ptr_and_meta, old_layout); + } + } + } + /// Creates a "by reference" adapter for this instance of `Allocator`. /// /// The returned adapter also implements `Allocator` and will simply borrow this. From 066da2a77852eea8936747441189c602940712fd Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sat, 10 Dec 2022 10:53:39 -0800 Subject: [PATCH 03/47] RawVec includes GlobalCoAllocMeta based on allocator; related bounds --- .../alloc/src/collections/vec_deque/macros.rs | 1 + .../alloc/src/collections/vec_deque/mod.rs | 72 +++++++++++------ library/alloc/src/lib.rs | 2 + library/alloc/src/raw_vec.rs | 44 +++++++---- library/alloc/src/slice.rs | 25 ++++-- library/alloc/src/vec/mod.rs | 78 ++++++++++++++----- library/alloc/tests/boxed.rs | 1 + library/core/src/alloc/mod.rs | 13 ++++ library/core/src/lib.rs | 1 + library/std/src/alloc.rs | 3 + 10 files changed, 175 insertions(+), 65 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/macros.rs b/library/alloc/src/collections/vec_deque/macros.rs index 5c7913073fe87..f7768f54c6225 100644 --- a/library/alloc/src/collections/vec_deque/macros.rs +++ b/library/alloc/src/collections/vec_deque/macros.rs @@ -4,6 +4,7 @@ macro_rules! __impl_slice_eq1 { impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs where T: PartialEq<U>, + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, $($constraints)* { fn eq(&self, other: &$rhs) -> bool { diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 451e4936bc50e..f76cbb0b17904 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -5,8 +5,10 @@ //! are not required to be copyable, and the queue will be sendable if the //! contained type is sendable. -#![stable(feature = "rust1", since = "1.0.0")] +#![feature(global_co_alloc)] +#![stable(feature = "rust1", since = "1.0.0")] +use core::alloc; use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; @@ -91,10 +93,13 @@ mod tests; #[cfg_attr(not(test), rustc_diagnostic_item = "VecDeque")] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_insignificant_dtor] +// @TODO pub struct VecDeque< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, -> { +> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ // `self[0]`, if it exists, is `buf[head]`. // `head < buf.capacity()`, unless `buf.capacity() == 0` when `head == 0`. head: usize, @@ -106,7 +111,8 @@ pub struct VecDeque< } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> { +impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn clone(&self) -> Self { let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone()); deq.extend(self.iter().cloned()); @@ -120,7 +126,8 @@ impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> { +unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { /// Runs the destructor for all items in the slice when it gets dropped (normally or /// during unwinding). @@ -153,7 +160,8 @@ impl<T> Default for VecDeque<T> { } } -impl<T, A: Allocator> VecDeque<T, A> { +impl<T, A: Allocator> VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Marginally more convenient #[inline] fn ptr(&self) -> *mut T { @@ -442,7 +450,8 @@ impl<T, A: Allocator> VecDeque<T, A> { mut iter: impl Iterator<Item = T>, len: usize, ) -> usize { - struct Guard<'a, T, A: Allocator> { + struct Guard<'a, T, A: Allocator> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { deque: &'a mut VecDeque<T, A>, written: usize, } @@ -561,7 +570,8 @@ impl<T> VecDeque<T> { } } -impl<T, A: Allocator> VecDeque<T, A> { +impl<T, A: Allocator> VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Creates an empty deque. /// /// # Examples @@ -2596,7 +2606,8 @@ impl<T, A: Allocator> VecDeque<T, A> { } } -impl<T: Clone, A: Allocator> VecDeque<T, A> { +impl<T: Clone, A: Allocator> VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Modifies the deque in-place so that `len()` is equal to new_len, /// either by removing excess elements from the back or by appending clones of `value` /// to the back. @@ -2641,7 +2652,8 @@ fn wrap_index(logical_index: usize, capacity: usize) -> usize { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialEq, A: Allocator> PartialEq for VecDeque<T, A> { +impl<T: PartialEq, A: Allocator> PartialEq for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn eq(&self, other: &Self) -> bool { if self.len != other.len() { return false; @@ -2680,7 +2692,8 @@ impl<T: PartialEq, A: Allocator> PartialEq for VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Eq, A: Allocator> Eq for VecDeque<T, A> {} +impl<T: Eq, A: Allocator> Eq for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} __impl_slice_eq1! { [] VecDeque<T, A>, Vec<U, A>, } __impl_slice_eq1! { [] VecDeque<T, A>, &[U], } @@ -2690,14 +2703,16 @@ __impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &[U; N], } __impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &mut [U; N], } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialOrd, A: Allocator> PartialOrd for VecDeque<T, A> { +impl<T: PartialOrd, A: Allocator> PartialOrd for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.iter().partial_cmp(other.iter()) } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Ord, A: Allocator> Ord for VecDeque<T, A> { +impl<T: Ord, A: Allocator> Ord for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn cmp(&self, other: &Self) -> Ordering { self.iter().cmp(other.iter()) @@ -2705,7 +2720,8 @@ impl<T: Ord, A: Allocator> Ord for VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Hash, A: Allocator> Hash for VecDeque<T, A> { +impl<T: Hash, A: Allocator> Hash for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn hash<H: Hasher>(&self, state: &mut H) { state.write_length_prefix(self.len); // 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> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Index<usize> for VecDeque<T, A> { +impl<T, A: Allocator> Index<usize> for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Output = T; #[inline] @@ -2729,7 +2746,8 @@ impl<T, A: Allocator> Index<usize> for VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> { +impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn index_mut(&mut self, index: usize) -> &mut T { self.get_mut(index).expect("Out of bounds access") @@ -2744,7 +2762,8 @@ impl<T> FromIterator<T> for VecDeque<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> IntoIterator for VecDeque<T, A> { +impl<T, A: Allocator> IntoIterator for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2756,7 +2775,8 @@ impl<T, A: Allocator> IntoIterator for VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque<T, A> { +impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -2766,7 +2786,8 @@ impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A> { +impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; @@ -2776,7 +2797,8 @@ impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Extend<T> for VecDeque<T, A> { +impl<T, A: Allocator> Extend<T> for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()); } @@ -2793,7 +2815,8 @@ impl<T, A: Allocator> Extend<T> for VecDeque<T, A> { } #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A> { +impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()); } @@ -2810,14 +2833,16 @@ impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for VecDeque<T, A> { +impl<T: fmt::Debug, A: Allocator> fmt::Debug for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() } } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> { +impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Turn a [`Vec<T>`] into a [`VecDeque<T>`]. /// /// [`Vec<T>`]: crate::vec::Vec @@ -2834,7 +2859,8 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> { } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> { +impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Turn a [`VecDeque<T>`] into a [`Vec<T>`]. /// /// [`Vec<T>`]: crate::vec::Vec diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 3e6ab0cfcbd64..5f98be5219384 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -86,6 +86,7 @@ #![warn(deprecated_in_future)] #![warn(missing_debug_implementations)] #![warn(missing_docs)] +#![allow(incomplete_features)] #![allow(explicit_outlives_requirements)] // // Library features: @@ -123,6 +124,7 @@ #![feature(extend_one)] #![feature(fmt_internals)] #![feature(fn_traits)] +#![feature(generic_const_exprs)] #![feature(global_co_alloc_meta)] #![feature(hasher_prefixfree_extras)] #![feature(inline_const)] diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 9129d3f0f7cab..9cf96ac700f55 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -1,6 +1,6 @@ #![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")] -use core::alloc::{LayoutError, GlobalCoAllocMeta}; +use core::alloc::{self, LayoutError, GlobalCoAllocMeta}; use core::cmp; use core::intrinsics; use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; @@ -49,12 +49,19 @@ enum AllocInit { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] -pub(crate) struct RawVec<T, A: Allocator = Global> { +// @TODO +// 1. make const generic _coop come from the target specification +// 2. apply `_coop` with logical && to `A::IsCoAllocator` +pub(crate) struct RawVec<T, A: Allocator = Global, const _coop: bool = true> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ ptr: Unique<T>, cap: usize, alloc: A, - #[allow(dead_code)] - pub(crate) meta: GlobalCoAllocMeta, + // As of v1.67.0, `cmp` for `TypeId` is not `const`, unfortunately: + //pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}], + //pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()], + pub(crate) meta: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots::<A>()], } impl<T> RawVec<T, Global> { @@ -104,7 +111,9 @@ impl<T> RawVec<T, Global> { } } -impl<T, A: Allocator> RawVec<T, A> { +impl<T, A: Allocator> RawVec<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ // Tiny Vecs are dumb. Skip to: // - 8 if the element size is 1, because any heap allocators is likely // 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> { slf: &mut RawVec<T, A>, len: usize, additional: usize, - ) { + ) + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: + { handle_reserve(slf.grow_amortized(len, additional)); } @@ -357,14 +368,18 @@ impl<T, A: Allocator> RawVec<T, A> { } } -impl<T, A: Allocator> RawVec<T, A> { +impl<T, A: Allocator> RawVec<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ /// Returns if the buffer needs to grow to fulfill the needed extra capacity. /// Mainly used to make inlining reserve-calls possible without inlining `grow`. - fn needs_to_grow(&self, len: usize, additional: usize) -> bool { + fn needs_to_grow(&self, len: usize, additional: usize) -> bool + { additional > self.capacity().wrapping_sub(len) } - fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) { + fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) + { // Allocators currently return a `NonNull<[u8]>` whose length matches // the size requested. If that ever changes, the capacity here should // change to `ptr.len() / mem::size_of::<T>()`. @@ -475,16 +490,19 @@ where memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into()) } -unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> { +unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. default fn drop(&mut self) { if let Some((ptr, layout)) = self.current_memory() { - unsafe { self.alloc.deallocate(ptr, layout) } + unsafe { self.alloc.co_deallocate(ptr, layout) } } } } -unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> { +// @TODO Custom +/*unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { // @TODO @@ -492,7 +510,7 @@ unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> { unsafe { self.alloc.deallocate(ptr, layout) } } } -} +}*/ // Central function for reserve error handling. #[cfg(not(no_global_oom_handling))] diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index e9886fc571799..5f5f42b3dd80f 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -95,7 +95,8 @@ pub(crate) mod hack { // We shouldn't add inline attribute to this since this is used in // `vec!` macro mostly and causes perf regression. See #71204 for // discussion and perf results. - pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A> { + pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { unsafe { let len = b.len(); let (b, alloc) = Box::into_raw_with_allocator(b); @@ -105,7 +106,8 @@ pub(crate) mod hack { #[cfg(not(no_global_oom_handling))] #[inline] - pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A> { + pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { T::to_vec(s, alloc) } @@ -113,13 +115,15 @@ pub(crate) mod hack { pub trait ConvertVec { fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> where - Self: Sized; + Self: Sized, + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:; } #[cfg(not(no_global_oom_handling))] impl<T: Clone> ConvertVec for T { #[inline] - default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> { + default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { struct DropGuard<'a, T, A: Allocator> { vec: &'a mut Vec<T, A>, num_init: usize, @@ -156,7 +160,8 @@ pub(crate) mod hack { #[cfg(not(no_global_oom_handling))] impl<T: Copy> ConvertVec for T { #[inline] - fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> { + fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { let mut v = Vec::with_capacity_in(s.len(), alloc); // SAFETY: // allocated above with the capacity of `s`, and initialize to `s.len()` in @@ -434,6 +439,7 @@ impl<T> [T] { pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A> where T: Clone, + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { // N.B., see the `hack` module in this file for more details. hack::to_vec(self, alloc) @@ -456,7 +462,8 @@ impl<T> [T] { #[rustc_allow_incoherent_impl] #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> { + pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { // N.B., see the `hack` module in this file for more details. hack::into_vec(self) } @@ -767,14 +774,16 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] { //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Borrow<[T]> for Vec<T, A> { +impl<T, A: Allocator> Borrow<[T]> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn borrow(&self) -> &[T] { &self[..] } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A> { +impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn borrow_mut(&mut self) -> &mut [T] { &mut self[..] } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 36b0b3c9e7cc0..bc3b223272007 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -397,11 +397,27 @@ mod spec_extend; #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")] #[rustc_insignificant_dtor] -pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> { - buf: RawVec<T, A>, +// @TODO _coop +pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const _coop_preferred: bool = true> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: +{ + buf: RawVec<T, A, _coop_preferred>, len: usize, } +#[unstable(feature = "global_co_alloc_vec", issue = "none")] +pub type CoVec<T> = Vec<T, Global, true>; + +/// "Plain" Vec. +#[unstable(feature = "global_co_alloc_vec", issue = "none")] +pub type PlVec<T> = Vec<T, Global, false>; + +/// "Weighted" Vec. +/// weight means how much it wants to cooperate. 0 = always pack; u8::MAX = always coop (if `Global` supports it). +/// @TODO Weighing on the side of Allocator - const fn. +#[unstable(feature = "global_co_alloc_vec", issue = "none")] +pub type WeVec<T, const weight: u8> = Vec<T, Global, {weight>1}>; + //////////////////////////////////////////////////////////////////////////////// // Inherent methods //////////////////////////////////////////////////////////////////////////////// @@ -593,7 +609,8 @@ impl<T> Vec<T> { } } -impl<T, A: Allocator> Vec<T, A> { +impl<T, A: Allocator> Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Constructs a new, empty `Vec<T, A>`. /// /// The vector will not allocate until elements are pushed onto it. @@ -1606,14 +1623,16 @@ impl<T, A: Allocator> Vec<T, A> { // This drop guard will be invoked when predicate or `drop` of element panicked. // It shifts unchecked elements to cover holes and `set_len` to the correct length. // In cases when predicate and `drop` never panick, it will be optimized out. - struct BackshiftOnDrop<'a, T, A: Allocator> { + struct BackshiftOnDrop<'a, T, A: Allocator> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { v: &'a mut Vec<T, A>, processed_len: usize, deleted_cnt: usize, original_len: usize, } - impl<T, A: Allocator> Drop for BackshiftOnDrop<'_, T, A> { + impl<T, A: Allocator> Drop for BackshiftOnDrop<'_, T, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { if self.deleted_cnt > 0 { // SAFETY: Trailing unchecked items must be valid since we never touch them. @@ -1640,6 +1659,7 @@ impl<T, A: Allocator> Vec<T, A> { g: &mut BackshiftOnDrop<'_, T, A>, ) where F: FnMut(&mut T) -> bool, + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { while g.processed_len != original_len { // SAFETY: Unchecked element must be valid. @@ -1732,7 +1752,8 @@ impl<T, A: Allocator> Vec<T, A> { } /* INVARIANT: vec.len() > read >= write > write-1 >= 0 */ - struct FillGapOnDrop<'a, T, A: core::alloc::Allocator> { + struct FillGapOnDrop<'a, T, A: core::alloc::Allocator> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /* Offset of the element we want to check if it is duplicate */ read: usize, @@ -1744,7 +1765,8 @@ impl<T, A: Allocator> Vec<T, A> { vec: &'a mut Vec<T, A>, } - impl<'a, T, A: core::alloc::Allocator> Drop for FillGapOnDrop<'a, T, A> { + impl<'a, T, A: core::alloc::Allocator> Drop for FillGapOnDrop<'a, T, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { /* This code gets executed when `same_bucket` panics */ @@ -2337,7 +2359,8 @@ impl<T, A: Allocator> Vec<T, A> { } } -impl<T: Clone, A: Allocator> Vec<T, A> { +impl<T: Clone, A: Allocator> Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. /// /// If `new_len` is greater than `len`, the `Vec` is extended by the @@ -2436,7 +2459,8 @@ impl<T: Clone, A: Allocator> Vec<T, A> { } } -impl<T, A: Allocator, const N: usize> Vec<[T; N], A> { +impl<T, A: Allocator, const N: usize> Vec<[T; N], A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`. /// /// # Panics @@ -2497,7 +2521,8 @@ impl<T: Clone> ExtendWith<T> for ExtendElement<T> { } } -impl<T, A: Allocator> Vec<T, A> { +impl<T, A: Allocator> Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[cfg(not(no_global_oom_handling))] /// Extend the vector by `n` values, using the given generator. fn extend_with<E: ExtendWith<T>>(&mut self, n: usize, mut value: E) { @@ -2529,7 +2554,8 @@ impl<T, A: Allocator> Vec<T, A> { } } -impl<T: PartialEq, A: Allocator> Vec<T, A> { +impl<T: PartialEq, A: Allocator> Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Removes consecutive repeated elements in the vector according to the /// [`PartialEq`] trait implementation. /// @@ -2565,7 +2591,8 @@ pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> { #[doc(hidden)] #[cfg(not(no_global_oom_handling))] #[unstable(feature = "allocator_api", issue = "32838")] -pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> { +pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { <T as SpecFromElem>::from_elem(elem, n, alloc) } @@ -2577,7 +2604,8 @@ trait ExtendFromWithinSpec { unsafe fn spec_extend_from_within(&mut self, src: Range<usize>); } -impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> { +impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { default unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { // SAFETY: // - len is increased only after initializing elements @@ -2596,7 +2624,8 @@ impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> { } } -impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> { +impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { let count = src.len(); { @@ -2629,7 +2658,8 @@ impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> { //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ops::Deref for Vec<T, A> { +impl<T, A: Allocator> ops::Deref for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { type Target = [T]; #[inline] @@ -2639,7 +2669,8 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ops::DerefMut for Vec<T, A> { +impl<T, A: Allocator> ops::DerefMut for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn deref_mut(&mut self) -> &mut [T] { unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } @@ -2652,7 +2683,8 @@ trait SpecCloneFrom { } #[cfg(not(no_global_oom_handling))] -impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> { +impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { default fn clone_from(this: &mut Self, other: &Self) { // drop anything that will not be overwritten this.truncate(other.len()); @@ -2668,7 +2700,8 @@ impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> { } #[cfg(not(no_global_oom_handling))] -impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> { +impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn clone_from(this: &mut Self, other: &Self) { this.clear(); this.extend_from_slice(other); @@ -2677,7 +2710,8 @@ impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> { +impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[cfg(not(test))] fn clone(&self) -> Self { let alloc = self.allocator().clone(); @@ -2712,7 +2746,8 @@ impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> { /// assert_eq!(b.hash_one(v), b.hash_one(s)); /// ``` #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Hash, A: Allocator> Hash for Vec<T, A> { +impl<T: Hash, A: Allocator> Hash for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { Hash::hash(&**self, state) @@ -2724,7 +2759,8 @@ impl<T: Hash, A: Allocator> Hash for Vec<T, A> { message = "vector indices are of type `usize` or ranges of `usize`", label = "vector indices are of type `usize` or ranges of `usize`" )] -impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> { +impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { type Output = I::Output; #[inline] diff --git a/library/alloc/tests/boxed.rs b/library/alloc/tests/boxed.rs index af49826ff30a3..c34e7a558061d 100644 --- a/library/alloc/tests/boxed.rs +++ b/library/alloc/tests/boxed.rs @@ -61,6 +61,7 @@ fn box_deref_lval() { pub struct ConstAllocator; +//@TODO unsafe impl const Allocator for ConstAllocator { fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { match layout.size() { diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 18bf6a92e9eca..c13975a5e2e39 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -78,6 +78,11 @@ pub struct SliceAndMeta { #[allow(missing_debug_implementations)] pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>; +#[unstable(feature = "global_co_alloc", issue = "none")] +pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { + if A::IS_CO_ALLOCATOR { 1 } else { 0 } +} + /// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of /// data described via [`Layout`][]. /// @@ -137,6 +142,13 @@ pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>; #[unstable(feature = "allocator_api", issue = "32838")] #[const_trait] pub unsafe trait Allocator { + //const fn is_co_allocator() -> bool {false} + // Can't have: const type Xyz; + /// If this is any type with non-zero size, then the actual `Allocator` implementation supports cooperative functions (`co_*`) as first class citizens. + //type IsCoAllocator = (); + // It applies to the global (default) allocator only. And/or System allocator?! TODO + const IS_CO_ALLOCATOR: bool = true; + /// Attempts to allocate a block of memory. /// /// On success, returns a [`NonNull<[u8]>`][NonNull] meeting the size and alignment guarantees of `layout`. @@ -505,6 +517,7 @@ pub unsafe trait Allocator { } } +// @TODO #[unstable(feature = "allocator_api", issue = "32838")] unsafe impl<A> Allocator for &A where diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 8790649abe6f1..b62da0571aec3 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -97,6 +97,7 @@ #![allow(incomplete_features)] // // Library features: +#![feature(associated_type_defaults)] #![feature(const_align_offset)] #![feature(const_align_of_val)] #![feature(const_align_of_val_raw)] diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index c5a5991cc81c4..bee6edbc40731 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -197,10 +197,13 @@ impl System { } } +// @TODO // The Allocator impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl, // which is in `std::sys::*::alloc`. #[unstable(feature = "allocator_api", issue = "32838")] unsafe impl Allocator for System { + const IS_CO_ALLOCATOR: bool = false; + #[inline] fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { self.alloc_impl(layout, false) From d726a087105c0d04e55978cf36110876dfbf1a46 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sun, 11 Dec 2022 11:09:09 -0800 Subject: [PATCH 04/47] Vec, VecDeque, RawVec with COOP_PREFERRED. WIP: probably NOT compilable. --- .../alloc/src/collections/vec_deque/drain.rs | 38 +++++++----- .../src/collections/vec_deque/into_iter.rs | 26 ++++++--- .../alloc/src/collections/vec_deque/mod.rs | 3 +- .../src/collections/vec_deque/spec_extend.rs | 13 ++++- library/alloc/src/lib.rs | 7 ++- library/alloc/src/slice.rs | 6 +- library/alloc/src/vec/drain.rs | 44 +++++++++----- library/alloc/src/vec/drain_filter.rs | 9 ++- library/alloc/src/vec/into_iter.rs | 58 ++++++++++++------- library/alloc/src/vec/mod.rs | 57 ++++++++++++------ library/alloc/src/vec/partial_eq.rs | 1 + library/alloc/src/vec/spec_extend.rs | 8 ++- library/alloc/src/vec/spec_from_elem.rs | 16 +++-- library/alloc/src/vec/splice.rs | 19 ++++-- 14 files changed, 209 insertions(+), 96 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index 89feb361ddc11..e4f2f80892504 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -2,7 +2,7 @@ use core::iter::FusedIterator; use core::marker::PhantomData; use core::mem::{self, SizedTypeProperties}; use core::ptr::NonNull; -use core::{fmt, ptr}; +use core::{alloc, fmt, ptr}; use crate::alloc::{Allocator, Global}; @@ -19,7 +19,8 @@ pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, -> { +> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { // We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T // and we want it to be covariant instead deque: NonNull<VecDeque<T, A>>, @@ -34,7 +35,8 @@ pub struct Drain< _marker: PhantomData<&'a T>, } -impl<'a, T, A: Allocator> Drain<'a, T, A> { +impl<'a, T, A: Allocator> Drain<'a, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { pub(super) unsafe fn new( deque: &'a mut VecDeque<T, A>, drain_start: usize, @@ -88,7 +90,8 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> { } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> { +impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain") .field(&self.drain_len) @@ -100,16 +103,21 @@ impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> { } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A> {} +unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A> {} +unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Drop for Drain<'_, T, A> { +impl<T, A: Allocator> Drop for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { - struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>); + struct DropGuard<'r, 'a, T, A: Allocator> (&'r mut Drain<'a, T, A>) + where [(); alloc::co_alloc_metadata_num_slots::<A>()]:; - impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> { + impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { if self.0.remaining != 0 { unsafe { @@ -190,7 +198,8 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Iterator for Drain<'_, T, A> { +impl<T, A: Allocator> Iterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = T; #[inline] @@ -212,7 +221,8 @@ impl<T, A: Allocator> Iterator for Drain<'_, T, A> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> { +impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn next_back(&mut self) -> Option<T> { if self.remaining == 0 { @@ -225,7 +235,9 @@ impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {} +impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {} +impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index e54880e865230..18a1891a3f3ed 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -1,4 +1,4 @@ -use core::fmt; +use core::{alloc, fmt}; use core::iter::{FusedIterator, TrustedLen}; use crate::alloc::{Allocator, Global}; @@ -17,11 +17,13 @@ use super::VecDeque; pub struct IntoIter< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, -> { +> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { inner: VecDeque<T, A>, } -impl<T, A: Allocator> IntoIter<T, A> { +impl<T, A: Allocator> IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { pub(super) fn new(inner: VecDeque<T, A>) -> Self { IntoIter { inner } } @@ -32,14 +34,16 @@ impl<T, A: Allocator> IntoIter<T, A> { } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> { +impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.inner).finish() } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Iterator for IntoIter<T, A> { +impl<T, A: Allocator> Iterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = T; #[inline] @@ -55,7 +59,8 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> { +impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn next_back(&mut self) -> Option<T> { self.inner.pop_back() @@ -63,14 +68,17 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> { +impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn is_empty(&self) -> bool { self.inner.is_empty() } } #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {} +impl<T, A: Allocator> FusedIterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {} +unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index f76cbb0b17904..cd13039cf015f 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -456,7 +456,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { written: usize, } - impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> { + impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { self.deque.len += self.written; } diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index dccf40ccb38aa..f6d957fdc03ba 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -1,5 +1,8 @@ +#![feature(min_specialization)] + use crate::alloc::Allocator; use crate::vec; +use core::alloc; use core::iter::TrustedLen; use core::slice; @@ -13,6 +16,7 @@ pub(super) trait SpecExtend<T, I> { impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A> where I: Iterator<Item = T>, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { default fn spec_extend(&mut self, mut iter: I) { // This function should be the moral equivalent of: @@ -22,7 +26,8 @@ where // } // May only be called if `deque.len() < deque.capacity()` - unsafe fn push_unchecked<T, A: Allocator>(deque: &mut VecDeque<T, A>, element: T) { + unsafe fn push_unchecked<T, A: Allocator>(deque: &mut VecDeque<T, A>, element: T) + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { // SAFETY: Because of the precondition, it's guaranteed that there is space // in the logical array after the last element. unsafe { deque.buffer_write(deque.to_physical_idx(deque.len), element) }; @@ -52,6 +57,7 @@ where impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A> where I: TrustedLen<Item = T>, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { default fn spec_extend(&mut self, iter: I) { // This is the case for a TrustedLen iterator. @@ -84,7 +90,8 @@ where } } -impl<T, A: Allocator> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A> { +impl<T, A: Allocator> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) { let slice = iterator.as_slice(); self.reserve(slice.len()); @@ -101,6 +108,7 @@ impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for VecDeque<T, A> where I: Iterator<Item = &'a T>, T: Copy, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.copied()) @@ -110,6 +118,7 @@ where impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque<T, A> where T: Copy, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 5f98be5219384..f592a1fbd865d 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -184,7 +184,12 @@ #![feature(global_co_alloc)] #![feature(hashmap_internals)] #![feature(lang_items)] -#![feature(min_specialization)] +// When we used min_specialization instead of specialization, library/alloc/src/vec/mod.rs was failing with: +// - cannot specialize on predicate `the constant `core::alloc::co_alloc_metadata_num_slots::<A>()` can be evaluated` +// - cannot specialize on predicate `[(); _] well-formed` +// - cannot specialize on predicate `the constant `core::alloc::co_alloc_metadata_num_slots::<A>()` can be evaluated` +//#![feature(min_specialization)] +#![feature(specialization)] #![feature(negative_impls)] #![feature(never_type)] #![feature(rustc_allow_const_fn_unstable)] diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 5f5f42b3dd80f..db4fcfdb8c441 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -124,11 +124,13 @@ pub(crate) mod hack { #[inline] default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { - struct DropGuard<'a, T, A: Allocator> { + struct DropGuard<'a, T, A: Allocator> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { vec: &'a mut Vec<T, A>, num_init: usize, } - impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A> { + impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A> + where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn drop(&mut self) { // SAFETY: diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 2b1a787cc5499..279488fd03450 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -3,7 +3,7 @@ use core::fmt; use core::iter::{FusedIterator, TrustedLen}; use core::mem::{self, ManuallyDrop, SizedTypeProperties}; use core::ptr::{self, NonNull}; -use core::slice::{self}; +use core::{alloc, slice}; use super::Vec; @@ -23,7 +23,8 @@ pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, -> { +> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Index of tail to preserve pub(super) tail_start: usize, /// Length of tail @@ -34,13 +35,15 @@ pub struct Drain< } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> { +impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() } } -impl<'a, T, A: Allocator> Drain<'a, T, A> { +impl<'a, T, A: Allocator> Drain<'a, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Returns the remaining items of this iterator as a slice. /// /// # Examples @@ -139,19 +142,23 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> { } #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] -impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> { +impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn as_ref(&self) -> &[T] { self.as_slice() } } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> {} +unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> {} +unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Iterator for Drain<'_, T, A> { +impl<T, A: Allocator> Iterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = T; #[inline] @@ -165,7 +172,8 @@ impl<T, A: Allocator> Iterator for Drain<'_, T, A> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> { +impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn next_back(&mut self) -> Option<T> { self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) }) @@ -173,12 +181,15 @@ impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Drop for Drain<'_, T, A> { +impl<T, A: Allocator> Drop for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { /// Moves back the un-`Drain`ed elements to restore the original `Vec`. - struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>); + struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>) + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ; - impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> { + impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { if self.0.tail_len > 0 { unsafe { @@ -242,14 +253,17 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> { +impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn is_empty(&self) -> bool { self.iter.is_empty() } } #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A> {} +unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {} +impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} diff --git a/library/alloc/src/vec/drain_filter.rs b/library/alloc/src/vec/drain_filter.rs index 8c03f1692d940..d78a81b647d5b 100644 --- a/library/alloc/src/vec/drain_filter.rs +++ b/library/alloc/src/vec/drain_filter.rs @@ -1,7 +1,6 @@ use crate::alloc::{Allocator, Global}; use core::mem::{self, ManuallyDrop}; -use core::ptr; -use core::slice; +use core::{alloc, ptr, slice}; use super::Vec; @@ -27,6 +26,7 @@ pub struct DrainFilter< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, > where F: FnMut(&mut T) -> bool, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { pub(super) vec: &'a mut Vec<T, A>, /// The index of the item that will be inspected by the next call to `next`. @@ -48,6 +48,7 @@ pub struct DrainFilter< impl<T, F, A: Allocator> DrainFilter<'_, T, F, A> where F: FnMut(&mut T) -> bool, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Returns a reference to the underlying allocator. #[unstable(feature = "allocator_api", issue = "32838")] @@ -116,6 +117,7 @@ where impl<T, F, A: Allocator> Iterator for DrainFilter<'_, T, F, A> where F: FnMut(&mut T) -> bool, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = T; @@ -154,11 +156,13 @@ where impl<T, F, A: Allocator> Drop for DrainFilter<'_, T, F, A> where F: FnMut(&mut T) -> bool, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { struct BackshiftOnDrop<'a, 'b, T, F, A: Allocator> where F: FnMut(&mut T) -> bool, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { drain: &'b mut DrainFilter<'a, T, F, A>, } @@ -166,6 +170,7 @@ where impl<'a, 'b, T, F, A: Allocator> Drop for BackshiftOnDrop<'a, 'b, T, F, A> where F: FnMut(&mut T) -> bool, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { unsafe { diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 37966007eb7e4..6c43afd3aeb0e 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -4,8 +4,7 @@ use crate::alloc::{Allocator, Global}; #[cfg(not(no_global_oom_handling))] use crate::collections::VecDeque; use crate::raw_vec::RawVec; -use core::array; -use core::fmt; +use core::{alloc, array, fmt}; use core::iter::{ FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccessNoCoerce, }; @@ -32,7 +31,8 @@ use core::slice::{self}; pub struct IntoIter< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, -> { +> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { pub(super) buf: NonNull<T>, pub(super) phantom: PhantomData<T>, pub(super) cap: usize, @@ -46,13 +46,15 @@ pub struct IntoIter< } #[stable(feature = "vec_intoiter_debug", since = "1.13.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> { +impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.as_slice()).finish() } } -impl<T, A: Allocator> IntoIter<T, A> { +impl<T, A: Allocator> IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Returns the remaining items of this iterator as a slice. /// /// # Examples @@ -141,7 +143,8 @@ impl<T, A: Allocator> IntoIter<T, A> { #[cfg(not(no_global_oom_handling))] #[inline] - pub(crate) fn into_vecdeque(self) -> VecDeque<T, A> { + pub(crate) fn into_vecdeque(self) -> VecDeque<T, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { // Keep our `Drop` impl from dropping the elements and the allocator let mut this = ManuallyDrop::new(self); @@ -168,19 +171,23 @@ impl<T, A: Allocator> IntoIter<T, A> { } #[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")] -impl<T, A: Allocator> AsRef<[T]> for IntoIter<T, A> { +impl<T, A: Allocator> AsRef<[T]> for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn as_ref(&self) -> &[T] { self.as_slice() } } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: Send, A: Allocator + Send> Send for IntoIter<T, A> {} +unsafe impl<T: Send, A: Allocator + Send> Send for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: Sync, A: Allocator + Sync> Sync for IntoIter<T, A> {} +unsafe impl<T: Sync, A: Allocator + Sync> Sync for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Iterator for IntoIter<T, A> { +impl<T, A: Allocator> Iterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = T; #[inline] @@ -294,7 +301,8 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> { +impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn next_back(&mut self) -> Option<T> { if self.end == self.ptr { @@ -335,17 +343,20 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> { +impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn is_empty(&self) -> bool { self.ptr == self.end } } #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {} +impl<T, A: Allocator> FusedIterator for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {} +unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[doc(hidden)] #[unstable(issue = "none", feature = "std_internals")] @@ -364,13 +375,15 @@ impl<T: Copy> NonDrop for T {} unsafe impl<T, A: Allocator> TrustedRandomAccessNoCoerce for IntoIter<T, A> where T: NonDrop, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { const MAY_HAVE_SIDE_EFFECT: bool = false; } #[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_into_iter_clone", since = "1.8.0")] -impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> { +impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[cfg(not(test))] fn clone(&self) -> Self { self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter() @@ -382,11 +395,14 @@ impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> { +unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { - struct DropGuard<'a, T, A: Allocator>(&'a mut IntoIter<T, A>); + struct DropGuard<'a, T, A: Allocator>(&'a mut IntoIter<T, A>) + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ; - impl<T, A: Allocator> Drop for DropGuard<'_, T, A> { + impl<T, A: Allocator> Drop for DropGuard<'_, T, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { unsafe { // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec @@ -410,11 +426,13 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> { // also refer to the vec::in_place_collect module documentation to get an overview #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] -unsafe impl<T, A: Allocator> InPlaceIterable for IntoIter<T, A> {} +unsafe impl<T, A: Allocator> InPlaceIterable for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] -unsafe impl<T, A: Allocator> SourceIter for IntoIter<T, A> { +unsafe impl<T, A: Allocator> SourceIter for IntoIter<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { type Source = Self; #[inline] diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index bc3b223272007..9c7a278bf7207 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2774,7 +2774,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { message = "vector indices are of type `usize` or ranges of `usize`", label = "vector indices are of type `usize` or ranges of `usize`" )] -impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> { +impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { IndexMut::index_mut(&mut **self, index) @@ -2791,7 +2792,8 @@ impl<T> FromIterator<T> for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> IntoIterator for Vec<T, A> { +impl<T, A: Allocator> IntoIterator for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2836,7 +2838,8 @@ impl<T, A: Allocator> IntoIterator for Vec<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> { +impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; @@ -2846,7 +2849,8 @@ impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> { +impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; @@ -2857,7 +2861,8 @@ impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Extend<T> for Vec<T, A> { +impl<T, A: Allocator> Extend<T> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()) @@ -2874,7 +2879,8 @@ impl<T, A: Allocator> Extend<T> for Vec<T, A> { } } -impl<T, A: Allocator> Vec<T, A> { +impl<T, A: Allocator> Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { // leaf method to which various SpecFrom/SpecExtend implementations delegate when // they have no further optimizations to apply #[cfg(not(no_global_oom_handling))] @@ -3048,7 +3054,8 @@ impl<T, A: Allocator> Vec<T, A> { /// [`copy_from_slice`]: slice::copy_from_slice #[cfg(not(no_global_oom_handling))] #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a, T: Copy + 'a, A: Allocator + 'a> Extend<&'a T> for Vec<T, A> { +impl<'a, T: Copy + 'a, A: Allocator + 'a> Extend<&'a T> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()) } @@ -3066,7 +3073,8 @@ impl<'a, T: Copy + 'a, A: Allocator + 'a> Extend<&'a T> for Vec<T, A> { /// Implements comparison of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison). #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialOrd, A: Allocator> PartialOrd for Vec<T, A> { +impl<T: PartialOrd, A: Allocator> PartialOrd for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { PartialOrd::partial_cmp(&**self, &**other) @@ -3074,11 +3082,13 @@ impl<T: PartialOrd, A: Allocator> PartialOrd for Vec<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Eq, A: Allocator> Eq for Vec<T, A> {} +impl<T: Eq, A: Allocator> Eq for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {} /// Implements ordering of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison). #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Ord, A: Allocator> Ord for Vec<T, A> { +impl<T: Ord, A: Allocator> Ord for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[inline] fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(&**self, &**other) @@ -3086,7 +3096,8 @@ impl<T: Ord, A: Allocator> Ord for Vec<T, A> { } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> { +unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { unsafe { // use drop for [T] @@ -3110,35 +3121,40 @@ impl<T> const Default for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> { +impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> AsRef<Vec<T, A>> for Vec<T, A> { +impl<T, A: Allocator> AsRef<Vec<T, A>> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn as_ref(&self) -> &Vec<T, A> { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl<T, A: Allocator> AsMut<Vec<T, A>> for Vec<T, A> { +impl<T, A: Allocator> AsMut<Vec<T, A>> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn as_mut(&mut self) -> &mut Vec<T, A> { self } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> AsRef<[T]> for Vec<T, A> { +impl<T, A: Allocator> AsRef<[T]> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn as_ref(&self) -> &[T] { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> { +impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn as_mut(&mut self) -> &mut [T] { self } @@ -3235,7 +3251,8 @@ where // note: test pulls in std, which causes errors here #[cfg(not(test))] #[stable(feature = "vec_from_box", since = "1.18.0")] -impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> { +impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Convert a boxed slice into a vector by transferring ownership of /// the existing heap allocation. /// @@ -3254,7 +3271,8 @@ impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> { #[cfg(not(no_global_oom_handling))] #[cfg(not(test))] #[stable(feature = "box_from_vec", since = "1.20.0")] -impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> { +impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Convert a vector into a boxed slice. /// /// If `v` has excess capacity, its items will be moved into a @@ -3294,7 +3312,8 @@ impl From<&str> for Vec<u8> { } #[stable(feature = "array_try_from_vec", since = "1.48.0")] -impl<T, A: Allocator, const N: usize> TryFrom<Vec<T, A>> for [T; N] { +impl<T, A: Allocator, const N: usize> TryFrom<Vec<T, A>> for [T; N] +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { type Error = Vec<T, A>; /// Gets the entire contents of the `Vec<T>` as an array, diff --git a/library/alloc/src/vec/partial_eq.rs b/library/alloc/src/vec/partial_eq.rs index b0cf72577a1be..b50b9673e1560 100644 --- a/library/alloc/src/vec/partial_eq.rs +++ b/library/alloc/src/vec/partial_eq.rs @@ -1,3 +1,4 @@ +//use core::alloc; use crate::alloc::Allocator; #[cfg(not(no_global_oom_handling))] use crate::borrow::Cow; diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index 56065ce565bfc..81245a394b22e 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -1,3 +1,4 @@ +use core::alloc; use crate::alloc::Allocator; use core::iter::TrustedLen; use core::slice::{self}; @@ -12,6 +13,7 @@ pub(super) trait SpecExtend<T, I> { impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A> where I: Iterator<Item = T>, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { default fn spec_extend(&mut self, iter: I) { self.extend_desugared(iter) @@ -21,13 +23,15 @@ where impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A> where I: TrustedLen<Item = T>, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { default fn spec_extend(&mut self, iterator: I) { self.extend_trusted(iterator) } } -impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> { +impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn spec_extend(&mut self, mut iterator: IntoIter<T>) { unsafe { self.append_elements(iterator.as_slice() as _); @@ -40,6 +44,7 @@ impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec<T, A> where I: Iterator<Item = &'a T>, T: Clone, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.cloned()) @@ -49,6 +54,7 @@ where impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A> where T: Copy, + [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index ff364c033ee98..7f95539b12d96 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -1,5 +1,6 @@ use core::ptr; +use core::alloc; use crate::alloc::Allocator; use crate::raw_vec::RawVec; @@ -7,11 +8,13 @@ use super::{ExtendElement, IsZero, Vec}; // Specialization trait used for Vec::from_elem pub(super) trait SpecFromElem: Sized { - fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>; + fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ; } impl<T: Clone> SpecFromElem for T { - default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> { + default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { let mut v = Vec::with_capacity_in(n, alloc); v.extend_with(n, ExtendElement(elem)); v @@ -20,7 +23,8 @@ impl<T: Clone> SpecFromElem for T { impl<T: Clone + IsZero> SpecFromElem for T { #[inline] - default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> { + default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { if elem.is_zero() { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } @@ -32,7 +36,8 @@ impl<T: Clone + IsZero> SpecFromElem for T { impl SpecFromElem for i8 { #[inline] - fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> { + fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } @@ -47,7 +52,8 @@ impl SpecFromElem for i8 { impl SpecFromElem for u8 { #[inline] - fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> { + fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> + where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index 1861147fe72fb..631d9d5e3aa60 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -1,3 +1,4 @@ +use core::alloc; use crate::alloc::{Allocator, Global}; use core::ptr::{self}; use core::slice::{self}; @@ -22,13 +23,15 @@ pub struct Splice< 'a, I: Iterator + 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, -> { +> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { pub(super) drain: Drain<'a, I::Item, A>, pub(super) replace_with: I, } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> Iterator for Splice<'_, I, A> { +impl<I: Iterator, A: Allocator> Iterator for Splice<'_, I, A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { type Item = I::Item; fn next(&mut self) -> Option<Self::Item> { @@ -41,17 +44,20 @@ impl<I: Iterator, A: Allocator> Iterator for Splice<'_, I, A> { } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> DoubleEndedIterator for Splice<'_, I, A> { +impl<I: Iterator, A: Allocator> DoubleEndedIterator for Splice<'_, I, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn next_back(&mut self) -> Option<Self::Item> { self.drain.next_back() } } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> {} +impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> { +impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { fn drop(&mut self) { self.drain.by_ref().for_each(drop); // At this point draining is done and the only remaining tasks are splicing @@ -98,7 +104,8 @@ impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> { } /// Private helper methods for `Splice::drop` -impl<T, A: Allocator> Drain<'_, T, A> { +impl<T, A: Allocator> Drain<'_, T, A> +where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// The range from `self.vec.len` to `self.tail_start` contains elements /// that have been moved out. /// Fill that range as much as possible with new elements from the `replace_with` iterator. From 48e7037215567364aa6e458b67bcc5e6517352c9 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sun, 11 Dec 2022 11:29:17 -0800 Subject: [PATCH 05/47] Vec, VecDeque, RawVec etc. with COOP_PREFERRED. WIP: probably NOT compilable. Triggers an internal compiler error from cargo 1.68.0-nightly (f6e737b1e 2022-12-02) --- library/alloc/src/boxed.rs | 15 +- .../alloc/src/collections/binary_heap/mod.rs | 20 +- .../alloc/src/collections/vec_deque/drain.rs | 51 ++--- .../alloc/src/collections/vec_deque/macros.rs | 4 +- .../alloc/src/collections/vec_deque/mod.rs | 107 +++++------ .../src/collections/vec_deque/spec_extend.rs | 24 +-- library/alloc/src/lib.rs | 1 + library/alloc/src/raw_vec.rs | 49 ++--- library/alloc/src/slice.rs | 8 +- library/alloc/src/vec/drain.rs | 55 +++--- library/alloc/src/vec/drain_filter.rs | 27 +-- library/alloc/src/vec/into_iter.rs | 76 ++++---- library/alloc/src/vec/mod.rs | 174 +++++++++--------- library/alloc/src/vec/partial_eq.rs | 28 +-- library/core/src/alloc/mod.rs | 11 ++ 15 files changed, 339 insertions(+), 311 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index a563b2587236c..9dc695559a80e 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -146,6 +146,7 @@ #![stable(feature = "rust1", since = "1.0.0")] +use core::alloc; use core::any::Any; use core::async_iter::AsyncIterator; use core::borrow; @@ -698,7 +699,7 @@ impl<T> Box<[T]> { Err(_) => return Err(AllocError), }; let ptr = Global.allocate(layout)?; - Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) + Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) } } @@ -730,12 +731,13 @@ impl<T> Box<[T]> { Err(_) => return Err(AllocError), }; let ptr = Global.allocate_zeroed(layout)?; - Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) + Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) } } } -impl<T, A: Allocator> Box<[T], A> { +impl<T, A: Allocator> Box<[T], A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Constructs a new boxed slice with uninitialized contents in the provided allocator. /// /// # Examples @@ -763,7 +765,7 @@ impl<T, A: Allocator> Box<[T], A> { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { - unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) } + unsafe { RawVec::<T, A, {alloc::SHORT_TERM_VEC_PREFERS_COOP}>::with_capacity_in(len, alloc).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents in the provided allocator, @@ -791,7 +793,7 @@ impl<T, A: Allocator> Box<[T], A> { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { - unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) } + unsafe { RawVec::<T, A, {alloc::SHORT_TERM_VEC_PREFERS_COOP}>::with_capacity_zeroed_in(len, alloc).into_box(len) } } } @@ -2019,7 +2021,8 @@ impl<I> FromIterator<I> for Box<[I]> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_slice_clone", since = "1.3.0")] -impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> { +impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> +where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { fn clone(&self) -> Self { let alloc = Box::allocator(self).clone(); self.to_vec_in(alloc).into_boxed_slice() diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index 0b73b1af4eb35..7c6b0bb6e6c75 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -143,13 +143,15 @@ #![allow(missing_docs)] #![stable(feature = "rust1", since = "1.0.0")] -use core::fmt; +use core::{alloc, fmt}; use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen}; use core::mem::{self, swap, ManuallyDrop}; use core::num::NonZeroUsize; use core::ops::{Deref, DerefMut}; use core::ptr; +use crate::alloc::Global; + use crate::collections::TryReserveError; use crate::slice; use crate::vec::{self, AsVecIntoIter, Vec}; @@ -1241,7 +1243,7 @@ impl<T> BinaryHeap<T> { /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] - pub fn drain(&mut self) -> Drain<'_, T> { + pub fn drain(&mut self) -> Drain<'_, T, {alloc::SHORT_TERM_VEC_PREFERS_COOP}> { Drain { iter: self.data.drain(..) } } @@ -1521,12 +1523,14 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {} /// [`drain`]: BinaryHeap::drain #[stable(feature = "drain", since = "1.6.0")] #[derive(Debug)] -pub struct Drain<'a, T: 'a> { - iter: vec::Drain<'a, T>, +pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { + iter: vec::Drain<'a, T, Global, COOP_PREFERRED>, } #[stable(feature = "drain", since = "1.6.0")] -impl<T> Iterator for Drain<'_, T> { +impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { type Item = T; #[inline] @@ -1541,7 +1545,7 @@ impl<T> Iterator for Drain<'_, T> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T> DoubleEndedIterator for Drain<'_, T> { +impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED> { #[inline] fn next_back(&mut self) -> Option<T> { self.iter.next_back() @@ -1549,14 +1553,14 @@ impl<T> DoubleEndedIterator for Drain<'_, T> { } #[stable(feature = "drain", since = "1.6.0")] -impl<T> ExactSizeIterator for Drain<'_, T> { +impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED> { fn is_empty(&self) -> bool { self.iter.is_empty() } } #[stable(feature = "fused", since = "1.26.0")] -impl<T> FusedIterator for Drain<'_, T> {} +impl<T, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, COOP_PREFERRED> {} /// A draining iterator over the elements of a `BinaryHeap`. /// diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index e4f2f80892504..bab4919eb6032 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -19,11 +19,12 @@ pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, + const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP} > -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { // We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T // and we want it to be covariant instead - deque: NonNull<VecDeque<T, A>>, + deque: NonNull<VecDeque<T, A, COOP_PREFERRED>>, // drain_start is stored in deque.len drain_len: usize, // index into the logical array, not the physical one (always lies in [0..deque.len)) @@ -35,10 +36,10 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { _marker: PhantomData<&'a T>, } -impl<'a, T, A: Allocator> Drain<'a, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { pub(super) unsafe fn new( - deque: &'a mut VecDeque<T, A>, + deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, drain_start: usize, drain_len: usize, ) -> Self { @@ -90,8 +91,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain") .field(&self.drain_len) @@ -103,21 +104,21 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Drop for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { - struct DropGuard<'r, 'a, T, A: Allocator> (&'r mut Drain<'a, T, A>) - where [(); alloc::co_alloc_metadata_num_slots::<A>()]:; + struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> (&'r mut Drain<'a, T, A, COOP_PREFERRED>) + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; - impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { if self.0.remaining != 0 { unsafe { @@ -198,8 +199,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Iterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = T; #[inline] @@ -221,8 +222,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn next_back(&mut self) -> Option<T> { if self.remaining == 0 { @@ -235,9 +236,9 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} diff --git a/library/alloc/src/collections/vec_deque/macros.rs b/library/alloc/src/collections/vec_deque/macros.rs index f7768f54c6225..68ee43152b5b5 100644 --- a/library/alloc/src/collections/vec_deque/macros.rs +++ b/library/alloc/src/collections/vec_deque/macros.rs @@ -1,10 +1,10 @@ macro_rules! __impl_slice_eq1 { ([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => { #[stable(feature = "vec_deque_partial_eq_slice", since = "1.17.0")] - impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs + impl<T, U, A: Allocator, const COOP_PREFERRED: bool, $($vars)*> PartialEq<$rhs> for $lhs where T: PartialEq<U>, - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, $($constraints)* { fn eq(&self, other: &$rhs) -> bool { diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index cd13039cf015f..0d1c562825ede 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -97,8 +97,9 @@ mod tests; pub struct VecDeque< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, + const COOP_PREFERRED: bool = true > -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { // `self[0]`, if it exists, is `buf[head]`. // `head < buf.capacity()`, unless `buf.capacity() == 0` when `head == 0`. @@ -107,12 +108,12 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: // if `len == 0`, the exact value of `head` is unimportant. // if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`. len: usize, - buf: RawVec<T, A>, + buf: RawVec<T, A, COOP_PREFERRED>, } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn clone(&self) -> Self { let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone()); deq.extend(self.iter().cloned()); @@ -126,8 +127,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { /// Runs the destructor for all items in the slice when it gets dropped (normally or /// during unwinding). @@ -160,8 +161,8 @@ impl<T> Default for VecDeque<T> { } } -impl<T, A: Allocator> VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Marginally more convenient #[inline] fn ptr(&self) -> *mut T { @@ -450,14 +451,14 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { mut iter: impl Iterator<Item = T>, len: usize, ) -> usize { - struct Guard<'a, T, A: Allocator> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { - deque: &'a mut VecDeque<T, A>, + struct Guard<'a, T, A: Allocator, const COOP_PREFERRED: bool> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, written: usize, } - impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for Guard<'a, T, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { self.deque.len += self.written; } @@ -571,8 +572,8 @@ impl<T> VecDeque<T> { } } -impl<T, A: Allocator> VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Creates an empty deque. /// /// # Examples @@ -2607,8 +2608,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<T: Clone, A: Allocator> VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Modifies the deque in-place so that `len()` is equal to new_len, /// either by removing excess elements from the back or by appending clones of `value` /// to the back. @@ -2653,8 +2654,8 @@ fn wrap_index(logical_index: usize, capacity: usize) -> usize { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialEq, A: Allocator> PartialEq for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> PartialEq for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn eq(&self, other: &Self) -> bool { if self.len != other.len() { return false; @@ -2693,27 +2694,27 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Eq, A: Allocator> Eq for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} -__impl_slice_eq1! { [] VecDeque<T, A>, Vec<U, A>, } -__impl_slice_eq1! { [] VecDeque<T, A>, &[U], } -__impl_slice_eq1! { [] VecDeque<T, A>, &mut [U], } -__impl_slice_eq1! { [const N: usize] VecDeque<T, A>, [U; N], } -__impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &[U; N], } -__impl_slice_eq1! { [const N: usize] VecDeque<T, A>, &mut [U; N], } +__impl_slice_eq1! { [] VecDeque<T, A, COOP_PREFERRED>, Vec<U, A, COOP_PREFERRED>, } +__impl_slice_eq1! { [] VecDeque<T, A, COOP_PREFERRED>, &[U], } +__impl_slice_eq1! { [] VecDeque<T, A, COOP_PREFERRED>, &mut [U], } +__impl_slice_eq1! { [const N: usize] VecDeque<T, A, COOP_PREFERRED>, [U; N], } +__impl_slice_eq1! { [const N: usize] VecDeque<T, A, COOP_PREFERRED>, &[U; N], } +__impl_slice_eq1! { [const N: usize] VecDeque<T, A, COOP_PREFERRED>, &mut [U; N], } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialOrd, A: Allocator> PartialOrd for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.iter().partial_cmp(other.iter()) } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Ord, A: Allocator> Ord for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn cmp(&self, other: &Self) -> Ordering { self.iter().cmp(other.iter()) @@ -2721,8 +2722,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Hash, A: Allocator> Hash for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn hash<H: Hasher>(&self, state: &mut H) { state.write_length_prefix(self.len); // It's not possible to use Hash::hash_slice on slices @@ -2736,8 +2737,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Index<usize> for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Index<usize> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Output = T; #[inline] @@ -2747,8 +2748,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> IndexMut<usize> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn index_mut(&mut self, index: usize) -> &mut T { self.get_mut(index).expect("Out of bounds access") @@ -2763,8 +2764,8 @@ impl<T> FromIterator<T> for VecDeque<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> IntoIterator for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2776,8 +2777,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -2787,8 +2788,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; @@ -2798,8 +2799,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Extend<T> for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()); } @@ -2816,8 +2817,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T: 'a + Copy, A: Allocator, const COOP_PREFERRED: bool> Extend<&'a T> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()); } @@ -2834,16 +2835,16 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() } } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Turn a [`Vec<T>`] into a [`VecDeque<T>`]. /// /// [`Vec<T>`]: crate::vec::Vec @@ -2860,8 +2861,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> From<VecDeque<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Turn a [`VecDeque<T>`] into a [`Vec<T>`]. /// /// [`Vec<T>`]: crate::vec::Vec diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index f6d957fdc03ba..cdeda1a57bed5 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -13,10 +13,10 @@ pub(super) trait SpecExtend<T, I> { fn spec_extend(&mut self, iter: I); } -impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn spec_extend(&mut self, mut iter: I) { // This function should be the moral equivalent of: @@ -26,8 +26,8 @@ where // } // May only be called if `deque.len() < deque.capacity()` - unsafe fn push_unchecked<T, A: Allocator>(deque: &mut VecDeque<T, A>, element: T) - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + unsafe fn push_unchecked<T, A: Allocator, const COOP_PREFERRED: bool>(deque: &mut VecDeque<T, A, COOP_PREFERRED>, element: T) + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { // SAFETY: Because of the precondition, it's guaranteed that there is space // in the logical array after the last element. unsafe { deque.buffer_write(deque.to_physical_idx(deque.len), element) }; @@ -54,10 +54,10 @@ where } } -impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn spec_extend(&mut self, iter: I) { // This is the case for a TrustedLen iterator. @@ -90,8 +90,8 @@ where } } -impl<T, A: Allocator> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) { let slice = iterator.as_slice(); self.reserve(slice.len()); @@ -104,21 +104,21 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for VecDeque<T, A> +impl<'a, T: 'a, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, I> for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = &'a T>, T: Copy, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.copied()) } } -impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque<T, A> +impl<'a, T: 'a, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index f592a1fbd865d..5d856158c206c 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -182,6 +182,7 @@ #![feature(fundamental)] #![cfg_attr(not(test), feature(generator_trait))] #![feature(global_co_alloc)] +#![feature(global_co_alloc_short_term_pref)] #![feature(hashmap_internals)] #![feature(lang_items)] // When we used min_specialization instead of specialization, library/alloc/src/vec/mod.rs was failing with: diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 9cf96ac700f55..19f1ef0ac06e1 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -1,6 +1,6 @@ #![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")] -use core::alloc::{self, LayoutError, GlobalCoAllocMeta}; +use core::alloc::{self, LayoutError, GlobalCoAllocMeta, PtrAndMeta}; use core::cmp; use core::intrinsics; use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; @@ -49,11 +49,9 @@ enum AllocInit { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] -// @TODO -// 1. make const generic _coop come from the target specification -// 2. apply `_coop` with logical && to `A::IsCoAllocator` -pub(crate) struct RawVec<T, A: Allocator = Global, const _coop: bool = true> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +// @TODO apply `_coop` with logical && to `A::IsCoAllocator` +pub(crate) struct RawVec<T, A: Allocator = Global, const COOP_PREFERRED: bool = true> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { ptr: Unique<T>, cap: usize, @@ -61,7 +59,7 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: // As of v1.67.0, `cmp` for `TypeId` is not `const`, unfortunately: //pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}], //pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()], - pub(crate) meta: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots::<A>()], + pub(crate) metas: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], } impl<T> RawVec<T, Global> { @@ -111,8 +109,8 @@ impl<T> RawVec<T, Global> { } } -impl<T, A: Allocator> RawVec<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { // Tiny Vecs are dumb. Skip to: // - 8 if the element size is 1, because any heap allocators is likely @@ -131,7 +129,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: /// the returned `RawVec`. pub const fn new_in(alloc: A) -> Self { // `cap: 0` means "unallocated". zero-sized types are ignored. - Self { ptr: Unique::dangling(), cap: 0, alloc, meta: GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/} } + Self { ptr: Unique::dangling(), cap: 0, alloc, + metas: [GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/}; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)] } } /// Like `with_capacity`, but parameterized over the choice of @@ -208,7 +207,7 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) }, cap: capacity, alloc, - meta: GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/} + metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)] } } } @@ -225,7 +224,7 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: /// guaranteed. #[inline] pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { - Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc, meta: GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/} } + Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)] } } /// Gets a raw pointer to the start of the allocation. Note that this is @@ -289,12 +288,12 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: // handle_reserve behind a call, while making sure that this function is likely to be // inlined as just a comparison and a call if the comparison fails. #[cold] - fn do_reserve_and_handle<T, A: Allocator>( - slf: &mut RawVec<T, A>, + fn do_reserve_and_handle<T, A: Allocator, const COOP_PREFERRED: bool>( + slf: &mut RawVec<T, A, COOP_PREFERRED>, len: usize, additional: usize, ) - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { handle_reserve(slf.grow_amortized(len, additional)); } @@ -368,8 +367,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: } } -impl<T, A: Allocator> RawVec<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Returns if the buffer needs to grow to fulfill the needed extra capacity. /// Mainly used to make inlining reserve-calls possible without inlining `grow`. @@ -490,19 +489,25 @@ where memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into()) } -unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for RawVec<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. default fn drop(&mut self) { if let Some((ptr, layout)) = self.current_memory() { - unsafe { self.alloc.co_deallocate(ptr, layout) } + if A::IS_CO_ALLOCATOR && COOP_PREFERRED { + let meta = self.metas[0]; + unsafe { self.alloc.co_deallocate(PtrAndMeta {ptr, meta}, layout) } + } else { + unsafe { self.alloc.deallocate(ptr, layout) } + } } } } // @TODO Custom -/*unsafe impl<#[may_dangle] T> Drop for RawVec<T, Global> { +unsafe impl<#[may_dangle] T, const COOP_PREFERRED: bool> Drop for RawVec<T, Global, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { // @TODO @@ -510,7 +515,7 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: unsafe { self.alloc.deallocate(ptr, layout) } } } -}*/ +} // Central function for reserve error handling. #[cfg(not(no_global_oom_handling))] diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index db4fcfdb8c441..fa762f5f8c4fe 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -776,16 +776,16 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] { //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Borrow<[T]> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Borrow<[T]> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn borrow(&self) -> &[T] { &self[..] } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> BorrowMut<[T]> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn borrow_mut(&mut self) -> &mut [T] { &mut self[..] } diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 279488fd03450..6372333bda81d 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -23,8 +23,9 @@ pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, + const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP} > -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Index of tail to preserve pub(super) tail_start: usize, /// Length of tail @@ -35,15 +36,15 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() } } -impl<'a, T, A: Allocator> Drain<'a, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Returns the remaining items of this iterator as a slice. /// /// # Examples @@ -142,23 +143,23 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] -impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Drain<'a, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn as_ref(&self) -> &[T] { self.as_slice() } } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T: Sync, A: Sync + Allocator, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T: Send, A: Send + Allocator, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Iterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = T; #[inline] @@ -172,8 +173,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn next_back(&mut self) -> Option<T> { self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) }) @@ -181,15 +182,15 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> Drop for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { /// Moves back the un-`Drain`ed elements to restore the original `Vec`. - struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>) - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ; + struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool>(&'r mut Drain<'a, T, A, COOP_PREFERRED>) + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: ; - impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { if self.0.tail_len > 0 { unsafe { @@ -253,17 +254,17 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn is_empty(&self) -> bool { self.iter.is_empty() } } #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} diff --git a/library/alloc/src/vec/drain_filter.rs b/library/alloc/src/vec/drain_filter.rs index d78a81b647d5b..7be4abacbec96 100644 --- a/library/alloc/src/vec/drain_filter.rs +++ b/library/alloc/src/vec/drain_filter.rs @@ -24,11 +24,12 @@ pub struct DrainFilter< T, F, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, + const COOP_PREFERRED: bool = true > where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { - pub(super) vec: &'a mut Vec<T, A>, + pub(super) vec: &'a mut Vec<T, A, COOP_PREFERRED>, /// The index of the item that will be inspected by the next call to `next`. pub(super) idx: usize, /// The number of items that have been drained (removed) thus far. @@ -45,10 +46,10 @@ pub struct DrainFilter< pub(super) panic_flag: bool, } -impl<T, F, A: Allocator> DrainFilter<'_, T, F, A> +impl<T, F, A: Allocator, const COOP_PREFERRED: bool> DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Returns a reference to the underlying allocator. #[unstable(feature = "allocator_api", issue = "32838")] @@ -114,10 +115,10 @@ where } #[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] -impl<T, F, A: Allocator> Iterator for DrainFilter<'_, T, F, A> +impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Iterator for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = T; @@ -153,24 +154,24 @@ where } #[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] -impl<T, F, A: Allocator> Drop for DrainFilter<'_, T, F, A> +impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Drop for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { - struct BackshiftOnDrop<'a, 'b, T, F, A: Allocator> + struct BackshiftOnDrop<'a, 'b, T, F, A: Allocator, const COOP_PREFERRED: bool> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { - drain: &'b mut DrainFilter<'a, T, F, A>, + drain: &'b mut DrainFilter<'a, T, F, A, COOP_PREFERRED>, } - impl<'a, 'b, T, F, A: Allocator> Drop for BackshiftOnDrop<'a, 'b, T, F, A> + impl<'a, 'b, T, F, A: Allocator, const COOP_PREFERRED: bool> Drop for BackshiftOnDrop<'a, 'b, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { unsafe { diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 6c43afd3aeb0e..fdad67de09716 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -31,8 +31,9 @@ use core::slice::{self}; pub struct IntoIter< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, + const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP} > -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { pub(super) buf: NonNull<T>, pub(super) phantom: PhantomData<T>, pub(super) cap: usize, @@ -46,15 +47,15 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "vec_intoiter_debug", since = "1.13.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.as_slice()).finish() } } -impl<T, A: Allocator> IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Returns the remaining items of this iterator as a slice. /// /// # Examples @@ -143,8 +144,7 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[cfg(not(no_global_oom_handling))] #[inline] - pub(crate) fn into_vecdeque(self) -> VecDeque<T, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + pub(crate) fn into_vecdeque(self) -> VecDeque<T, A> { // Keep our `Drop` impl from dropping the elements and the allocator let mut this = ManuallyDrop::new(self); @@ -171,23 +171,23 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")] -impl<T, A: Allocator> AsRef<[T]> for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn as_ref(&self) -> &[T] { self.as_slice() } } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: Send, A: Allocator + Send> Send for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: Sync, A: Allocator + Sync> Sync for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Iterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = T; #[inline] @@ -301,8 +301,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn next_back(&mut self) -> Option<T> { if self.end == self.ptr { @@ -343,20 +343,20 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn is_empty(&self) -> bool { self.ptr == self.end } } #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[doc(hidden)] #[unstable(issue = "none", feature = "std_internals")] @@ -372,18 +372,18 @@ impl<T: Copy> NonDrop for T {} #[unstable(issue = "none", feature = "std_internals")] // TrustedRandomAccess (without NoCoerce) must not be implemented because // subtypes/supertypes of `T` might not be `NonDrop` -unsafe impl<T, A: Allocator> TrustedRandomAccessNoCoerce for IntoIter<T, A> +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedRandomAccessNoCoerce for IntoIter<T, A, COOP_PREFERRED> where T: NonDrop, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { const MAY_HAVE_SIDE_EFFECT: bool = false; } #[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_into_iter_clone", since = "1.8.0")] -impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[cfg(not(test))] fn clone(&self) -> Self { self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter() @@ -395,20 +395,20 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { - struct DropGuard<'a, T, A: Allocator>(&'a mut IntoIter<T, A>) - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ; + struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool>(&'a mut IntoIter<T, A, COOP_PREFERRED>) + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: ; - impl<T, A: Allocator> Drop for DropGuard<'_, T, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'_, T, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { unsafe { // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec let alloc = ManuallyDrop::take(&mut self.0.alloc); // RawVec handles deallocation - let _ = RawVec::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc); + let _ = RawVec::<T, A, true>::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc); } } } @@ -426,13 +426,13 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { // also refer to the vec::in_place_collect module documentation to get an overview #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] -unsafe impl<T, A: Allocator> InPlaceIterable for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> InPlaceIterable for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] -unsafe impl<T, A: Allocator> SourceIter for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> SourceIter for IntoIter<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Source = Self; #[inline] diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 9c7a278bf7207..1f8607a05a442 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -398,10 +398,10 @@ mod spec_extend; #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")] #[rustc_insignificant_dtor] // @TODO _coop -pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const _coop_preferred: bool = true> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: +pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = true> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { - buf: RawVec<T, A, _coop_preferred>, + buf: RawVec<T, A, COOP_PREFERRED>, len: usize, } @@ -609,8 +609,8 @@ impl<T> Vec<T> { } } -impl<T, A: Allocator> Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Constructs a new, empty `Vec<T, A>`. /// /// The vector will not allocate until elements are pushed onto it. @@ -1623,16 +1623,16 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { // This drop guard will be invoked when predicate or `drop` of element panicked. // It shifts unchecked elements to cover holes and `set_len` to the correct length. // In cases when predicate and `drop` never panick, it will be optimized out. - struct BackshiftOnDrop<'a, T, A: Allocator> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { - v: &'a mut Vec<T, A>, + struct BackshiftOnDrop<'a, T, A: Allocator, const VEC_IS_COOP: bool=true> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]: { + v: &'a mut Vec<T, A, VEC_IS_COOP>, processed_len: usize, deleted_cnt: usize, original_len: usize, } - impl<T, A: Allocator> Drop for BackshiftOnDrop<'_, T, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + impl<T, A: Allocator, const VEC_IS_COOP: bool> Drop for BackshiftOnDrop<'_, T, A, VEC_IS_COOP> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]: { fn drop(&mut self) { if self.deleted_cnt > 0 { // SAFETY: Trailing unchecked items must be valid since we never touch them. @@ -1651,15 +1651,15 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } } - let mut g = BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len }; + let mut g = BackshiftOnDrop::<T, A, COOP_PREFERRED> { v: self, processed_len: 0, deleted_cnt: 0, original_len }; - fn process_loop<F, T, A: Allocator, const DELETED: bool>( + fn process_loop<F, T, A: Allocator, const DELETED: bool, const VEC_IS_COOP: bool>( original_len: usize, f: &mut F, - g: &mut BackshiftOnDrop<'_, T, A>, + g: &mut BackshiftOnDrop<'_, T, A, VEC_IS_COOP>, ) where F: FnMut(&mut T) -> bool, - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]: { while g.processed_len != original_len { // SAFETY: Unchecked element must be valid. @@ -1690,10 +1690,10 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } // Stage 1: Nothing was deleted. - process_loop::<F, T, A, false>(original_len, &mut f, &mut g); + process_loop::<F, T, A, false, COOP_PREFERRED>(original_len, &mut f, &mut g); // Stage 2: Some elements were deleted. - process_loop::<F, T, A, true>(original_len, &mut f, &mut g); + process_loop::<F, T, A, true, COOP_PREFERRED>(original_len, &mut f, &mut g); // All item are processed. This can be optimized to `set_len` by LLVM. drop(g); @@ -1752,8 +1752,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } /* INVARIANT: vec.len() > read >= write > write-1 >= 0 */ - struct FillGapOnDrop<'a, T, A: core::alloc::Allocator> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + struct FillGapOnDrop<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /* Offset of the element we want to check if it is duplicate */ read: usize, @@ -1762,11 +1762,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { write: usize, /* The Vec that would need correction if `same_bucket` panicked */ - vec: &'a mut Vec<T, A>, + vec: &'a mut Vec<T, A, COOP_PREFERRED>, } - impl<'a, T, A: core::alloc::Allocator> Drop for FillGapOnDrop<'a, T, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + impl<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> Drop for FillGapOnDrop<'a, T, A, COOP_PREFERRED> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { /* This code gets executed when `same_bucket` panics */ @@ -2359,8 +2359,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<T: Clone, A: Allocator> Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. /// /// If `new_len` is greater than `len`, the `Vec` is extended by the @@ -2459,8 +2459,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<T, A: Allocator, const N: usize> Vec<[T; N], A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> Vec<[T; N], A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`. /// /// # Panics @@ -2521,8 +2521,8 @@ impl<T: Clone> ExtendWith<T> for ExtendElement<T> { } } -impl<T, A: Allocator> Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[cfg(not(no_global_oom_handling))] /// Extend the vector by `n` values, using the given generator. fn extend_with<E: ExtendWith<T>>(&mut self, n: usize, mut value: E) { @@ -2554,8 +2554,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<T: PartialEq, A: Allocator> Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Removes consecutive repeated elements in the vector according to the /// [`PartialEq`] trait implementation. /// @@ -2591,8 +2591,8 @@ pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> { #[doc(hidden)] #[cfg(not(no_global_oom_handling))] #[unstable(feature = "allocator_api", issue = "32838")] -pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +pub fn from_elem_in<T: Clone, A: Allocator, const COOP_PREFERRED: bool>(elem: T, n: usize, alloc: A) -> Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { <T as SpecFromElem>::from_elem(elem, n, alloc) } @@ -2604,8 +2604,8 @@ trait ExtendFromWithinSpec { unsafe fn spec_extend_from_within(&mut self, src: Range<usize>); } -impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { // SAFETY: // - len is increased only after initializing elements @@ -2624,8 +2624,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { let count = src.len(); { @@ -2658,8 +2658,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ops::Deref for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::Deref for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Target = [T]; #[inline] @@ -2669,8 +2669,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ops::DerefMut for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::DerefMut for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn deref_mut(&mut self) -> &mut [T] { unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } @@ -2683,8 +2683,8 @@ trait SpecCloneFrom { } #[cfg(not(no_global_oom_handling))] -impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn clone_from(this: &mut Self, other: &Self) { // drop anything that will not be overwritten this.truncate(other.len()); @@ -2700,8 +2700,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } #[cfg(not(no_global_oom_handling))] -impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn clone_from(this: &mut Self, other: &Self) { this.clear(); this.extend_from_slice(other); @@ -2710,8 +2710,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[cfg(not(test))] fn clone(&self) -> Self { let alloc = self.allocator().clone(); @@ -2746,8 +2746,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// assert_eq!(b.hash_one(v), b.hash_one(s)); /// ``` #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Hash, A: Allocator> Hash for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { Hash::hash(&**self, state) @@ -2759,8 +2759,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { message = "vector indices are of type `usize` or ranges of `usize`", label = "vector indices are of type `usize` or ranges of `usize`" )] -impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> Index<I> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Output = I::Output; #[inline] @@ -2774,8 +2774,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { message = "vector indices are of type `usize` or ranges of `usize`", label = "vector indices are of type `usize` or ranges of `usize`" )] -impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> IndexMut<I> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { IndexMut::index_mut(&mut **self, index) @@ -2792,8 +2792,8 @@ impl<T> FromIterator<T> for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> IntoIterator for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2838,8 +2838,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; @@ -2849,8 +2849,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; @@ -2861,8 +2861,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Extend<T> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()) @@ -2879,8 +2879,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<T, A: Allocator> Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { // leaf method to which various SpecFrom/SpecExtend implementations delegate when // they have no further optimizations to apply #[cfg(not(no_global_oom_handling))] @@ -3054,8 +3054,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// [`copy_from_slice`]: slice::copy_from_slice #[cfg(not(no_global_oom_handling))] #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a, T: Copy + 'a, A: Allocator + 'a> Extend<&'a T> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<'a, T: Copy + 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> Extend<&'a T> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()) } @@ -3073,8 +3073,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { /// Implements comparison of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison). #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialOrd, A: Allocator> PartialOrd for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { PartialOrd::partial_cmp(&**self, &**other) @@ -3082,13 +3082,13 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Eq, A: Allocator> Eq for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} /// Implements ordering of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison). #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Ord, A: Allocator> Ord for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(&**self, &**other) @@ -3096,8 +3096,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn drop(&mut self) { unsafe { // use drop for [T] @@ -3121,40 +3121,40 @@ impl<T> const Default for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> AsRef<Vec<T, A>> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A>> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn as_ref(&self) -> &Vec<T, A> { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl<T, A: Allocator> AsMut<Vec<T, A>> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A>> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn as_mut(&mut self) -> &mut Vec<T, A> { self } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> AsRef<[T]> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn as_ref(&self) -> &[T] { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<[T]> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn as_mut(&mut self) -> &mut [T] { self } @@ -3251,8 +3251,8 @@ where // note: test pulls in std, which causes errors here #[cfg(not(test))] #[stable(feature = "vec_from_box", since = "1.18.0")] -impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Box<[T], A>> for Vec<T, A, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Convert a boxed slice into a vector by transferring ownership of /// the existing heap allocation. /// @@ -3271,8 +3271,8 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[cfg(not(no_global_oom_handling))] #[cfg(not(test))] #[stable(feature = "box_from_vec", since = "1.20.0")] -impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for Box<[T], A> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Convert a vector into a boxed slice. /// /// If `v` has excess capacity, its items will be moved into a @@ -3312,8 +3312,8 @@ impl From<&str> for Vec<u8> { } #[stable(feature = "array_try_from_vec", since = "1.48.0")] -impl<T, A: Allocator, const N: usize> TryFrom<Vec<T, A>> for [T; N] -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, A, COOP_PREFERRED>> for [T; N] +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { type Error = Vec<T, A>; /// Gets the entire contents of the `Vec<T>` as an array, diff --git a/library/alloc/src/vec/partial_eq.rs b/library/alloc/src/vec/partial_eq.rs index b50b9673e1560..bcf52b7333218 100644 --- a/library/alloc/src/vec/partial_eq.rs +++ b/library/alloc/src/vec/partial_eq.rs @@ -6,12 +6,12 @@ use crate::borrow::Cow; use super::Vec; macro_rules! __impl_slice_eq1 { - ([$($vars:tt)*] $lhs:ty, $rhs:ty $(where $ty:ty: $bound:ident)?, #[$stability:meta]) => { + ([$($vars:tt)*] $lhs:ty, $rhs:ty, #[$stability:meta], $($constraints:tt)*) => { #[$stability] impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs where T: PartialEq<U>, - $($ty: $bound)? + $($constraints)* { #[inline] fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] } @@ -21,21 +21,21 @@ macro_rules! __impl_slice_eq1 { } } -__impl_slice_eq1! { [A1: Allocator, A2: Allocator] Vec<T, A1>, Vec<U, A2>, #[stable(feature = "rust1", since = "1.0.0")] } -__impl_slice_eq1! { [A: Allocator] Vec<T, A>, &[U], #[stable(feature = "rust1", since = "1.0.0")] } -__impl_slice_eq1! { [A: Allocator] Vec<T, A>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")] } -__impl_slice_eq1! { [A: Allocator] &[T], Vec<U, A>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")] } -__impl_slice_eq1! { [A: Allocator] &mut [T], Vec<U, A>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")] } -__impl_slice_eq1! { [A: Allocator] Vec<T, A>, [U], #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")] } -__impl_slice_eq1! { [A: Allocator] [T], Vec<U, A>, #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")] } +__impl_slice_eq1! { [A1: Allocator, A2: Allocator, const COOP_PREFERRED1: bool, const COOP_PREFERRED2: bool] Vec<T, A1, COOP_PREFERRED1>, Vec<U, A2, COOP_PREFERRED2>, #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A1>(COOP_PREFERRED1)]:, [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A2>(COOP_PREFERRED2)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &[U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &[T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &mut [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, [U], #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } #[cfg(not(no_global_oom_handling))] -__impl_slice_eq1! { [A: Allocator] Cow<'_, [T]>, Vec<U, A> where T: Clone, #[stable(feature = "rust1", since = "1.0.0")] } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Cow<'_, [T]>, Vec<U, A, COOP_PREFERRED>, #[stable(feature = "rust1", since = "1.0.0")], T: Clone, [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } #[cfg(not(no_global_oom_handling))] -__impl_slice_eq1! { [] Cow<'_, [T]>, &[U] where T: Clone, #[stable(feature = "rust1", since = "1.0.0")] } +__impl_slice_eq1! { [] Cow<'_, [T]>, &[U], #[stable(feature = "rust1", since = "1.0.0")], T: Clone } #[cfg(not(no_global_oom_handling))] -__impl_slice_eq1! { [] Cow<'_, [T]>, &mut [U] where T: Clone, #[stable(feature = "rust1", since = "1.0.0")] } -__impl_slice_eq1! { [A: Allocator, const N: usize] Vec<T, A>, [U; N], #[stable(feature = "rust1", since = "1.0.0")] } -__impl_slice_eq1! { [A: Allocator, const N: usize] Vec<T, A>, &[U; N], #[stable(feature = "rust1", since = "1.0.0")] } +__impl_slice_eq1! { [] Cow<'_, [T]>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], T: Clone } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, [U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, &[U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } // NOTE: some less important impls are omitted to reduce code bloat // FIXME(Centril): Reconsider this? diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index c13975a5e2e39..d49e94402a7ed 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -29,6 +29,7 @@ use crate::ptr::{self, NonNull}; // @TODO Make this target-specific #[unstable(feature = "global_co_alloc_meta", issue = "none")] #[allow(missing_debug_implementations)] +#[derive(Clone, Copy)] pub struct GlobalCoAllocMeta { //pub one: usize, /*pub two: usize, @@ -74,6 +75,9 @@ pub struct SliceAndMeta { pub meta: GlobalCoAllocMeta, } +#[unstable(feature = "global_co_alloc_short_term_pref", issue = "none")] +pub const SHORT_TERM_VEC_PREFERS_COOP: bool = true; + #[unstable(feature = "global_co_alloc_meta", issue = "none")] #[allow(missing_debug_implementations)] pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>; @@ -83,6 +87,13 @@ pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { if A::IS_CO_ALLOCATOR { 1 } else { 0 } } +#[unstable(feature = "global_co_alloc", issue = "none")] +/// Param `coop_preferred` - if false, then this returns `0`, regardless of +/// whether allocator `A` is cooperative. +pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>(coop_preferred: bool) -> usize { + if A::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } +} + /// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of /// data described via [`Layout`][]. /// From d867e938c3db2c741171fb320b4cd59618a0bf2e Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 4 Jan 2023 13:44:48 -0800 Subject: [PATCH 06/47] VecDeque use cases have COOP_PREFERRED. Probably still not compilable and ICE. --- .../src/collections/vec_deque/into_iter.rs | 11 ++++++----- library/alloc/src/collections/vec_deque/mod.rs | 18 ++++++++++-------- library/std/src/io/impls.rs | 6 ++++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index 18a1891a3f3ed..477ab0c4db594 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -17,18 +17,19 @@ use super::VecDeque; pub struct IntoIter< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, + const COOP_PREFERRED: bool = true > -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { - inner: VecDeque<T, A>, +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + inner: VecDeque<T, A, COOP_PREFERRED>, } -impl<T, A: Allocator> IntoIter<T, A> +impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { - pub(super) fn new(inner: VecDeque<T, A>) -> Self { + pub(super) fn new(inner: VecDeque<T, A, COOP_PREFERRED>) -> Self { IntoIter { inner } } - pub(super) fn into_vecdeque(self) -> VecDeque<T, A> { + pub(super) fn into_vecdeque(self) -> VecDeque<T, A, COOP_PREFERRED> { self.inner } } diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 0d1c562825ede..95fda23c22274 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -153,10 +153,10 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<T> Default for VecDeque<T> { +impl<T, const COOP_PREFERRED: bool = true> Default for VecDeque<T, Global, COOP_PREFERRED> { /// Creates an empty deque. #[inline] - fn default() -> VecDeque<T> { + fn default() -> VecDeque<T, Global, COOP_PREFERRED> { VecDeque::new() } } @@ -536,7 +536,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } } -impl<T> VecDeque<T> { +impl<T, Global, const COOP_PREFERRED: bool> VecDeque<T, Global, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Creates an empty deque. /// /// # Examples @@ -550,7 +551,7 @@ impl<T> VecDeque<T> { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_vec_deque_new", since = "CURRENT_RUSTC_VERSION")] #[must_use] - pub const fn new() -> VecDeque<T> { + pub const fn new() -> VecDeque<T, Global, COOP_PREFERRED> { // FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable. VecDeque { head: 0, len: 0, buf: RawVec::NEW } } @@ -567,7 +568,7 @@ impl<T> VecDeque<T> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] - pub fn with_capacity(capacity: usize) -> VecDeque<T> { + pub fn with_capacity(capacity: usize) -> VecDeque<T, Global, COOP_PREFERRED> { Self::with_capacity_in(capacity, Global) } } @@ -585,7 +586,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// ``` #[inline] #[unstable(feature = "allocator_api", issue = "32838")] - pub const fn new_in(alloc: A) -> VecDeque<T, A> { + pub const fn new_in(alloc: A) -> VecDeque<T, A, COOP_PREFERRED> { VecDeque { head: 0, len: 0, buf: RawVec::new_in(alloc) } } @@ -599,7 +600,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// let deque: VecDeque<u32> = VecDeque::with_capacity(10); /// ``` #[unstable(feature = "allocator_api", issue = "32838")] - pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> { + pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A, COOP_PREFERRED> { VecDeque { head: 0, len: 0, buf: RawVec::with_capacity_in(capacity, alloc) } } @@ -2892,7 +2893,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// assert_eq!(vec, [8, 9, 1, 2, 3, 4]); /// assert_eq!(vec.as_ptr(), ptr); /// ``` - fn from(mut other: VecDeque<T, A>) -> Self { + fn from<const _VECDEQUE_COOP_PREFERRED: bool>(mut other: VecDeque<T, A, _VECDEQUE_COOP_PREFERRED>) -> Self { other.make_contiguous(); unsafe { @@ -2905,6 +2906,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE if other.head != 0 { ptr::copy(buf.add(other.head), buf, len); } + // @TODO: Vec::from_raw_parts_in(buf, len, cap, alloc) } } diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index e5048dcc8acd9..d575d9fa3f990 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -414,7 +414,8 @@ impl<A: Allocator> Write for Vec<u8, A> { /// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`. #[stable(feature = "vecdeque_read_write", since = "1.63.0")] -impl<A: Allocator> Read for VecDeque<u8, A> { +impl<A: Allocator, const _COOP_PREFERRED: bool> Read for VecDeque<u8, A, _COOP_PREFERRED> +where [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]: { /// Fill `buf` with the contents of the "front" slice as returned by /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are /// discontiguous, multiple calls to `read` will be needed to read the entire content. @@ -438,7 +439,8 @@ impl<A: Allocator> Read for VecDeque<u8, A> { /// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed. #[stable(feature = "vecdeque_read_write", since = "1.63.0")] -impl<A: Allocator> Write for VecDeque<u8, A> { +impl<A: Allocator, const _COOP_PREFERRED: bool> Write for VecDeque<u8, A, _COOP_PREFERRED> +where [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]: { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.extend(buf); From e3798b5f2045c4e0ec30e4cfbbb2b8b7a9751b41 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 12 Dec 2022 11:00:24 -0800 Subject: [PATCH 07/47] Vec & VecDeque use cases have COOP_PREFERRED. NOT COMPILABLE. --- .../alloc/src/collections/vec_deque/mod.rs | 13 ++++---- .../collections/vec_deque/spec_from_iter.rs | 11 +++++-- library/alloc/src/raw_vec.rs | 3 +- library/alloc/src/slice.rs | 30 +++++++++---------- library/alloc/src/vec/drain.rs | 2 +- library/alloc/src/vec/into_iter.rs | 2 +- library/alloc/src/vec/mod.rs | 16 +++++----- library/alloc/src/vec/spec_extend.rs | 20 ++++++------- library/alloc/src/vec/spec_from_elem.rs | 20 ++++++------- 9 files changed, 63 insertions(+), 54 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 95fda23c22274..d21b39feddffb 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -8,7 +8,7 @@ #![feature(global_co_alloc)] #![stable(feature = "rust1", since = "1.0.0")] -use core::alloc; +use core::alloc::{self, GlobalAlloc}; use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; @@ -2758,8 +2758,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<T> FromIterator<T> for VecDeque<T> { - fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T> { +impl<T, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, GlobalAlloc, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> { SpecFromIter::spec_from_iter(iter.into_iter()) } } @@ -2855,7 +2856,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// and to not re-allocate the `Vec`'s buffer or allocate /// any additional memory. #[inline] - fn from(other: Vec<T, A>) -> Self { + fn from<const OTHER_COOP_PREFERRED: bool>(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(OTHER_COOP_PREFERRED)]: { let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } } @@ -2913,7 +2915,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "std_collections_from_array", since = "1.56.0")] -impl<T, const N: usize> From<[T; N]> for VecDeque<T> { +impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]> for VecDeque<T, Global, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { /// Converts a `[T; N]` into a `VecDeque<T>`. /// /// ``` diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index 7650492ebdad1..b9641ef156c88 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -1,13 +1,16 @@ use super::{IntoIter, VecDeque}; +use crate::alloc::Allocator; +use core::alloc; /// Specialization trait used for `VecDeque::from_iter` pub(super) trait SpecFromIter<T, I> { fn spec_from_iter(iter: I) -> Self; } -impl<T, I> SpecFromIter<T, I> for VecDeque<T> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn spec_from_iter(iterator: I) -> Self { // Since converting is O(1) now, just re-use the `Vec` logic for @@ -18,14 +21,16 @@ where } } -impl<T> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T> { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self { iterator.into_vecdeque() } } -impl<T> SpecFromIter<T, IntoIter<T>> for VecDeque<T> { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn spec_from_iter(iterator: IntoIter<T>) -> Self { iterator.into_vecdeque() diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 19f1ef0ac06e1..b231d6785b0d9 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -62,7 +62,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE pub(crate) metas: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], } -impl<T> RawVec<T, Global> { +impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so /// they cannot call `Self::new()`. /// diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index fa762f5f8c4fe..3f45f8b587375 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -95,8 +95,8 @@ pub(crate) mod hack { // We shouldn't add inline attribute to this since this is used in // `vec!` macro mostly and causes perf regression. See #71204 for // discussion and perf results. - pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + pub fn into_vec<T, A: Allocator, const COOP_PREFERRED: bool>(b: Box<[T], A>) -> Vec<T, A, COOP_PREFERRED> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { unsafe { let len = b.len(); let (b, alloc) = Box::into_raw_with_allocator(b); @@ -106,31 +106,31 @@ pub(crate) mod hack { #[cfg(not(no_global_oom_handling))] #[inline] - pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + pub fn to_vec<T: ConvertVec, A: Allocator, const COOP_PREFERRED: bool>(s: &[T], alloc: A) -> Vec<T, A, COOP_PREFERRED> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { T::to_vec(s, alloc) } #[cfg(not(no_global_oom_handling))] pub trait ConvertVec { - fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> + fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED> where Self: Sized, - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:; + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; } #[cfg(not(no_global_oom_handling))] impl<T: Clone> ConvertVec for T { #[inline] - default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { - struct DropGuard<'a, T, A: Allocator> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { - vec: &'a mut Vec<T, A>, + default fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + vec: &'a mut Vec<T, A, COOP_PREFERRED>, num_init: usize, } - impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'a, T, A, COOP_PREFERRED> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn drop(&mut self) { // SAFETY: @@ -162,8 +162,8 @@ pub(crate) mod hack { #[cfg(not(no_global_oom_handling))] impl<T: Copy> ConvertVec for T { #[inline] - fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { let mut v = Vec::with_capacity_in(s.len(), alloc); // SAFETY: // allocated above with the capacity of `s`, and initialize to `s.len()` in diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 6372333bda81d..e0dc758b87b59 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -32,7 +32,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE pub(super) tail_len: usize, /// Current remaining range to remove pub(super) iter: slice::Iter<'a, T>, - pub(super) vec: NonNull<Vec<T, A>>, + pub(super) vec: NonNull<Vec<T, A, COOP_PREFERRED>>, } #[stable(feature = "collection_debug", since = "1.17.0")] diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index fdad67de09716..d07aad4724ccd 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -144,7 +144,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[cfg(not(no_global_oom_handling))] #[inline] - pub(crate) fn into_vecdeque(self) -> VecDeque<T, A> { + pub(crate) fn into_vecdeque(self) -> VecDeque<T, A, COOP_PREFERRED> { // Keep our `Drop` impl from dropping the elements and the allocator let mut this = ManuallyDrop::new(self); diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 1f8607a05a442..cd57a6b9ccbb1 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2483,7 +2483,7 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR /// assert_eq!(flattened.pop(), Some(6)); /// ``` #[unstable(feature = "slice_flatten", issue = "95629")] - pub fn into_flattened(self) -> Vec<T, A> { + pub fn into_flattened(self) -> Vec<T, A, COOP_PREFERRED> { let (ptr, len, cap, alloc) = self.into_raw_parts_with_alloc(); let (new_len, new_cap) = if T::IS_ZST { (len.checked_mul(N).expect("vec len overflow"), usize::MAX) @@ -3129,17 +3129,17 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A>> for Vec<T, A, COOP_PREFERRED> +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { - fn as_ref(&self) -> &Vec<T, A> { + fn as_ref(&self) -> &Vec<T, A, COOP_PREFERRED> { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A>> for Vec<T, A, COOP_PREFERRED> +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { - fn as_mut(&mut self) -> &mut Vec<T, A> { + fn as_mut(&mut self) -> &mut Vec<T, A, COOP_PREFERRED> { self } } @@ -3291,7 +3291,7 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR /// /// assert_eq!(Box::from(vec), vec![1, 2, 3].into_boxed_slice()); /// ``` - fn from(v: Vec<T, A>) -> Self { + fn from(v: Vec<T, A, COOP_PREFERRED>) -> Self { v.into_boxed_slice() } } @@ -3314,7 +3314,7 @@ impl From<&str> for Vec<u8> { #[stable(feature = "array_try_from_vec", since = "1.48.0")] impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, A, COOP_PREFERRED>> for [T; N] where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { - type Error = Vec<T, A>; + type Error = Vec<T, A, COOP_PREFERRED>; /// Gets the entire contents of the `Vec<T>` as an array, /// if its size exactly matches that of the requested array. @@ -3342,7 +3342,7 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR /// assert_eq!(a, b' '); /// assert_eq!(b, b'd'); /// ``` - fn try_from(mut vec: Vec<T, A>) -> Result<[T; N], Vec<T, A>> { + fn try_from(mut vec: Vec<T, A, COOP_PREFERRED>) -> Result<[T; N], Vec<T, A, COOP_PREFERRED>> { if vec.len() != N { return Err(vec); } diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index 81245a394b22e..87bdb3aaab7a5 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -10,28 +10,28 @@ pub(super) trait SpecExtend<T, I> { fn spec_extend(&mut self, iter: I); } -impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn spec_extend(&mut self, iter: I) { self.extend_desugared(iter) } } -impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn spec_extend(&mut self, iterator: I) { self.extend_trusted(iterator) } } -impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, IntoIter<T>> for Vec<T, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn spec_extend(&mut self, mut iterator: IntoIter<T>) { unsafe { self.append_elements(iterator.as_slice() as _); @@ -40,21 +40,21 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { } } -impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec<T, A> +impl<'a, T: 'a, I, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, I> for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = &'a T>, T: Clone, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.cloned()) } } -impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A> +impl<'a, T: 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots::<A>()]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index 7f95539b12d96..682375a607837 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -8,13 +8,13 @@ use super::{ExtendElement, IsZero, Vec}; // Specialization trait used for Vec::from_elem pub(super) trait SpecFromElem: Sized { - fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ; + fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: Self, n: usize, alloc: A) -> Vec<Self, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots::<A>(COOP_PREFERRED)]: ; } impl<T: Clone> SpecFromElem for T { - default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: Self, n: usize, alloc: A) -> Vec<Self, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { let mut v = Vec::with_capacity_in(n, alloc); v.extend_with(n, ExtendElement(elem)); v @@ -23,8 +23,8 @@ impl<T: Clone> SpecFromElem for T { impl<T: Clone + IsZero> SpecFromElem for T { #[inline] - default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: T, n: usize, alloc: A) -> Vec<T, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { if elem.is_zero() { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } @@ -36,8 +36,8 @@ impl<T: Clone + IsZero> SpecFromElem for T { impl SpecFromElem for i8 { #[inline] - fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: i8, n: usize, alloc: A) -> Vec<i8, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } @@ -52,8 +52,8 @@ impl SpecFromElem for i8 { impl SpecFromElem for u8 { #[inline] - fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> - where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { + fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: u8, n: usize, alloc: A) -> Vec<u8, A, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } From c6fa9aafc280db8c2b811919413ca8468d520798 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 12 Dec 2022 13:55:14 -0800 Subject: [PATCH 08/47] Vec & VecDeque use cases have COOP_PREFERRED. NOT COMPILABLE. --- .../alloc/src/collections/vec_deque/mod.rs | 6 ++-- library/alloc/src/raw_vec.rs | 8 ++--- library/alloc/src/vec/mod.rs | 30 ++++++++++++------- library/core/src/alloc/global.rs | 4 +-- library/core/src/alloc/mod.rs | 11 +++---- library/std/src/alloc.rs | 2 +- library/std/src/io/cursor.rs | 25 ++++++++++------ library/std/src/io/impls.rs | 3 +- .../std/src/sys/hermit/thread_local_dtor.rs | 4 ++- .../std/src/sys/solid/thread_local_dtor.rs | 4 ++- library/std/src/sys/unix/thread_local_dtor.rs | 5 +++- .../std/src/sys/windows/thread_local_dtor.rs | 5 +++- 12 files changed, 68 insertions(+), 39 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index d21b39feddffb..694d733f68a54 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -9,6 +9,7 @@ #![stable(feature = "rust1", since = "1.0.0")] use core::alloc::{self, GlobalAlloc}; +use crate::vec::DEFAULT_COOP_PREFERRED; use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; @@ -93,11 +94,10 @@ mod tests; #[cfg_attr(not(test), rustc_diagnostic_item = "VecDeque")] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_insignificant_dtor] -// @TODO pub struct VecDeque< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = true + const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED > where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { @@ -2908,7 +2908,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE if other.head != 0 { ptr::copy(buf.add(other.head), buf, len); } - // @TODO: + // @FIXME: COOP Vec::from_raw_parts_in(buf, len, cap, alloc) } } diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index b231d6785b0d9..58e8d72fc2d25 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -14,6 +14,7 @@ use crate::alloc::{Allocator, Global, Layout}; use crate::boxed::Box; use crate::collections::TryReserveError; use crate::collections::TryReserveErrorKind::*; +use crate::vec::DEFAULT_COOP_PREFERRED; #[cfg(test)] mod tests; @@ -49,8 +50,7 @@ enum AllocInit { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] -// @TODO apply `_coop` with logical && to `A::IsCoAllocator` -pub(crate) struct RawVec<T, A: Allocator = Global, const COOP_PREFERRED: bool = true> +pub(crate) struct RawVec<T, A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { ptr: Unique<T>, @@ -506,12 +506,12 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } } -// @TODO Custom +// @FIXME Custom unsafe impl<#[may_dangle] T, const COOP_PREFERRED: bool> Drop for RawVec<T, Global, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { - // @TODO + // @TOFIXMEDO if let Some((ptr, layout)) = self.current_memory() { unsafe { self.alloc.deallocate(ptr, layout) } } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index cd57a6b9ccbb1..cbd286054a9b5 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -397,24 +397,34 @@ mod spec_extend; #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")] #[rustc_insignificant_dtor] -// @TODO _coop -pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = true> +pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { buf: RawVec<T, A, COOP_PREFERRED>, len: usize, } -#[unstable(feature = "global_co_alloc_vec", issue = "none")] -pub type CoVec<T> = Vec<T, Global, true>; +#[unstable(feature = "global_co_alloc_covec", issue = "none")] +pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, true>; + +/// "Plain" Vec. Not "cooperative" - not carrying extra data to assist the allocator. +#[unstable(feature = "global_co_alloc_plvec", issue = "none")] +pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, false>; + +/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter. +#[unstable(feature = "global_co_alloc_def", issue = "none")] +pub const DEFAULT_COOP_PREFERRED: bool = true; + +/// "Default" Vec. Either "cooperative" or not - as specified by `DEFAULT_COOP_PREFERRED`. The +/// difference to `Vec` (used without specifying `COOP_PREFERRED`): `DefVec` indicates that the +/// author considered using `CoVec` or `PlVec`, but left it to default instead. +#[unstable(feature = "global_co_alloc_defvec", issue = "none")] +pub type DefVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, DEFAULT_COOP_PREFERRED>; -/// "Plain" Vec. -#[unstable(feature = "global_co_alloc_vec", issue = "none")] -pub type PlVec<T> = Vec<T, Global, false>; -/// "Weighted" Vec. -/// weight means how much it wants to cooperate. 0 = always pack; u8::MAX = always coop (if `Global` supports it). -/// @TODO Weighing on the side of Allocator - const fn. +/// "Weighted cooperative" Vec. Weight means how much it wants to cooperate (with the allocator). 0 +/// = always pack; u8::MAX = always cooperate (if `Global` supports it). +/// @FIXME A `pub const` threshold. #[unstable(feature = "global_co_alloc_vec", issue = "none")] pub type WeVec<T, const weight: u8> = Vec<T, Global, {weight>1}>; diff --git a/library/core/src/alloc/global.rs b/library/core/src/alloc/global.rs index fd47d6cf88025..b0d457ff8c4d9 100644 --- a/library/core/src/alloc/global.rs +++ b/library/core/src/alloc/global.rs @@ -166,7 +166,7 @@ pub unsafe trait GlobalAlloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8; #[unstable(feature = "global_co_alloc", issue = "none")] - unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) {panic!("TODO")} + unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) {panic!("@FIXME")} /// Deallocate the block of memory at the given `ptr` pointer with the given `layout`. /// @@ -184,7 +184,7 @@ pub unsafe trait GlobalAlloc { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout); #[unstable(feature = "global_co_alloc", issue = "none")] - unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) {panic!("TODO")} + unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) {panic!("@FIXME")} /// Behaves like `alloc`, but also ensures that the contents /// are set to zero before being returned. diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index d49e94402a7ed..f67bdf35b9416 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -26,7 +26,7 @@ use crate::error::Error; use crate::fmt; use crate::ptr::{self, NonNull}; -// @TODO Make this target-specific +// @FIXME Make this target-specific #[unstable(feature = "global_co_alloc_meta", issue = "none")] #[allow(missing_debug_implementations)] #[derive(Clone, Copy)] @@ -157,7 +157,8 @@ pub unsafe trait Allocator { // Can't have: const type Xyz; /// If this is any type with non-zero size, then the actual `Allocator` implementation supports cooperative functions (`co_*`) as first class citizens. //type IsCoAllocator = (); - // It applies to the global (default) allocator only. And/or System allocator?! TODO + // It applies to the global (default) allocator only. And/or System allocator?! @FIXME + // @FIXME make false by default const IS_CO_ALLOCATOR: bool = true; /// Attempts to allocate a block of memory. @@ -182,7 +183,7 @@ pub unsafe trait Allocator { /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>; - fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) {panic!("TODO")} + fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) {panic!("FIXME")} /// Behaves like `allocate`, but also ensures that the returned memory is zero-initialized. /// @@ -230,7 +231,7 @@ pub unsafe trait Allocator { /// [*fit*]: #memory-fitting unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout); - unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("TODO")} + unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("FIXME")} /// Attempts to extend the memory block. /// @@ -528,7 +529,7 @@ pub unsafe trait Allocator { } } -// @TODO +// @FIXME #[unstable(feature = "allocator_api", issue = "32838")] unsafe impl<A> Allocator for &A where diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index bee6edbc40731..6969c7d81dd5e 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -197,7 +197,7 @@ impl System { } } -// @TODO +// @FIXME // The Allocator impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl, // which is in `std::sys::*::alloc`. #[unstable(feature = "allocator_api", issue = "32838")] diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index d98ab021cadb1..33d1ee065de2a 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -6,6 +6,7 @@ use crate::io::prelude::*; use crate::alloc::Allocator; use crate::cmp; use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, SeekFrom}; +use core::alloc; /// A `Cursor` wraps an in-memory buffer and provides it with a /// [`Seek`] implementation. @@ -397,11 +398,12 @@ fn slice_write_vectored( } /// Reserves the required space, and pads the vec with 0s if necessary. -fn reserve_and_pad<A: Allocator>( +fn reserve_and_pad<A: Allocator, const COOP_PREFERRED: bool>( pos_mut: &mut u64, - vec: &mut Vec<u8, A>, + vec: &mut Vec<u8, A, COOP_PREFERRED>, buf_len: usize, -) -> io::Result<usize> { +) -> io::Result<usize> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { let pos: usize = (*pos_mut).try_into().map_err(|_| { io::const_io_error!( ErrorKind::InvalidInput, @@ -440,9 +442,10 @@ fn reserve_and_pad<A: Allocator>( /// Writes the slice to the vec without allocating /// # Safety: vec must have buf.len() spare capacity -unsafe fn vec_write_unchecked<A>(pos: usize, vec: &mut Vec<u8, A>, buf: &[u8]) -> usize +unsafe fn vec_write_unchecked<A, const COOP_PREFERRED: bool>(pos: usize, vec: &mut Vec<u8, A, COOP_PREFERRED>, buf: &[u8]) -> usize where A: Allocator, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { debug_assert!(vec.capacity() >= pos + buf.len()); vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()); @@ -458,9 +461,10 @@ where /// This also allows for the vec body to be empty, but with a position of N. /// This means that [`Write`] will pad the vec with 0 initially, /// before writing anything from that point -fn vec_write<A>(pos_mut: &mut u64, vec: &mut Vec<u8, A>, buf: &[u8]) -> io::Result<usize> +fn vec_write<A, const COOP_PREFERRED: bool>(pos_mut: &mut u64, vec: &mut Vec<u8, A, COOP_PREFERRED>, buf: &[u8]) -> io::Result<usize> where A: Allocator, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { let buf_len = buf.len(); let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?; @@ -489,13 +493,14 @@ where /// This also allows for the vec body to be empty, but with a position of N. /// This means that [`Write`] will pad the vec with 0 initially, /// before writing anything from that point -fn vec_write_vectored<A>( +fn vec_write_vectored<A, const COOP_PREFERRED: bool>( pos_mut: &mut u64, - vec: &mut Vec<u8, A>, + vec: &mut Vec<u8, A, COOP_PREFERRED>, bufs: &[IoSlice<'_>], ) -> io::Result<usize> where A: Allocator, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { // For safety reasons, we don't want this sum to overflow ever. // If this saturates, the reserve should panic to avoid any unsound writing. @@ -543,9 +548,10 @@ impl Write for Cursor<&mut [u8]> { } #[stable(feature = "cursor_mut_vec", since = "1.25.0")] -impl<A> Write for Cursor<&mut Vec<u8, A>> +impl<A, const COOP_PREFERRED: bool> Write for Cursor<&mut Vec<u8, A, COOP_PREFERRED>> where A: Allocator, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, self.inner, buf) @@ -567,9 +573,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<A> Write for Cursor<Vec<u8, A>> +impl<A, const COOP_PREFERRED: bool> Write for Cursor<Vec<u8, A, COOP_PREFERRED>> where A: Allocator, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, &mut self.inner, buf) diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index d575d9fa3f990..ec3394e0b16c1 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -378,7 +378,8 @@ impl Write for &mut [u8] { /// Write is implemented for `Vec<u8>` by appending to the vector. /// The vector will grow as needed. #[stable(feature = "rust1", since = "1.0.0")] -impl<A: Allocator> Write for Vec<u8, A> { +impl<A: Allocator, const COOP_PREFERRED: bool> Write for Vec<u8, A, COOP_PREFERRED> +where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.extend_from_slice(buf); diff --git a/library/std/src/sys/hermit/thread_local_dtor.rs b/library/std/src/sys/hermit/thread_local_dtor.rs index 9b683fce15748..7f0c2b0b180cb 100644 --- a/library/std/src/sys/hermit/thread_local_dtor.rs +++ b/library/std/src/sys/hermit/thread_local_dtor.rs @@ -1,5 +1,6 @@ #![cfg(target_thread_local)] #![unstable(feature = "thread_local_internals", issue = "none")] +#![feature(global_co_alloc_plvec)] // Simplify dtor registration by using a list of destructors. // The this solution works like the implementation of macOS and @@ -7,11 +8,12 @@ use crate::cell::Cell; use crate::ptr; +use core::alloc::PlVec; #[thread_local] static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut()); -type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>; +type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>; pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { if DTORS.get().is_null() { diff --git a/library/std/src/sys/solid/thread_local_dtor.rs b/library/std/src/sys/solid/thread_local_dtor.rs index 9735645705776..c7cf2f02ebbd1 100644 --- a/library/std/src/sys/solid/thread_local_dtor.rs +++ b/library/std/src/sys/solid/thread_local_dtor.rs @@ -1,16 +1,18 @@ #![cfg(target_thread_local)] #![unstable(feature = "thread_local_internals", issue = "none")] +#![feature(global_co_alloc_plvec)] // Simplify dtor registration by using a list of destructors. use super::{abi, itron::task}; use crate::cell::Cell; use crate::ptr; +use core::alloc::PlVec; #[thread_local] static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut()); -type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>; +type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>; pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { if DTORS.get().is_null() { diff --git a/library/std/src/sys/unix/thread_local_dtor.rs b/library/std/src/sys/unix/thread_local_dtor.rs index d7fd2130f7cce..c09b0f7832b6a 100644 --- a/library/std/src/sys/unix/thread_local_dtor.rs +++ b/library/std/src/sys/unix/thread_local_dtor.rs @@ -1,5 +1,8 @@ #![cfg(target_thread_local)] #![unstable(feature = "thread_local_internals", issue = "none")] +#![feature(global_co_alloc_plvec)] + +use core::alloc::PlVec; //! Provides thread-local destructors without an associated "key", which //! can be more efficient. @@ -66,7 +69,7 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { REGISTERED.set(true); } - type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>; + type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>; #[thread_local] static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut()); diff --git a/library/std/src/sys/windows/thread_local_dtor.rs b/library/std/src/sys/windows/thread_local_dtor.rs index 9707a95dff21b..cbadd2dd23aea 100644 --- a/library/std/src/sys/windows/thread_local_dtor.rs +++ b/library/std/src/sys/windows/thread_local_dtor.rs @@ -3,10 +3,13 @@ #![unstable(feature = "thread_local_internals", issue = "none")] #![cfg(target_thread_local)] +#![feature(global_co_alloc_plvec)] + +use core::alloc::PlVec; // Using a per-thread list avoids the problems in synchronizing global state. #[thread_local] -static mut DESTRUCTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new(); +static mut DESTRUCTORS: PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new(); // Ensure this can never be inlined because otherwise this may break in dylibs. // See #44391. From 1549f7b254b90b4503267eb29ba0b315e68f7ab6 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 12 Dec 2022 14:19:28 -0800 Subject: [PATCH 09/47] Clean up: Applied x test tidy --bless. NOT COMPILABLE --- compiler/rustc_middle/src/mir/mod.rs | 2 +- compiler/rustc_middle/src/mir/syntax.rs | 2 +- .../rustc_parse/src/parser/attr_wrapper.rs | 2 +- compiler/rustc_parse/src/parser/mod.rs | 7 +- library/alloc/src/boxed.rs | 34 ++- .../alloc/src/collections/binary_heap/mod.rs | 12 +- .../alloc/src/collections/vec_deque/drain.rs | 77 ++++-- .../src/collections/vec_deque/into_iter.rs | 41 +++- .../alloc/src/collections/vec_deque/mod.rs | 154 ++++++++---- .../src/collections/vec_deque/spec_extend.rs | 35 ++- .../collections/vec_deque/spec_from_iter.rs | 19 +- library/alloc/src/raw_vec.rs | 65 +++-- library/alloc/src/slice.rs | 62 +++-- library/alloc/src/vec/drain.rs | 90 +++++-- library/alloc/src/vec/drain_filter.rs | 23 +- library/alloc/src/vec/into_iter.rs | 123 +++++++--- library/alloc/src/vec/mod.rs | 230 +++++++++++++----- library/alloc/src/vec/spec_extend.rs | 23 +- library/alloc/src/vec/spec_from_elem.rs | 51 +++- library/alloc/src/vec/splice.rs | 29 ++- library/alloc/tests/boxed.rs | 2 +- library/core/src/alloc/global.rs | 12 +- library/core/src/alloc/mod.rs | 33 +-- library/std/src/io/cursor.rs | 26 +- library/std/src/io/impls.rs | 12 +- library/std/src/sys/unix/thread_local_dtor.rs | 4 +- src/librustdoc/html/render/context.rs | 5 +- 27 files changed, 828 insertions(+), 347 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 5cab90c4cc3b5..61a8baee62196 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2,7 +2,6 @@ //! //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html -use core::alloc::GlobalCoAllocMeta; use crate::mir::interpret::{ AllocRange, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, LitToConstInput, Scalar, }; @@ -14,6 +13,7 @@ use crate::ty::visit::{TypeVisitable, TypeVisitor}; use crate::ty::{self, DefIdTree, List, Ty, TyCtxt}; use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex}; use crate::ty::{GenericArg, InternalSubsts, SubstsRef}; +use core::alloc::GlobalCoAllocMeta; use rustc_data_structures::captures::Captures; use rustc_errors::ErrorGuaranteed; diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index cfaecf0775ff4..36cc4fa4cce3b 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -3,9 +3,9 @@ //! This is in a dedicated file so that changes to this file can be reviewed more carefully. //! The intention is that this file only contains datatype declarations, no code. +use super::{BasicBlock, Constant, Field, Local, SwitchTargets, UserTypeProjection}; use core::alloc::GlobalCoAllocMeta; use core::mem; -use super::{BasicBlock, Constant, Field, Local, SwitchTargets, UserTypeProjection}; use crate::mir::coverage::{CodeRegion, CoverageKind}; use crate::traits::Reveal; diff --git a/compiler/rustc_parse/src/parser/attr_wrapper.rs b/compiler/rustc_parse/src/parser/attr_wrapper.rs index a8c2d8c2c59d3..df02751005ce8 100644 --- a/compiler/rustc_parse/src/parser/attr_wrapper.rs +++ b/compiler/rustc_parse/src/parser/attr_wrapper.rs @@ -1,6 +1,6 @@ +use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCursor, TrailingToken}; use core::alloc::GlobalCoAllocMeta; use core::mem; -use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCursor, TrailingToken}; use rustc_ast::token::{self, Delimiter, Token, TokenKind}; use rustc_ast::tokenstream::{AttrTokenStream, AttributesData, ToAttrTokenStream}; use rustc_ast::tokenstream::{AttrTokenTree, DelimSpan, LazyAttrTokenStream, Spacing}; diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index e8567a650d85f..8b66ec971b006 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -11,8 +11,8 @@ mod stmt; mod ty; use crate::lexer::UnmatchedBrace; -use core::alloc::GlobalCoAllocMeta; pub use attr_wrapper::AttrWrapper; +use core::alloc::GlobalCoAllocMeta; pub use diagnostics::AttemptLocalParseRecovery; pub(crate) use item::FnParseMode; pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma}; @@ -169,7 +169,10 @@ pub struct Parser<'a> { // This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure // it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Parser<'_>, 336 + 4 * mem::size_of::<GlobalCoAllocMeta>()); +rustc_data_structures::static_assert_size!( + Parser<'_>, + 336 + 4 * mem::size_of::<GlobalCoAllocMeta>() +); /// Stores span information about a closure. #[derive(Clone)] diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 9dc695559a80e..021f36e6770bd 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -699,7 +699,12 @@ impl<T> Box<[T]> { Err(_) => return Err(AllocError), }; let ptr = Global.allocate(layout)?; - Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) + Ok(RawVec::<T, Global, false>::from_raw_parts_in( + ptr.as_mut_ptr() as *mut _, + len, + Global, + ) + .into_box(len)) } } @@ -731,13 +736,20 @@ impl<T> Box<[T]> { Err(_) => return Err(AllocError), }; let ptr = Global.allocate_zeroed(layout)?; - Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) + Ok(RawVec::<T, Global, false>::from_raw_parts_in( + ptr.as_mut_ptr() as *mut _, + len, + Global, + ) + .into_box(len)) } } } impl<T, A: Allocator> Box<[T], A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, +{ /// Constructs a new boxed slice with uninitialized contents in the provided allocator. /// /// # Examples @@ -765,7 +777,10 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { - unsafe { RawVec::<T, A, {alloc::SHORT_TERM_VEC_PREFERS_COOP}>::with_capacity_in(len, alloc).into_box(len) } + unsafe { + RawVec::<T, A, { alloc::SHORT_TERM_VEC_PREFERS_COOP }>::with_capacity_in(len, alloc) + .into_box(len) + } } /// Constructs a new boxed slice with uninitialized contents in the provided allocator, @@ -793,7 +808,12 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { - unsafe { RawVec::<T, A, {alloc::SHORT_TERM_VEC_PREFERS_COOP}>::with_capacity_zeroed_in(len, alloc).into_box(len) } + unsafe { + RawVec::<T, A, { alloc::SHORT_TERM_VEC_PREFERS_COOP }>::with_capacity_zeroed_in( + len, alloc, + ) + .into_box(len) + } } } @@ -2022,7 +2042,9 @@ impl<I> FromIterator<I> for Box<[I]> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_slice_clone", since = "1.3.0")] impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, +{ fn clone(&self) -> Self { let alloc = Box::allocator(self).clone(); self.to_vec_in(alloc).into_boxed_slice() diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index 7c6b0bb6e6c75..3d10072ce43b4 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -143,12 +143,12 @@ #![allow(missing_docs)] #![stable(feature = "rust1", since = "1.0.0")] -use core::{alloc, fmt}; use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen}; use core::mem::{self, swap, ManuallyDrop}; use core::num::NonZeroUsize; use core::ops::{Deref, DerefMut}; use core::ptr; +use core::{alloc, fmt}; use crate::alloc::Global; @@ -1243,7 +1243,7 @@ impl<T> BinaryHeap<T> { /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] - pub fn drain(&mut self) -> Drain<'_, T, {alloc::SHORT_TERM_VEC_PREFERS_COOP}> { + pub fn drain(&mut self) -> Drain<'_, T, { alloc::SHORT_TERM_VEC_PREFERS_COOP }> { Drain { iter: self.data.drain(..) } } @@ -1524,13 +1524,17 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {} #[stable(feature = "drain", since = "1.6.0")] #[derive(Debug)] pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ iter: vec::Drain<'a, T, Global, COOP_PREFERRED>, } #[stable(feature = "drain", since = "1.6.0")] impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ type Item = T; #[inline] diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index bab4919eb6032..0bcf82e3ceff5 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -19,9 +19,10 @@ pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP} -> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, +> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ // We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T // and we want it to be covariant instead deque: NonNull<VecDeque<T, A, COOP_PREFERRED>>, @@ -37,7 +38,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ pub(super) unsafe fn new( deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, drain_start: usize, @@ -91,8 +94,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain") .field(&self.drain_len) @@ -104,21 +110,37 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn drop(&mut self) { - struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> (&'r mut Drain<'a, T, A, COOP_PREFERRED>) - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; + struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool>( + &'r mut Drain<'a, T, A, COOP_PREFERRED>, + ) + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; - impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop + for DropGuard<'r, 'a, T, A, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { fn drop(&mut self) { if self.0.remaining != 0 { unsafe { @@ -200,7 +222,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = T; #[inline] @@ -222,8 +246,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn next_back(&mut self) -> Option<T> { if self.remaining == 0 { @@ -236,9 +263,15 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +{ +} diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index 477ab0c4db594..7924098c14db2 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -1,5 +1,5 @@ -use core::{alloc, fmt}; use core::iter::{FusedIterator, TrustedLen}; +use core::{alloc, fmt}; use crate::alloc::{Allocator, Global}; @@ -17,14 +17,17 @@ use super::VecDeque; pub struct IntoIter< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = true -> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + const COOP_PREFERRED: bool = true, +> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ inner: VecDeque<T, A, COOP_PREFERRED>, } impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ pub(super) fn new(inner: VecDeque<T, A, COOP_PREFERRED>) -> Self { IntoIter { inner } } @@ -36,7 +39,9 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[stable(feature = "collection_debug", since = "1.17.0")] impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.inner).finish() } @@ -44,7 +49,9 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator> Iterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ type Item = T; #[inline] @@ -61,7 +68,9 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ #[inline] fn next_back(&mut self) -> Option<T> { self.inner.pop_back() @@ -70,16 +79,22 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ fn is_empty(&self) -> bool { self.inner.is_empty() } } #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<T, A: Allocator> FusedIterator for IntoIter<T, A> where + [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ +} #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> where + [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ +} diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 694d733f68a54..b441f23ed23f3 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -6,10 +6,9 @@ //! contained type is sendable. #![feature(global_co_alloc)] - #![stable(feature = "rust1", since = "1.0.0")] -use core::alloc::{self, GlobalAlloc}; use crate::vec::DEFAULT_COOP_PREFERRED; +use core::alloc::{self, GlobalAlloc}; use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; @@ -97,9 +96,9 @@ mod tests; pub struct VecDeque< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED -> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, +> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // `self[0]`, if it exists, is `buf[head]`. // `head < buf.capacity()`, unless `buf.capacity() == 0` when `head == 0`. @@ -112,8 +111,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn clone(&self) -> Self { let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone()); deq.extend(self.iter().cloned()); @@ -127,8 +129,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn drop(&mut self) { /// Runs the destructor for all items in the slice when it gets dropped (normally or /// during unwinding). @@ -162,7 +167,9 @@ impl<T, const COOP_PREFERRED: bool = true> Default for VecDeque<T, Global, COOP_ } impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Marginally more convenient #[inline] fn ptr(&self) -> *mut T { @@ -452,13 +459,17 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE len: usize, ) -> usize { struct Guard<'a, T, A: Allocator, const COOP_PREFERRED: bool> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, written: usize, } impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for Guard<'a, T, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { fn drop(&mut self) { self.deque.len += self.written; } @@ -536,8 +547,10 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } } -impl<T, Global, const COOP_PREFERRED: bool> VecDeque<T, Global, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, Global, const COOP_PREFERRED: bool> VecDeque<T, Global, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Creates an empty deque. /// /// # Examples @@ -574,7 +587,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Creates an empty deque. /// /// # Examples @@ -2610,7 +2625,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Modifies the deque in-place so that `len()` is equal to new_len, /// either by removing excess elements from the back or by appending clones of `value` /// to the back. @@ -2655,8 +2672,11 @@ fn wrap_index(logical_index: usize, capacity: usize) -> usize { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> PartialEq for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> PartialEq + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn eq(&self, other: &Self) -> bool { if self.len != other.len() { return false; @@ -2695,8 +2715,10 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for VecDeque<T, A, COOP_PREFERRED> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +{ +} __impl_slice_eq1! { [] VecDeque<T, A, COOP_PREFERRED>, Vec<U, A, COOP_PREFERRED>, } __impl_slice_eq1! { [] VecDeque<T, A, COOP_PREFERRED>, &[U], } @@ -2706,8 +2728,11 @@ __impl_slice_eq1! { [const N: usize] VecDeque<T, A, COOP_PREFERRED>, &[U; N], } __impl_slice_eq1! { [const N: usize] VecDeque<T, A, COOP_PREFERRED>, &mut [U; N], } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.iter().partial_cmp(other.iter()) } @@ -2715,7 +2740,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "rust1", since = "1.0.0")] impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn cmp(&self, other: &Self) -> Ordering { self.iter().cmp(other.iter()) @@ -2724,7 +2751,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "rust1", since = "1.0.0")] impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn hash<H: Hasher>(&self, state: &mut H) { state.write_length_prefix(self.len); // It's not possible to use Hash::hash_slice on slices @@ -2739,7 +2768,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Index<usize> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Output = T; #[inline] @@ -2750,7 +2781,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IndexMut<usize> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, index: usize) -> &mut T { self.get_mut(index).expect("Out of bounds access") @@ -2759,7 +2792,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "rust1", since = "1.0.0")] impl<T, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, GlobalAlloc, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> { SpecFromIter::spec_from_iter(iter.into_iter()) } @@ -2767,7 +2802,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = T; type IntoIter = IntoIter<T, A>; @@ -2779,8 +2816,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator + for &'a VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -2790,8 +2830,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator + for &'a mut VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = &'a mut T; type IntoIter = IterMut<'a, T>; @@ -2802,7 +2845,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()); } @@ -2819,8 +2864,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a, T: 'a + Copy, A: Allocator, const COOP_PREFERRED: bool> Extend<&'a T> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<'a, T: 'a + Copy, A: Allocator, const COOP_PREFERRED: bool> Extend<&'a T> + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()); } @@ -2837,16 +2885,22 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() } } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Turn a [`Vec<T>`] into a [`VecDeque<T>`]. /// /// [`Vec<T>`]: crate::vec::Vec @@ -2857,15 +2911,20 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// any additional memory. #[inline] fn from<const OTHER_COOP_PREFERRED: bool>(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(OTHER_COOP_PREFERRED)]: { + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(OTHER_COOP_PREFERRED)]:, + { let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } } } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> From<VecDeque<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> From<VecDeque<T, A, COOP_PREFERRED>> + for Vec<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Turn a [`VecDeque<T>`] into a [`Vec<T>`]. /// /// [`Vec<T>`]: crate::vec::Vec @@ -2895,7 +2954,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// assert_eq!(vec, [8, 9, 1, 2, 3, 4]); /// assert_eq!(vec.as_ptr(), ptr); /// ``` - fn from<const _VECDEQUE_COOP_PREFERRED: bool>(mut other: VecDeque<T, A, _VECDEQUE_COOP_PREFERRED>) -> Self { + fn from<const _VECDEQUE_COOP_PREFERRED: bool>( + mut other: VecDeque<T, A, _VECDEQUE_COOP_PREFERRED>, + ) -> Self { other.make_contiguous(); unsafe { @@ -2915,8 +2976,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "std_collections_from_array", since = "1.56.0")] -impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]> for VecDeque<T, Global, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]> + for VecDeque<T, Global, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Converts a `[T; N]` into a `VecDeque<T>`. /// /// ``` diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index cdeda1a57bed5..b1a9d23fd4a99 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -13,10 +13,11 @@ pub(super) trait SpecExtend<T, I> { fn spec_extend(&mut self, iter: I); } -impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> + for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, mut iter: I) { // This function should be the moral equivalent of: @@ -26,8 +27,12 @@ where // } // May only be called if `deque.len() < deque.capacity()` - unsafe fn push_unchecked<T, A: Allocator, const COOP_PREFERRED: bool>(deque: &mut VecDeque<T, A, COOP_PREFERRED>, element: T) - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + unsafe fn push_unchecked<T, A: Allocator, const COOP_PREFERRED: bool>( + deque: &mut VecDeque<T, A, COOP_PREFERRED>, + element: T, + ) where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { // SAFETY: Because of the precondition, it's guaranteed that there is space // in the logical array after the last element. unsafe { deque.buffer_write(deque.to_physical_idx(deque.len), element) }; @@ -54,10 +59,11 @@ where } } -impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> + for VecDeque<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iter: I) { // This is the case for a TrustedLen iterator. @@ -90,8 +96,11 @@ where } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, vec::IntoIter<T>> + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) { let slice = iterator.as_slice(); self.reserve(slice.len()); @@ -104,21 +113,23 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } } -impl<'a, T: 'a, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, I> for VecDeque<T, A, COOP_PREFERRED> +impl<'a, T: 'a, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, I> + for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = &'a T>, T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.copied()) } } -impl<'a, T: 'a, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque<T, A, COOP_PREFERRED> +impl<'a, T: 'a, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, slice::Iter<'a, T>> + for VecDeque<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index b9641ef156c88..85dd756337350 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -7,10 +7,11 @@ pub(super) trait SpecFromIter<T, I> { fn spec_from_iter(iter: I) -> Self; } -impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for VecDeque<T, A, COOP_PREFERRED> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> + for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_from_iter(iterator: I) -> Self { // Since converting is O(1) now, just re-use the `Vec` logic for @@ -21,16 +22,22 @@ where } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T>> + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self { iterator.into_vecdeque() } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> + for VecDeque<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn spec_from_iter(iterator: IntoIter<T>) -> Self { iterator.into_vecdeque() diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 58e8d72fc2d25..84fd954edbd7a 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -1,6 +1,6 @@ #![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")] -use core::alloc::{self, LayoutError, GlobalCoAllocMeta, PtrAndMeta}; +use core::alloc::{self, GlobalCoAllocMeta, LayoutError, PtrAndMeta}; use core::cmp; use core::intrinsics; use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; @@ -50,8 +50,12 @@ enum AllocInit { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] -pub(crate) struct RawVec<T, A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +pub(crate) struct RawVec< + T, + A: Allocator = Global, + const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, +> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { ptr: Unique<T>, cap: usize, @@ -59,11 +63,14 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE // As of v1.67.0, `cmp` for `TypeId` is not `const`, unfortunately: //pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}], //pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()], - pub(crate) metas: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], + pub(crate) metas: [GlobalCoAllocMeta; + alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], } impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so /// they cannot call `Self::new()`. /// @@ -111,7 +118,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PRE } impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // Tiny Vecs are dumb. Skip to: // - 8 if the element size is 1, because any heap allocators is likely @@ -130,8 +138,13 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// the returned `RawVec`. pub const fn new_in(alloc: A) -> Self { // `cap: 0` means "unallocated". zero-sized types are ignored. - Self { ptr: Unique::dangling(), cap: 0, alloc, - metas: [GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/}; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)] } + Self { + ptr: Unique::dangling(), + cap: 0, + alloc, + metas: [GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/}; + alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], + } } /// Like `with_capacity`, but parameterized over the choice of @@ -208,7 +221,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) }, cap: capacity, alloc, - metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)] + metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; + alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], } } } @@ -225,7 +239,13 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// guaranteed. #[inline] pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { - Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap: capacity, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)] } + Self { + ptr: unsafe { Unique::new_unchecked(ptr) }, + cap: capacity, + alloc, + metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; + alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], + } } /// Gets a raw pointer to the start of the allocation. Note that this is @@ -293,8 +313,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE slf: &mut RawVec<T, A, COOP_PREFERRED>, len: usize, additional: usize, - ) - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + ) where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { handle_reserve(slf.grow_amortized(len, additional)); } @@ -369,17 +389,16 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Returns if the buffer needs to grow to fulfill the needed extra capacity. /// Mainly used to make inlining reserve-calls possible without inlining `grow`. - fn needs_to_grow(&self, len: usize, additional: usize) -> bool - { + fn needs_to_grow(&self, len: usize, additional: usize) -> bool { additional > self.capacity().wrapping_sub(len) } - fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) - { + fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) { // Allocators currently return a `NonNull<[u8]>` whose length matches // the size requested. If that ever changes, the capacity here should // change to `ptr.len() / mem::size_of::<T>()`. @@ -490,15 +509,17 @@ where memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into()) } -unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for RawVec<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop + for RawVec<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. default fn drop(&mut self) { if let Some((ptr, layout)) = self.current_memory() { if A::IS_CO_ALLOCATOR && COOP_PREFERRED { let meta = self.metas[0]; - unsafe { self.alloc.co_deallocate(PtrAndMeta {ptr, meta}, layout) } + unsafe { self.alloc.co_deallocate(PtrAndMeta { ptr, meta }, layout) } } else { unsafe { self.alloc.deallocate(ptr, layout) } } @@ -508,7 +529,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE // @FIXME Custom unsafe impl<#[may_dangle] T, const COOP_PREFERRED: bool> Drop for RawVec<T, Global, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { // @TOFIXMEDO diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 3f45f8b587375..5e44d3c8e0646 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -95,8 +95,12 @@ pub(crate) mod hack { // We shouldn't add inline attribute to this since this is used in // `vec!` macro mostly and causes perf regression. See #71204 for // discussion and perf results. - pub fn into_vec<T, A: Allocator, const COOP_PREFERRED: bool>(b: Box<[T], A>) -> Vec<T, A, COOP_PREFERRED> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + pub fn into_vec<T, A: Allocator, const COOP_PREFERRED: bool>( + b: Box<[T], A>, + ) -> Vec<T, A, COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { unsafe { let len = b.len(); let (b, alloc) = Box::into_raw_with_allocator(b); @@ -106,14 +110,22 @@ pub(crate) mod hack { #[cfg(not(no_global_oom_handling))] #[inline] - pub fn to_vec<T: ConvertVec, A: Allocator, const COOP_PREFERRED: bool>(s: &[T], alloc: A) -> Vec<T, A, COOP_PREFERRED> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + pub fn to_vec<T: ConvertVec, A: Allocator, const COOP_PREFERRED: bool>( + s: &[T], + alloc: A, + ) -> Vec<T, A, COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { T::to_vec(s, alloc) } #[cfg(not(no_global_oom_handling))] pub trait ConvertVec { - fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED> + fn to_vec<A: Allocator, const COOP_PREFERRED: bool>( + s: &[Self], + alloc: A, + ) -> Vec<Self, A, COOP_PREFERRED> where Self: Sized, [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; @@ -122,15 +134,24 @@ pub(crate) mod hack { #[cfg(not(no_global_oom_handling))] impl<T: Clone> ConvertVec for T { #[inline] - default fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + default fn to_vec<A: Allocator, const COOP_PREFERRED: bool>( + s: &[Self], + alloc: A, + ) -> Vec<Self, A, COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { vec: &'a mut Vec<T, A, COOP_PREFERRED>, num_init: usize, } impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'a, T, A, COOP_PREFERRED> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { #[inline] fn drop(&mut self) { // SAFETY: @@ -162,8 +183,13 @@ pub(crate) mod hack { #[cfg(not(no_global_oom_handling))] impl<T: Copy> ConvertVec for T { #[inline] - fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + fn to_vec<A: Allocator, const COOP_PREFERRED: bool>( + s: &[Self], + alloc: A, + ) -> Vec<Self, A, COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { let mut v = Vec::with_capacity_in(s.len(), alloc); // SAFETY: // allocated above with the capacity of `s`, and initialize to `s.len()` in @@ -441,7 +467,7 @@ impl<T> [T] { pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A> where T: Clone, - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, { // N.B., see the `hack` module in this file for more details. hack::to_vec(self, alloc) @@ -465,7 +491,9 @@ impl<T> [T] { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> - where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { + where + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, + { // N.B., see the `hack` module in this file for more details. hack::into_vec(self) } @@ -777,7 +805,9 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Borrow<[T]> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn borrow(&self) -> &[T] { &self[..] } @@ -785,7 +815,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> BorrowMut<[T]> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn borrow_mut(&mut self) -> &mut [T] { &mut self[..] } diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index e0dc758b87b59..d7f7292e4f1cf 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -23,9 +23,10 @@ pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, - const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP} -> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, +> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Index of tail to preserve pub(super) tail_start: usize, /// Length of tail @@ -36,15 +37,20 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() } } impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Returns the remaining items of this iterator as a slice. /// /// # Examples @@ -144,22 +150,34 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Drain<'a, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn as_ref(&self) -> &[T] { self.as_slice() } } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Sync, A: Sync + Allocator, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T: Sync, A: Sync + Allocator, const COOP_PREFERRED: bool> Sync + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<T: Send, A: Send + Allocator, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T: Send, A: Send + Allocator, const COOP_PREFERRED: bool> Send + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = T; #[inline] @@ -173,8 +191,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn next_back(&mut self) -> Option<T> { self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) }) @@ -183,14 +204,22 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn drop(&mut self) { /// Moves back the un-`Drain`ed elements to restore the original `Vec`. - struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool>(&'r mut Drain<'a, T, A, COOP_PREFERRED>) - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: ; - - impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool>( + &'r mut Drain<'a, T, A, COOP_PREFERRED>, + ) + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; + + impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop + for DropGuard<'r, 'a, T, A, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { fn drop(&mut self) { if self.0.tail_len > 0 { unsafe { @@ -254,17 +283,26 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "drain", since = "1.6.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn is_empty(&self) -> bool { self.iter.is_empty() } } #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen + for Drain<'_, T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +{ +} diff --git a/library/alloc/src/vec/drain_filter.rs b/library/alloc/src/vec/drain_filter.rs index 7be4abacbec96..89baafca46729 100644 --- a/library/alloc/src/vec/drain_filter.rs +++ b/library/alloc/src/vec/drain_filter.rs @@ -24,10 +24,10 @@ pub struct DrainFilter< T, F, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = true + const COOP_PREFERRED: bool = true, > where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { pub(super) vec: &'a mut Vec<T, A, COOP_PREFERRED>, /// The index of the item that will be inspected by the next call to `next`. @@ -49,7 +49,7 @@ pub struct DrainFilter< impl<T, F, A: Allocator, const COOP_PREFERRED: bool> DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Returns a reference to the underlying allocator. #[unstable(feature = "allocator_api", issue = "32838")] @@ -115,10 +115,11 @@ where } #[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] -impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Iterator for DrainFilter<'_, T, F, A, COOP_PREFERRED> +impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Iterator + for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -154,24 +155,26 @@ where } #[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] -impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Drop for DrainFilter<'_, T, F, A, COOP_PREFERRED> +impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Drop + for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { struct BackshiftOnDrop<'a, 'b, T, F, A: Allocator, const COOP_PREFERRED: bool> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { drain: &'b mut DrainFilter<'a, T, F, A, COOP_PREFERRED>, } - impl<'a, 'b, T, F, A: Allocator, const COOP_PREFERRED: bool> Drop for BackshiftOnDrop<'a, 'b, T, F, A, COOP_PREFERRED> + impl<'a, 'b, T, F, A: Allocator, const COOP_PREFERRED: bool> Drop + for BackshiftOnDrop<'a, 'b, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index d07aad4724ccd..790e6323f514e 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -4,7 +4,6 @@ use crate::alloc::{Allocator, Global}; #[cfg(not(no_global_oom_handling))] use crate::collections::VecDeque; use crate::raw_vec::RawVec; -use core::{alloc, array, fmt}; use core::iter::{ FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccessNoCoerce, }; @@ -14,6 +13,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; use core::ops::Deref; use core::ptr::{self, NonNull}; use core::slice::{self}; +use core::{alloc, array, fmt}; /// An iterator that moves out of a vector. /// @@ -31,9 +31,10 @@ use core::slice::{self}; pub struct IntoIter< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP} -> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, +> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ pub(super) buf: NonNull<T>, pub(super) phantom: PhantomData<T>, pub(super) cap: usize, @@ -47,15 +48,20 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "vec_intoiter_debug", since = "1.13.0")] -impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.as_slice()).finish() } } impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Returns the remaining items of this iterator as a slice. /// /// # Examples @@ -172,22 +178,34 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE #[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn as_ref(&self) -> &[T] { self.as_slice() } } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = T; #[inline] @@ -301,8 +319,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn next_back(&mut self) -> Option<T> { if self.end == self.ptr { @@ -343,20 +364,29 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn is_empty(&self) -> bool { self.ptr == self.end } } #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, A, COOP_PREFERRED> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +{ +} #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[doc(hidden)] #[unstable(issue = "none", feature = "std_internals")] @@ -372,18 +402,22 @@ impl<T: Copy> NonDrop for T {} #[unstable(issue = "none", feature = "std_internals")] // TrustedRandomAccess (without NoCoerce) must not be implemented because // subtypes/supertypes of `T` might not be `NonDrop` -unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedRandomAccessNoCoerce for IntoIter<T, A, COOP_PREFERRED> +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedRandomAccessNoCoerce + for IntoIter<T, A, COOP_PREFERRED> where T: NonDrop, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { const MAY_HAVE_SIDE_EFFECT: bool = false; } #[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_into_iter_clone", since = "1.8.0")] -impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[cfg(not(test))] fn clone(&self) -> Self { self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter() @@ -395,20 +429,32 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn drop(&mut self) { - struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool>(&'a mut IntoIter<T, A, COOP_PREFERRED>) - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: ; + struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool>( + &'a mut IntoIter<T, A, COOP_PREFERRED>, + ) + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'_, T, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { fn drop(&mut self) { unsafe { // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec let alloc = ManuallyDrop::take(&mut self.0.alloc); // RawVec handles deallocation - let _ = RawVec::<T, A, true>::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc); + let _ = RawVec::<T, A, true>::from_raw_parts_in( + self.0.buf.as_ptr(), + self.0.cap, + alloc, + ); } } } @@ -426,13 +472,20 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE // also refer to the vec::in_place_collect module documentation to get an overview #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] -unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> InPlaceIterable for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> InPlaceIterable + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ +} #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] -unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> SourceIter for IntoIter<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> SourceIter + for IntoIter<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Source = Self; #[inline] diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index cbd286054a9b5..c53e3d3ad483f 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -397,19 +397,25 @@ mod spec_extend; #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")] #[rustc_insignificant_dtor] -pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +pub struct Vec< + T, + #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, + const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, +> where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { buf: RawVec<T, A, COOP_PREFERRED>, len: usize, } #[unstable(feature = "global_co_alloc_covec", issue = "none")] -pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, true>; +pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = + Vec<T, A, true>; /// "Plain" Vec. Not "cooperative" - not carrying extra data to assist the allocator. #[unstable(feature = "global_co_alloc_plvec", issue = "none")] -pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, false>; +pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = + Vec<T, A, false>; /// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter. #[unstable(feature = "global_co_alloc_def", issue = "none")] @@ -419,14 +425,14 @@ pub const DEFAULT_COOP_PREFERRED: bool = true; /// difference to `Vec` (used without specifying `COOP_PREFERRED`): `DefVec` indicates that the /// author considered using `CoVec` or `PlVec`, but left it to default instead. #[unstable(feature = "global_co_alloc_defvec", issue = "none")] -pub type DefVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, DEFAULT_COOP_PREFERRED>; - +pub type DefVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = + Vec<T, A, DEFAULT_COOP_PREFERRED>; /// "Weighted cooperative" Vec. Weight means how much it wants to cooperate (with the allocator). 0 /// = always pack; u8::MAX = always cooperate (if `Global` supports it). /// @FIXME A `pub const` threshold. #[unstable(feature = "global_co_alloc_vec", issue = "none")] -pub type WeVec<T, const weight: u8> = Vec<T, Global, {weight>1}>; +pub type WeVec<T, const weight: u8> = Vec<T, Global, { weight > 1 }>; //////////////////////////////////////////////////////////////////////////////// // Inherent methods @@ -620,7 +626,9 @@ impl<T> Vec<T> { } impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Constructs a new, empty `Vec<T, A>`. /// /// The vector will not allocate until elements are pushed onto it. @@ -1633,8 +1641,10 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR // This drop guard will be invoked when predicate or `drop` of element panicked. // It shifts unchecked elements to cover holes and `set_len` to the correct length. // In cases when predicate and `drop` never panick, it will be optimized out. - struct BackshiftOnDrop<'a, T, A: Allocator, const VEC_IS_COOP: bool=true> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]: { + struct BackshiftOnDrop<'a, T, A: Allocator, const VEC_IS_COOP: bool = true> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]:, + { v: &'a mut Vec<T, A, VEC_IS_COOP>, processed_len: usize, deleted_cnt: usize, @@ -1642,7 +1652,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } impl<T, A: Allocator, const VEC_IS_COOP: bool> Drop for BackshiftOnDrop<'_, T, A, VEC_IS_COOP> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]: { + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]:, + { fn drop(&mut self) { if self.deleted_cnt > 0 { // SAFETY: Trailing unchecked items must be valid since we never touch them. @@ -1661,7 +1673,12 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } } - let mut g = BackshiftOnDrop::<T, A, COOP_PREFERRED> { v: self, processed_len: 0, deleted_cnt: 0, original_len }; + let mut g = BackshiftOnDrop::<T, A, COOP_PREFERRED> { + v: self, + processed_len: 0, + deleted_cnt: 0, + original_len, + }; fn process_loop<F, T, A: Allocator, const DELETED: bool, const VEC_IS_COOP: bool>( original_len: usize, @@ -1669,7 +1686,7 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR g: &mut BackshiftOnDrop<'_, T, A, VEC_IS_COOP>, ) where F: FnMut(&mut T) -> bool, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]: + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]:, { while g.processed_len != original_len { // SAFETY: Unchecked element must be valid. @@ -1763,7 +1780,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR /* INVARIANT: vec.len() > read >= write > write-1 >= 0 */ struct FillGapOnDrop<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { /* Offset of the element we want to check if it is duplicate */ read: usize, @@ -1775,8 +1794,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR vec: &'a mut Vec<T, A, COOP_PREFERRED>, } - impl<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> Drop for FillGapOnDrop<'a, T, A, COOP_PREFERRED> - where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + impl<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> Drop + for FillGapOnDrop<'a, T, A, COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { fn drop(&mut self) { /* This code gets executed when `same_bucket` panics */ @@ -2370,7 +2392,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. /// /// If `new_len` is greater than `len`, the `Vec` is extended by the @@ -2470,7 +2494,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> Vec<[T; N], A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`. /// /// # Panics @@ -2532,7 +2558,9 @@ impl<T: Clone> ExtendWith<T> for ExtendElement<T> { } impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[cfg(not(no_global_oom_handling))] /// Extend the vector by `n` values, using the given generator. fn extend_with<E: ExtendWith<T>>(&mut self, n: usize, mut value: E) { @@ -2565,7 +2593,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Removes consecutive repeated elements in the vector according to the /// [`PartialEq`] trait implementation. /// @@ -2601,8 +2631,14 @@ pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> { #[doc(hidden)] #[cfg(not(no_global_oom_handling))] #[unstable(feature = "allocator_api", issue = "32838")] -pub fn from_elem_in<T: Clone, A: Allocator, const COOP_PREFERRED: bool>(elem: T, n: usize, alloc: A) -> Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +pub fn from_elem_in<T: Clone, A: Allocator, const COOP_PREFERRED: bool>( + elem: T, + n: usize, + alloc: A, +) -> Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ <T as SpecFromElem>::from_elem(elem, n, alloc) } @@ -2614,8 +2650,11 @@ trait ExtendFromWithinSpec { unsafe fn spec_extend_from_within(&mut self, src: Range<usize>); } -impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ default unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { // SAFETY: // - len is increased only after initializing elements @@ -2634,8 +2673,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } } -impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { let count = src.len(); { @@ -2669,7 +2711,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::Deref for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Target = [T]; #[inline] @@ -2680,7 +2724,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::DerefMut for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn deref_mut(&mut self) -> &mut [T] { unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } @@ -2694,7 +2740,9 @@ trait SpecCloneFrom { #[cfg(not(no_global_oom_handling))] impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ default fn clone_from(this: &mut Self, other: &Self) { // drop anything that will not be overwritten this.truncate(other.len()); @@ -2711,7 +2759,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[cfg(not(no_global_oom_handling))] impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn clone_from(this: &mut Self, other: &Self) { this.clear(); this.extend_from_slice(other); @@ -2721,7 +2771,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[cfg(not(test))] fn clone(&self) -> Self { let alloc = self.allocator().clone(); @@ -2757,7 +2809,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR /// ``` #[stable(feature = "rust1", since = "1.0.0")] impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn hash<H: Hasher>(&self, state: &mut H) { Hash::hash(&**self, state) @@ -2769,8 +2823,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR message = "vector indices are of type `usize` or ranges of `usize`", label = "vector indices are of type `usize` or ranges of `usize`" )] -impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> Index<I> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> Index<I> + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Output = I::Output; #[inline] @@ -2784,8 +2841,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR message = "vector indices are of type `usize` or ranges of `usize`", label = "vector indices are of type `usize` or ranges of `usize`" )] -impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> IndexMut<I> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> IndexMut<I> + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { IndexMut::index_mut(&mut **self, index) @@ -2803,7 +2863,9 @@ impl<T> FromIterator<T> for Vec<T> { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = T; type IntoIter = IntoIter<T, A>; @@ -2849,7 +2911,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = &'a T; type IntoIter = slice::Iter<'a, T>; @@ -2859,8 +2923,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator + for &'a mut Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; @@ -2872,7 +2939,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()) @@ -2890,7 +2959,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ // leaf method to which various SpecFrom/SpecExtend implementations delegate when // they have no further optimizations to apply #[cfg(not(no_global_oom_handling))] @@ -3064,8 +3135,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR /// [`copy_from_slice`]: slice::copy_from_slice #[cfg(not(no_global_oom_handling))] #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a, T: Copy + 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> Extend<&'a T> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<'a, T: Copy + 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> Extend<&'a T> + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()) } @@ -3083,8 +3157,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR /// Implements comparison of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison). #[stable(feature = "rust1", since = "1.0.0")] -impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { PartialOrd::partial_cmp(&**self, &**other) @@ -3092,13 +3169,17 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {} +impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for Vec<T, A, COOP_PREFERRED> where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: +{ +} /// Implements ordering of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison). #[stable(feature = "rust1", since = "1.0.0")] impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(&**self, &**other) @@ -3106,8 +3187,11 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR } #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn drop(&mut self) { unsafe { // use drop for [T] @@ -3131,24 +3215,33 @@ impl<T> const Default for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A, COOP_PREFERRED>> + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn as_ref(&self) -> &Vec<T, A, COOP_PREFERRED> { self } } #[stable(feature = "vec_as_mut", since = "1.5.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A, COOP_PREFERRED>> + for Vec<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn as_mut(&mut self) -> &mut Vec<T, A, COOP_PREFERRED> { self } @@ -3156,7 +3249,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn as_ref(&self) -> &[T] { self } @@ -3164,7 +3259,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[stable(feature = "vec_as_mut", since = "1.5.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<[T]> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn as_mut(&mut self) -> &mut [T] { self } @@ -3262,7 +3359,9 @@ where #[cfg(not(test))] #[stable(feature = "vec_from_box", since = "1.18.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Box<[T], A>> for Vec<T, A, COOP_PREFERRED> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Convert a boxed slice into a vector by transferring ownership of /// the existing heap allocation. /// @@ -3282,7 +3381,9 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR #[cfg(not(test))] #[stable(feature = "box_from_vec", since = "1.20.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for Box<[T], A> -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ /// Convert a vector into a boxed slice. /// /// If `v` has excess capacity, its items will be moved into a @@ -3322,8 +3423,11 @@ impl From<&str> for Vec<u8> { } #[stable(feature = "array_try_from_vec", since = "1.48.0")] -impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, A, COOP_PREFERRED>> for [T; N] -where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, A, COOP_PREFERRED>> + for [T; N] +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ type Error = Vec<T, A, COOP_PREFERRED>; /// Gets the entire contents of the `Vec<T>` as an array, diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index 87bdb3aaab7a5..73cf325889290 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -1,5 +1,5 @@ -use core::alloc; use crate::alloc::Allocator; +use core::alloc; use core::iter::TrustedLen; use core::slice::{self}; @@ -13,7 +13,7 @@ pub(super) trait SpecExtend<T, I> { impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iter: I) { self.extend_desugared(iter) @@ -23,15 +23,18 @@ where impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.extend_trusted(iterator) } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, IntoIter<T>> for Vec<T, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, IntoIter<T>> + for Vec<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn spec_extend(&mut self, mut iterator: IntoIter<T>) { unsafe { self.append_elements(iterator.as_slice() as _); @@ -40,21 +43,23 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE } } -impl<'a, T: 'a, I, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, I> for Vec<T, A, COOP_PREFERRED> +impl<'a, T: 'a, I, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, I> + for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = &'a T>, T: Clone, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.cloned()) } } -impl<'a, T: 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A, COOP_PREFERRED> +impl<'a, T: 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, slice::Iter<'a, T>> + for Vec<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index 682375a607837..3766f6de061bb 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -1,20 +1,31 @@ use core::ptr; -use core::alloc; use crate::alloc::Allocator; use crate::raw_vec::RawVec; +use core::alloc; use super::{ExtendElement, IsZero, Vec}; // Specialization trait used for Vec::from_elem pub(super) trait SpecFromElem: Sized { - fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: Self, n: usize, alloc: A) -> Vec<Self, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots::<A>(COOP_PREFERRED)]: ; + fn from_elem<A: Allocator, const COOP_PREFERRED: bool>( + elem: Self, + n: usize, + alloc: A, + ) -> Vec<Self, A, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots::<A>(COOP_PREFERRED)]:; } impl<T: Clone> SpecFromElem for T { - default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: Self, n: usize, alloc: A) -> Vec<Self, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>( + elem: Self, + n: usize, + alloc: A, + ) -> Vec<Self, A, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { let mut v = Vec::with_capacity_in(n, alloc); v.extend_with(n, ExtendElement(elem)); v @@ -23,8 +34,14 @@ impl<T: Clone> SpecFromElem for T { impl<T: Clone + IsZero> SpecFromElem for T { #[inline] - default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: T, n: usize, alloc: A) -> Vec<T, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>( + elem: T, + n: usize, + alloc: A, + ) -> Vec<T, A, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { if elem.is_zero() { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } @@ -36,8 +53,14 @@ impl<T: Clone + IsZero> SpecFromElem for T { impl SpecFromElem for i8 { #[inline] - fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: i8, n: usize, alloc: A) -> Vec<i8, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + fn from_elem<A: Allocator, const COOP_PREFERRED: bool>( + elem: i8, + n: usize, + alloc: A, + ) -> Vec<i8, A, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } @@ -52,8 +75,14 @@ impl SpecFromElem for i8 { impl SpecFromElem for u8 { #[inline] - fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: u8, n: usize, alloc: A) -> Vec<u8, A, COOP_PREFERRED> - where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { + fn from_elem<A: Allocator, const COOP_PREFERRED: bool>( + elem: u8, + n: usize, + alloc: A, + ) -> Vec<u8, A, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index 631d9d5e3aa60..d8569a6e89c62 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -1,5 +1,5 @@ -use core::alloc; use crate::alloc::{Allocator, Global}; +use core::alloc; use core::ptr::{self}; use core::slice::{self}; @@ -23,15 +23,18 @@ pub struct Splice< 'a, I: Iterator + 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, -> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +> where + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, +{ pub(super) drain: Drain<'a, I::Item, A>, pub(super) replace_with: I, } #[stable(feature = "vec_splice", since = "1.21.0")] impl<I: Iterator, A: Allocator> Iterator for Splice<'_, I, A> -where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, +{ type Item = I::Item; fn next(&mut self) -> Option<Self::Item> { @@ -45,19 +48,25 @@ where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: { #[stable(feature = "vec_splice", since = "1.21.0")] impl<I: Iterator, A: Allocator> DoubleEndedIterator for Splice<'_, I, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ fn next_back(&mut self) -> Option<Self::Item> { self.drain.next_back() } } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {} +impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> where + [(); alloc::co_alloc_metadata_num_slots::<A>()]: +{ +} #[stable(feature = "vec_splice", since = "1.21.0")] impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ fn drop(&mut self) { self.drain.by_ref().for_each(drop); // At this point draining is done and the only remaining tasks are splicing @@ -105,7 +114,9 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { /// Private helper methods for `Splice::drop` impl<T, A: Allocator> Drain<'_, T, A> -where [(); alloc::co_alloc_metadata_num_slots::<A>()]: { +where + [(); alloc::co_alloc_metadata_num_slots::<A>()]:, +{ /// The range from `self.vec.len` to `self.tail_start` contains elements /// that have been moved out. /// Fill that range as much as possible with new elements from the `replace_with` iterator. diff --git a/library/alloc/tests/boxed.rs b/library/alloc/tests/boxed.rs index c34e7a558061d..656e079f9c61c 100644 --- a/library/alloc/tests/boxed.rs +++ b/library/alloc/tests/boxed.rs @@ -61,7 +61,7 @@ fn box_deref_lval() { pub struct ConstAllocator; -//@TODO +//@FIXME unsafe impl const Allocator for ConstAllocator { fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { match layout.size() { diff --git a/library/core/src/alloc/global.rs b/library/core/src/alloc/global.rs index b0d457ff8c4d9..a7251328c128b 100644 --- a/library/core/src/alloc/global.rs +++ b/library/core/src/alloc/global.rs @@ -1,7 +1,7 @@ +use crate::alloc::GlobalCoAllocMeta; use crate::alloc::Layout; use crate::cmp; use crate::ptr; -use crate::alloc::GlobalCoAllocMeta; #[unstable(feature = "global_co_alloc_meta", issue = "none")] #[allow(missing_debug_implementations)] @@ -166,7 +166,9 @@ pub unsafe trait GlobalAlloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8; #[unstable(feature = "global_co_alloc", issue = "none")] - unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) {panic!("@FIXME")} + unsafe fn co_alloc(&self, _layout: Layout, mut _result: &mut RawAndMeta) { + panic!("@FIXME") + } /// Deallocate the block of memory at the given `ptr` pointer with the given `layout`. /// @@ -184,7 +186,9 @@ pub unsafe trait GlobalAlloc { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout); #[unstable(feature = "global_co_alloc", issue = "none")] - unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) {panic!("@FIXME")} + unsafe fn co_dealloc(&self, _ptr_and_meta: RawAndMeta, _layout: Layout) { + panic!("@FIXME") + } /// Behaves like `alloc`, but also ensures that the contents /// are set to zero before being returned. @@ -308,7 +312,7 @@ pub unsafe trait GlobalAlloc { ptr_and_meta: RawAndMeta, layout: Layout, new_size: usize, - mut result: &mut RawAndMeta + mut result: &mut RawAndMeta, ) { // SAFETY: the caller must ensure that the `new_size` does not overflow. // `layout.align()` comes from a `Layout` and is thus guaranteed to be valid. diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index f67bdf35b9416..f8200a774839e 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -90,7 +90,9 @@ pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { #[unstable(feature = "global_co_alloc", issue = "none")] /// Param `coop_preferred` - if false, then this returns `0`, regardless of /// whether allocator `A` is cooperative. -pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>(coop_preferred: bool) -> usize { +pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>( + coop_preferred: bool, +) -> usize { if A::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } } @@ -183,7 +185,9 @@ pub unsafe trait Allocator { /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>; - fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) {panic!("FIXME")} + fn co_allocate(&self, _layout: Layout, _result: &mut SliceAndMetaResult) { + panic!("FIXME") + } /// Behaves like `allocate`, but also ensures that the returned memory is zero-initialized. /// @@ -209,14 +213,9 @@ pub unsafe trait Allocator { fn co_allocate_zeroed(&self, layout: Layout, mut result: &mut SliceAndMetaResult) { self.co_allocate(layout, &mut result); - if let Ok(SliceAndMeta{slice, ..}) = result { + if let Ok(SliceAndMeta { slice, .. }) = result { // SAFETY: `alloc` returns a valid memory block - unsafe { - slice - .as_non_null_ptr() - .as_ptr() - .write_bytes(0, slice.len()) - } + unsafe { slice.as_non_null_ptr().as_ptr().write_bytes(0, slice.len()) } } } @@ -231,7 +230,9 @@ pub unsafe trait Allocator { /// [*fit*]: #memory-fitting unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout); - unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) {panic!("FIXME")} + unsafe fn co_deallocate(&self, _ptr_and_meta: PtrAndMeta, _layout: Layout) { + panic!("FIXME") + } /// Attempts to extend the memory block. /// @@ -302,7 +303,7 @@ pub unsafe trait Allocator { ptr_and_meta: PtrAndMeta, old_layout: Layout, new_layout: Layout, - mut result: &mut SliceAndMetaResult + mut result: &mut SliceAndMetaResult, ) { debug_assert!( new_layout.size() >= old_layout.size(), @@ -311,7 +312,7 @@ pub unsafe trait Allocator { self.co_allocate(new_layout, &mut result); - if let Ok(SliceAndMeta {slice, ..}) = result { + if let Ok(SliceAndMeta { slice, .. }) = result { // SAFETY: because `new_layout.size()` must be greater than or equal to // `old_layout.size()`, both the old and new memory allocation are valid for reads and // writes for `old_layout.size()` bytes. Also, because the old allocation wasn't yet @@ -396,7 +397,7 @@ pub unsafe trait Allocator { ptr_and_meta: PtrAndMeta, old_layout: Layout, new_layout: Layout, - mut result: &mut SliceAndMetaResult + mut result: &mut SliceAndMetaResult, ) { debug_assert!( new_layout.size() >= old_layout.size(), @@ -405,7 +406,7 @@ pub unsafe trait Allocator { self.co_allocate_zeroed(new_layout, &mut result); - if let Ok(SliceAndMeta{ slice, ..}) = result { + if let Ok(SliceAndMeta { slice, .. }) = result { // SAFETY: because `new_layout.size()` must be greater than or equal to // `old_layout.size()`, both the old and new memory allocation are valid for reads and // writes for `old_layout.size()` bytes. Also, because the old allocation wasn't yet @@ -491,7 +492,7 @@ pub unsafe trait Allocator { ptr_and_meta: PtrAndMeta, old_layout: Layout, new_layout: Layout, - mut result: &mut SliceAndMetaResult + mut result: &mut SliceAndMetaResult, ) { debug_assert!( new_layout.size() <= old_layout.size(), @@ -500,7 +501,7 @@ pub unsafe trait Allocator { self.co_allocate(new_layout, &mut result); - if let Ok(SliceAndMeta{ slice, ..}) = result { + if let Ok(SliceAndMeta { slice, .. }) = result { // SAFETY: because `new_layout.size()` must be lower than or equal to // `old_layout.size()`, both the old and new memory allocation are valid for reads and // writes for `new_layout.size()` bytes. Also, because the old allocation wasn't yet diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index 33d1ee065de2a..63045ebcb8157 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -403,7 +403,9 @@ fn reserve_and_pad<A: Allocator, const COOP_PREFERRED: bool>( vec: &mut Vec<u8, A, COOP_PREFERRED>, buf_len: usize, ) -> io::Result<usize> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ let pos: usize = (*pos_mut).try_into().map_err(|_| { io::const_io_error!( ErrorKind::InvalidInput, @@ -442,10 +444,14 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// Writes the slice to the vec without allocating /// # Safety: vec must have buf.len() spare capacity -unsafe fn vec_write_unchecked<A, const COOP_PREFERRED: bool>(pos: usize, vec: &mut Vec<u8, A, COOP_PREFERRED>, buf: &[u8]) -> usize +unsafe fn vec_write_unchecked<A, const COOP_PREFERRED: bool>( + pos: usize, + vec: &mut Vec<u8, A, COOP_PREFERRED>, + buf: &[u8], +) -> usize where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { debug_assert!(vec.capacity() >= pos + buf.len()); vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()); @@ -461,10 +467,14 @@ where /// This also allows for the vec body to be empty, but with a position of N. /// This means that [`Write`] will pad the vec with 0 initially, /// before writing anything from that point -fn vec_write<A, const COOP_PREFERRED: bool>(pos_mut: &mut u64, vec: &mut Vec<u8, A, COOP_PREFERRED>, buf: &[u8]) -> io::Result<usize> +fn vec_write<A, const COOP_PREFERRED: bool>( + pos_mut: &mut u64, + vec: &mut Vec<u8, A, COOP_PREFERRED>, + buf: &[u8], +) -> io::Result<usize> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { let buf_len = buf.len(); let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?; @@ -500,7 +510,7 @@ fn vec_write_vectored<A, const COOP_PREFERRED: bool>( ) -> io::Result<usize> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // For safety reasons, we don't want this sum to overflow ever. // If this saturates, the reserve should panic to avoid any unsound writing. @@ -551,7 +561,7 @@ impl Write for Cursor<&mut [u8]> { impl<A, const COOP_PREFERRED: bool> Write for Cursor<&mut Vec<u8, A, COOP_PREFERRED>> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, self.inner, buf) @@ -576,7 +586,7 @@ where impl<A, const COOP_PREFERRED: bool> Write for Cursor<Vec<u8, A, COOP_PREFERRED>> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, &mut self.inner, buf) diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index ec3394e0b16c1..8671ebf5c8141 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -379,7 +379,9 @@ impl Write for &mut [u8] { /// The vector will grow as needed. #[stable(feature = "rust1", since = "1.0.0")] impl<A: Allocator, const COOP_PREFERRED: bool> Write for Vec<u8, A, COOP_PREFERRED> -where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ #[inline] fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.extend_from_slice(buf); @@ -416,7 +418,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE /// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`. #[stable(feature = "vecdeque_read_write", since = "1.63.0")] impl<A: Allocator, const _COOP_PREFERRED: bool> Read for VecDeque<u8, A, _COOP_PREFERRED> -where [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]: { +where + [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]:, +{ /// Fill `buf` with the contents of the "front" slice as returned by /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are /// discontiguous, multiple calls to `read` will be needed to read the entire content. @@ -441,7 +445,9 @@ where [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]: { /// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed. #[stable(feature = "vecdeque_read_write", since = "1.63.0")] impl<A: Allocator, const _COOP_PREFERRED: bool> Write for VecDeque<u8, A, _COOP_PREFERRED> -where [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]: { +where + [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]:, +{ #[inline] fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.extend(buf); diff --git a/library/std/src/sys/unix/thread_local_dtor.rs b/library/std/src/sys/unix/thread_local_dtor.rs index c09b0f7832b6a..4549f5d77742c 100644 --- a/library/std/src/sys/unix/thread_local_dtor.rs +++ b/library/std/src/sys/unix/thread_local_dtor.rs @@ -2,11 +2,11 @@ #![unstable(feature = "thread_local_internals", issue = "none")] #![feature(global_co_alloc_plvec)] -use core::alloc::PlVec; - //! Provides thread-local destructors without an associated "key", which //! can be more efficient. +use core::alloc::PlVec; + // Since what appears to be glibc 2.18 this symbol has been shipped which // GCC and clang both use to invoke destructors in thread_local globals, so // let's do the same! diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 8e2d67eb173d1..6e974fc701e87 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -77,7 +77,10 @@ pub(crate) struct Context<'tcx> { // `Context` is cloned a lot, so we don't want the size to grow unexpectedly. #[cfg(all(not(windows), target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Context<'_>, 160 + 2 * mem::size_of::<GlobalCoAllocMeta>()); +rustc_data_structures::static_assert_size!( + Context<'_>, + 160 + 2 * mem::size_of::<GlobalCoAllocMeta>() +); /// Shared mutable state used in [`Context`] and elsewhere. pub(crate) struct SharedContext<'tcx> { From 219edc7edb0ada758e867076e8d7afe9c61bdb46 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 12 Dec 2022 21:42:54 -0800 Subject: [PATCH 10/47] Vec/VecDeque to use COOP_PREFERRED. WIP, NOT COMPILING. But: rustc doesn't die anymore. --- library/alloc/src/collections/vec_deque/mod.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index b441f23ed23f3..7be36d0e1132c 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -158,7 +158,7 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, const COOP_PREFERRED: bool = true> Default for VecDeque<T, Global, COOP_PREFERRED> { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Default for VecDeque<T, Global, COOP_PREFERRED> { /// Creates an empty deque. #[inline] fn default() -> VecDeque<T, Global, COOP_PREFERRED> { @@ -547,7 +547,7 @@ where } } -impl<T, Global, const COOP_PREFERRED: bool> VecDeque<T, Global, COOP_PREFERRED> +impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { @@ -581,7 +581,7 @@ where #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] - pub fn with_capacity(capacity: usize) -> VecDeque<T, Global, COOP_PREFERRED> { + pub fn with_capacity(capacity: usize) -> VecDeque<T, A, COOP_PREFERRED> { Self::with_capacity_in(capacity, Global) } } @@ -2791,7 +2791,7 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, GlobalAlloc, COOP_PREFERRED> +impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { @@ -2976,8 +2976,8 @@ where } #[stable(feature = "std_collections_from_array", since = "1.56.0")] -impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]> - for VecDeque<T, Global, COOP_PREFERRED> +impl<T, const N: usize, A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> + From<[T; N]> for VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { @@ -2996,6 +2996,7 @@ where if !<T>::IS_ZST { // SAFETY: VecDeque::with_capacity ensures that there is enough capacity. unsafe { + // @FIXME for COOP_PREFERRED: ptr::copy_nonoverlapping(arr.as_ptr(), deq.ptr(), N); } } From e61c02ff68149efc8d563d56ea7bb889115fe08a Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 12 Dec 2022 21:54:59 -0800 Subject: [PATCH 11/47] (Some) compile errors fixed; but: nightly panics again! --- library/alloc/src/collections/vec_deque/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 7be36d0e1132c..422d9287ab4a7 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -8,7 +8,7 @@ #![feature(global_co_alloc)] #![stable(feature = "rust1", since = "1.0.0")] use crate::vec::DEFAULT_COOP_PREFERRED; -use core::alloc::{self, GlobalAlloc}; +use core::alloc; use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; @@ -158,10 +158,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> Default for VecDeque<T, Global, COOP_PREFERRED> { +impl<T, A: Allocator, const COOP_PREFERRED: bool> Default for VecDeque<T, A, COOP_PREFERRED> { /// Creates an empty deque. #[inline] - fn default() -> VecDeque<T, Global, COOP_PREFERRED> { + fn default() -> VecDeque<T, A, COOP_PREFERRED> { VecDeque::new() } } @@ -2976,8 +2976,8 @@ where } #[stable(feature = "std_collections_from_array", since = "1.56.0")] -impl<T, const N: usize, A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> - From<[T; N]> for VecDeque<T, A, COOP_PREFERRED> +impl<T, const N: usize, A: Allocator, const COOP_PREFERRED: bool> From<[T; N]> + for VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { From 313616fb0e93661fe50f8e1724d1509860eddfd8 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Tue, 13 Dec 2022 11:35:25 -0800 Subject: [PATCH 12/47] Docs and FIXME --- library/core/src/alloc/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index f8200a774839e..491bc45a19128 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -27,6 +27,12 @@ use crate::fmt; use crate::ptr::{self, NonNull}; // @FIXME Make this target-specific +/// Metadata for `Vec/VecDeque/RawVec` to assist the allocator. Make sure its +/// alignment is not bigger than alignment of `usize`. Otherwise, even if (a +/// particular) `Vec/VecDeque/RawVec` generic instance doesn't use cooperation, +/// it would increase size of that `Vec/VecDeque/RawVec` because of alignment +/// rules! @FIXME compile time test that `GlobalCoAllocMeta` alignment <= +/// `usize` alignment. #[unstable(feature = "global_co_alloc_meta", issue = "none")] #[allow(missing_debug_implementations)] #[derive(Clone, Copy)] @@ -84,6 +90,9 @@ pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>; #[unstable(feature = "global_co_alloc", issue = "none")] pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { + if true { + panic!("FIXME - consider removing co_alloc_metadata_num_slots()"); + } if A::IS_CO_ALLOCATOR { 1 } else { 0 } } From be8f77664f071691a9e3f622a2ac4f103f2a1388 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 14 Dec 2022 13:27:35 -0800 Subject: [PATCH 13/47] Vec to use COOP_PREFERRED --- library/alloc/src/boxed.rs | 10 +++++++--- .../alloc/src/collections/binary_heap/mod.rs | 4 ++-- library/alloc/src/raw_vec.rs | 16 +--------------- library/alloc/src/rc.rs | 12 ++++++++++-- library/alloc/src/slice.rs | 19 ++++++++++++++----- library/alloc/src/str.rs | 8 +++++++- library/alloc/src/vec/mod.rs | 8 ++++---- library/core/src/alloc/mod.rs | 13 +++++++++++++ .../std/src/sys_common/thread_local_dtor.rs | 4 +++- 9 files changed, 61 insertions(+), 33 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 021f36e6770bd..65baed0056619 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -1683,8 +1683,12 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_array_try_from_vec", since = "1.66.0")] -impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> { - type Error = Vec<T>; +impl<T, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, Global, COOP_PREFERRED>> + for Box<[T; N]> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + type Error = Vec<T, Global, COOP_PREFERRED>; /// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`. /// @@ -1704,7 +1708,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> { /// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap(); /// assert_eq!(state.len(), 100); /// ``` - fn try_from(vec: Vec<T>) -> Result<Self, Self::Error> { + fn try_from(vec: Vec<T, Global, COOP_PREFERRED>) -> Result<Self, Self::Error> { if vec.len() == N { let boxed_slice = vec.into_boxed_slice(); Ok(unsafe { boxed_slice_as_array_unchecked(boxed_slice) }) diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index 3d10072ce43b4..8409954c5dbc8 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -1525,7 +1525,7 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {} #[derive(Debug)] pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { iter: vec::Drain<'a, T, Global, COOP_PREFERRED>, } @@ -1533,7 +1533,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Item = T; diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 84fd954edbd7a..e195285511430 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -69,7 +69,7 @@ pub(crate) struct RawVec< impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so /// they cannot call `Self::new()`. @@ -527,20 +527,6 @@ where } } -// @FIXME Custom -unsafe impl<#[may_dangle] T, const COOP_PREFERRED: bool> Drop for RawVec<T, Global, COOP_PREFERRED> -where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, -{ - /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. - fn drop(&mut self) { - // @TOFIXMEDO - if let Some((ptr, layout)) = self.current_memory() { - unsafe { self.alloc.deallocate(ptr, layout) } - } - } -} - // Central function for reserve error handling. #[cfg(not(no_global_oom_handling))] #[inline] diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index c9aa23fc4af1f..b1cffc74f283c 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -247,6 +247,7 @@ use crate::boxed::Box; #[cfg(test)] use std::boxed::Box; +use crate::alloc; use core::any::Any; use core::borrow; use core::cell::Cell; @@ -1987,7 +1988,10 @@ impl<T: ?Sized> From<Box<T>> for Rc<T> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "shared_from_slice", since = "1.21.0")] -impl<T> From<Vec<T>> for Rc<[T]> { +impl<T, const COOP_PREFERRED: bool> From<Vec<T, Global, COOP_PREFERRED>> for Rc<[T]> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Allocate a reference-counted slice and move `v`'s items into it. /// /// # Example @@ -1999,7 +2003,10 @@ impl<T> From<Vec<T>> for Rc<[T]> { /// assert_eq!(vec![1, 2, 3], *shared); /// ``` #[inline] - fn from(mut v: Vec<T>) -> Rc<[T]> { + fn from(mut v: Vec<T, Global, COOP_PREFERRED>) -> Rc<[T]> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + { unsafe { let rc = Rc::copy_from_slice(&v); // Allow the Vec to free its memory, but not destroy its contents @@ -2120,6 +2127,7 @@ trait ToRcSlice<T>: Iterator<Item = T> + Sized { fn to_rc_slice(self) -> Rc<[T]>; } +// COOP_NOT_POSSIBLE #[cfg(not(no_global_oom_handling))] impl<T, I: Iterator<Item = T>> ToRcSlice<T> for I { default fn to_rc_slice(self) -> Rc<[T]> { diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 5e44d3c8e0646..696bb1454b22e 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -464,10 +464,13 @@ impl<T> [T] { #[rustc_allow_incoherent_impl] #[inline] #[unstable(feature = "allocator_api", issue = "32838")] - pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A> + pub fn to_vec_in<A: Allocator, const COOP_PREFERRED: bool>( + &self, + alloc: A, + ) -> Vec<T, A, COOP_PREFERRED> where T: Clone, - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // N.B., see the `hack` module in this file for more details. hack::to_vec(self, alloc) @@ -490,9 +493,11 @@ impl<T> [T] { #[rustc_allow_incoherent_impl] #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> + pub fn into_vec<A: Allocator, const COOP_PREFERRED: bool>( + self: Box<Self, A>, + ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // N.B., see the `hack` module in this file for more details. hack::into_vec(self) @@ -737,6 +742,7 @@ pub trait Join<Separator> { fn join(slice: &Self, sep: Separator) -> Self::Output; } +// COOP_NOT_POSSIBLE #[cfg(not(no_global_oom_handling))] #[unstable(feature = "slice_concat_ext", issue = "27747")] impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] { @@ -752,6 +758,7 @@ impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] { } } +// COOP_NOT_POSSIBLE #[cfg(not(no_global_oom_handling))] #[unstable(feature = "slice_concat_ext", issue = "27747")] impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] { @@ -775,10 +782,11 @@ impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] { } } +// COOP_NOT_POSSIBLE #[cfg(not(no_global_oom_handling))] #[unstable(feature = "slice_concat_ext", issue = "27747")] impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] { - type Output = Vec<T>; + type Output = Vec<T, Global>; fn join(slice: &Self, sep: &[T]) -> Vec<T> { let mut iter = slice.iter(); @@ -823,6 +831,7 @@ where } } +// COOP_NOT_POSSIBLE #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] impl<T: Clone> ToOwned for [T] { diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index afbe5cfaf8ef9..eedf856f5d253 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -14,6 +14,8 @@ use core::ptr; use core::str::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher}; use core::unicode::conversions; +use crate::alloc; +use crate::alloc::Global; use crate::borrow::ToOwned; use crate::boxed::Box; use crate::slice::{Concat, Join, SliceIndex}; @@ -126,11 +128,15 @@ macro_rules! copy_slice_and_advance { // [T] and str both impl AsRef<[T]> for some T // => s.borrow().as_ref() and we always have slices #[cfg(not(no_global_oom_handling))] -fn join_generic_copy<B, T, S>(slice: &[S], sep: &[T]) -> Vec<T> +fn join_generic_copy<B, T, S, const COOP_PREFERRED: bool>( + slice: &[S], + sep: &[T], +) -> Vec<T, Global, COOP_PREFERRED> where T: Copy, B: AsRef<[T]> + ?Sized, S: Borrow<B>, + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { let sep_len = sep.len(); let mut iter = slice.iter(); diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index c53e3d3ad483f..66d1baafa5538 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -408,6 +408,10 @@ pub struct Vec< len: usize, } +/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter. +#[unstable(feature = "global_co_alloc_def", issue = "none")] +pub const DEFAULT_COOP_PREFERRED: bool = true; + #[unstable(feature = "global_co_alloc_covec", issue = "none")] pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, true>; @@ -417,10 +421,6 @@ pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: All pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, false>; -/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter. -#[unstable(feature = "global_co_alloc_def", issue = "none")] -pub const DEFAULT_COOP_PREFERRED: bool = true; - /// "Default" Vec. Either "cooperative" or not - as specified by `DEFAULT_COOP_PREFERRED`. The /// difference to `Vec` (used without specifying `COOP_PREFERRED`): `DefVec` indicates that the /// author considered using `CoVec` or `PlVec`, but left it to default instead. diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 491bc45a19128..807c61effb819 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -105,6 +105,19 @@ pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>( if A::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } } +#[unstable(feature = "global_co_alloc", issue = "none")] +/// Param `coop_preferred` - if false, then this returns `0`, regardless of +/// whether allocator `A` is cooperative. +pub const fn co_alloc_metadata_num_slots_with_preference_global(_coop_preferred: bool) -> usize { + if true { + panic!("FIXME"); + } + // @FIXME Move these functions to :alloc instead. + // Then: + 0 + //if Global::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } +} + /// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of /// data described via [`Layout`][]. /// diff --git a/library/std/src/sys_common/thread_local_dtor.rs b/library/std/src/sys_common/thread_local_dtor.rs index 1d13a7171b035..0914dc2639cc3 100644 --- a/library/std/src/sys_common/thread_local_dtor.rs +++ b/library/std/src/sys_common/thread_local_dtor.rs @@ -12,9 +12,11 @@ #![unstable(feature = "thread_local_internals", issue = "none")] #![allow(dead_code)] +#![feature(global_co_alloc_plvec)] use crate::ptr; use crate::sys_common::thread_local_key::StaticKey; +use alloc::vec::PlVec; pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { // 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 // flagged for destruction. static DTORS: StaticKey = StaticKey::new(Some(run_dtors)); - type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>; + type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>; if DTORS.get().is_null() { let v: Box<List> = box Vec::new(); DTORS.set(Box::into_raw(v) as *mut u8); From 482762431f434a810c1b7020903424794df16671 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 14 Dec 2022 13:27:48 -0800 Subject: [PATCH 14/47] String uses COOP_PREFERRED --- library/alloc/src/string.rs | 457 +++++++++++++++++++++++++++--------- 1 file changed, 345 insertions(+), 112 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 3118c7189a5ed..bbb320195839d 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -41,7 +41,12 @@ //! ``` #![stable(feature = "rust1", since = "1.0.0")] +#![feature(global_co_alloc_plvec)] +use crate::alloc::Global; + +use crate::alloc; +use crate::vec::PlVec; #[cfg(not(no_global_oom_handling))] use core::char::{decode_utf16, REPLACEMENT_CHARACTER}; use core::error::Error; @@ -71,6 +76,7 @@ use crate::str::{self, from_utf8_unchecked_mut, Chars, Utf8Error}; #[cfg(not(no_global_oom_handling))] use crate::str::{from_boxed_utf8_unchecked, FromStr}; use crate::vec::Vec; +use crate::vec::DEFAULT_COOP_PREFERRED; /// A UTF-8–encoded, growable string. /// @@ -364,8 +370,11 @@ use crate::vec::Vec; #[derive(PartialOrd, Eq, Ord)] #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), lang = "String")] -pub struct String { - vec: Vec<u8>, +pub struct String<const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + vec: Vec<u8, Global, COOP_PREFERRED>, } /// A possible error value when converting a `String` from a UTF-8 byte vector. @@ -405,7 +414,7 @@ pub struct String { #[cfg_attr(not(no_global_oom_handling), derive(Clone))] #[derive(Debug, PartialEq, Eq)] pub struct FromUtf8Error { - bytes: Vec<u8>, + bytes: PlVec<u8>, error: Utf8Error, } @@ -429,7 +438,10 @@ pub struct FromUtf8Error { #[derive(Debug)] pub struct FromUtf16Error(()); -impl String { +impl<const COOP_PREFERRED: bool> String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Creates a new empty `String`. /// /// Given that the `String` is empty, this will not allocate any initial @@ -452,7 +464,7 @@ impl String { #[rustc_const_stable(feature = "const_string_new", since = "1.39.0")] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] - pub const fn new() -> String { + pub const fn new() -> String<COOP_PREFERRED> { String { vec: Vec::new() } } @@ -497,7 +509,7 @@ impl String { #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] - pub fn with_capacity(capacity: usize) -> String { + pub fn with_capacity(capacity: usize) -> String<COOP_PREFERRED> { String { vec: Vec::with_capacity(capacity) } } @@ -507,7 +519,10 @@ impl String { // NB see the slice::hack module in slice.rs for more information #[inline] #[cfg(test)] - pub fn from_str(_: &str) -> String { + pub fn from_str(_: &str) -> String<COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + { panic!("not available with cfg(test)"); } @@ -569,7 +584,7 @@ impl String { /// [`into_bytes`]: String::into_bytes #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> { + pub fn from_utf8(vec: Vec<u8, Global, COOP_PREFERRED>) -> Result<String, FromUtf8Error> { match str::from_utf8(&vec) { Ok(..) => Ok(String { vec }), Err(e) => Err(FromUtf8Error { bytes: vec, error: e }), @@ -679,7 +694,7 @@ impl String { /// ``` #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] - pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> { + pub fn from_utf16(v: &[u16]) -> Result<String<COOP_PREFERRED>, FromUtf16Error> { // This isn't done via collect::<Result<_, _>>() for performance reasons. // FIXME: the function can be simplified again when #48994 is closed. let mut ret = String::with_capacity(v.len()); @@ -721,7 +736,7 @@ impl String { #[must_use] #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn from_utf16_lossy(v: &[u16]) -> String { + pub fn from_utf16_lossy(v: &[u16]) -> String<COOP_PREFERRED> { decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect() } @@ -807,7 +822,11 @@ impl String { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String { + pub unsafe fn from_raw_parts( + buf: *mut u8, + length: usize, + capacity: usize, + ) -> String<COOP_PREFERRED> { unsafe { String { vec: Vec::from_raw_parts(buf, length, capacity) } } } @@ -842,7 +861,9 @@ impl String { #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String { + pub unsafe fn from_utf8_unchecked( + bytes: Vec<u8, Global, COOP_PREFERRED>, + ) -> String<COOP_PREFERRED> { String { vec: bytes } } @@ -863,7 +884,10 @@ impl String { #[inline] #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "rust1", since = "1.0.0")] - pub fn into_bytes(self) -> Vec<u8> { + pub fn into_bytes(self) -> Vec<u8, Global, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + { self.vec } @@ -1610,7 +1634,10 @@ impl String { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> { + pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, Global, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + { &mut self.vec } @@ -1684,7 +1711,10 @@ impl String { #[inline] #[stable(feature = "string_split_off", since = "1.16.0")] #[must_use = "use `.truncate()` if you don't need the other half"] - pub fn split_off(&mut self, at: usize) -> String { + pub fn split_off(&mut self, at: usize) -> String<COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + { assert!(self.is_char_boundary(at)); let other = self.vec.split_off(at); unsafe { String::from_utf8_unchecked(other) } @@ -1921,7 +1951,10 @@ impl FromUtf8Error { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "rust1", since = "1.0.0")] - pub fn into_bytes(self) -> Vec<u8> { + pub fn into_bytes<const COOP_PREFERRED: bool>(self) -> Vec<u8, Global, COOP_PREFERRED> + where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + { self.bytes } @@ -1987,7 +2020,10 @@ impl Error for FromUtf16Error { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl Clone for String { +impl<const COOP_PREFERRED: bool> Clone for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn clone(&self) -> Self { String { vec: self.vec.clone() } } @@ -1999,8 +2035,11 @@ impl Clone for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl FromIterator<char> for String { - fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String { +impl<const COOP_PREFERRED: bool> FromIterator<char> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); buf.extend(iter); buf @@ -2009,8 +2048,11 @@ impl FromIterator<char> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "string_from_iter_by_ref", since = "1.17.0")] -impl<'a> FromIterator<&'a char> for String { - fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String { +impl<'a, const COOP_PREFERRED: bool> FromIterator<&'a char> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); buf.extend(iter); buf @@ -2019,8 +2061,11 @@ impl<'a> FromIterator<&'a char> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> FromIterator<&'a str> for String { - fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String { +impl<'a, const COOP_PREFERRED: bool> FromIterator<&'a str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); buf.extend(iter); buf @@ -2029,8 +2074,13 @@ impl<'a> FromIterator<&'a str> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "extend_string", since = "1.4.0")] -impl FromIterator<String> for String { - fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String { +impl<const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>( + iter: I, + ) -> String<COOP_PREFERRED> { let mut iterator = iter.into_iter(); // Because we're iterating over `String`s, we can avoid at least @@ -2048,8 +2098,11 @@ impl FromIterator<String> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_str2", since = "1.45.0")] -impl FromIterator<Box<str>> for String { - fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String { +impl<const COOP_PREFERRED: bool> FromIterator<Box<str>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); buf.extend(iter); buf @@ -2058,8 +2111,11 @@ impl FromIterator<Box<str>> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "herd_cows", since = "1.19.0")] -impl<'a> FromIterator<Cow<'a, str>> for String { - fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String { +impl<'a, const COOP_PREFERRED: bool> FromIterator<Cow<'a, str>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String<COOP_PREFERRED> { let mut iterator = iter.into_iter(); // Because we're iterating over CoWs, we can (potentially) avoid at least @@ -2078,7 +2134,10 @@ impl<'a> FromIterator<Cow<'a, str>> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl Extend<char> for String { +impl<const COOP_PREFERRED: bool> Extend<char> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) { let iterator = iter.into_iter(); let (lower_bound, _) = iterator.size_hint(); @@ -2099,7 +2158,10 @@ impl Extend<char> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a> Extend<&'a char> for String { +impl<'a, const COOP_PREFERRED: bool> Extend<&'a char> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) { self.extend(iter.into_iter().cloned()); } @@ -2117,7 +2179,10 @@ impl<'a> Extend<&'a char> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Extend<&'a str> for String { +impl<'a, const COOP_PREFERRED: bool> Extend<&'a str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(s)); } @@ -2138,7 +2203,10 @@ impl Extend<Box<str>> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "extend_string", since = "1.4.0")] -impl Extend<String> for String { +impl<const COOP_PREFERRED: bool> Extend<String> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); } @@ -2151,7 +2219,10 @@ impl Extend<String> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "herd_cows", since = "1.19.0")] -impl<'a> Extend<Cow<'a, str>> for String { +impl<'a, const COOP_PREFERRED: bool> Extend<Cow<'a, str>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); } @@ -2174,7 +2245,10 @@ impl<'a> Extend<Cow<'a, str>> for String { reason = "API not fully fleshed out and ready to be stabilized", issue = "27721" )] -impl<'a, 'b> Pattern<'a> for &'b String { +impl<'a, 'b, const COOP_PREFERRED: bool> Pattern<'a> for &'b String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Searcher = <&'b str as Pattern<'a>>::Searcher; fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher { @@ -2208,13 +2282,16 @@ impl<'a, 'b> Pattern<'a> for &'b String { } #[stable(feature = "rust1", since = "1.0.0")] -impl PartialEq for String { +impl<const COOP_PREFERRED: bool> PartialEq for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] - fn eq(&self, other: &String) -> bool { + fn eq(&self, other: &String<COOP_PREFERRED>) -> bool { PartialEq::eq(&self[..], &other[..]) } #[inline] - fn ne(&self, other: &String) -> bool { + fn ne(&self, other: &String<COOP_PREFERRED>) -> bool { PartialEq::ne(&self[..], &other[..]) } } @@ -2249,6 +2326,7 @@ macro_rules! impl_eq { }; } +// @FIXME for COOP_PREFERRED impl_eq! { String, str } impl_eq! { String, &'a str } #[cfg(not(no_global_oom_handling))] @@ -2260,16 +2338,22 @@ impl_eq! { Cow<'a, str>, String } #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] -impl const Default for String { +impl<const COOP_PREFERRED: bool> const Default for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Creates an empty `String`. #[inline] - fn default() -> String { + fn default() -> String<COOP_PREFERRED> { String::new() } } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Display for String { +impl<const COOP_PREFERRED: bool> fmt::Display for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&**self, f) @@ -2277,7 +2361,10 @@ impl fmt::Display for String { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Debug for String { +impl<const COOP_PREFERRED: bool> fmt::Debug for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) @@ -2285,7 +2372,10 @@ impl fmt::Debug for String { } #[stable(feature = "rust1", since = "1.0.0")] -impl hash::Hash for String { +impl<const COOP_PREFERRED: bool> hash::Hash for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn hash<H: hash::Hasher>(&self, hasher: &mut H) { (**self).hash(hasher) @@ -2331,11 +2421,14 @@ impl hash::Hash for String { /// ``` #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl Add<&str> for String { - type Output = String; +impl<const COOP_PREFERRED: bool> Add<&str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + type Output = String<COOP_PREFERRED>; #[inline] - fn add(mut self, other: &str) -> String { + fn add(mut self, other: &str) -> String<COOP_PREFERRED> { self.push_str(other); self } @@ -2346,7 +2439,10 @@ impl Add<&str> for String { /// This has the same behavior as the [`push_str`][String::push_str] method. #[cfg(not(no_global_oom_handling))] #[stable(feature = "stringaddassign", since = "1.12.0")] -impl AddAssign<&str> for String { +impl<const COOP_PREFERRED: bool> AddAssign<&str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn add_assign(&mut self, other: &str) { self.push_str(other); @@ -2354,7 +2450,10 @@ impl AddAssign<&str> for String { } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Index<ops::Range<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::Index<ops::Range<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Output = str; #[inline] @@ -2363,7 +2462,10 @@ impl ops::Index<ops::Range<usize>> for String { } } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Index<ops::RangeTo<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeTo<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Output = str; #[inline] @@ -2372,7 +2474,10 @@ impl ops::Index<ops::RangeTo<usize>> for String { } } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Index<ops::RangeFrom<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeFrom<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Output = str; #[inline] @@ -2381,7 +2486,10 @@ impl ops::Index<ops::RangeFrom<usize>> for String { } } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Index<ops::RangeFull> for String { +impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeFull> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Output = str; #[inline] @@ -2390,7 +2498,10 @@ impl ops::Index<ops::RangeFull> for String { } } #[stable(feature = "inclusive_range", since = "1.26.0")] -impl ops::Index<ops::RangeInclusive<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeInclusive<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Output = str; #[inline] @@ -2399,7 +2510,10 @@ impl ops::Index<ops::RangeInclusive<usize>> for String { } } #[stable(feature = "inclusive_range", since = "1.26.0")] -impl ops::Index<ops::RangeToInclusive<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeToInclusive<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Output = str; #[inline] @@ -2409,42 +2523,62 @@ impl ops::Index<ops::RangeToInclusive<usize>> for String { } #[stable(feature = "derefmut_for_string", since = "1.3.0")] -impl ops::IndexMut<ops::Range<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::Range<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str { &mut self[..][index] } } #[stable(feature = "derefmut_for_string", since = "1.3.0")] -impl ops::IndexMut<ops::RangeTo<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeTo<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str { &mut self[..][index] } } #[stable(feature = "derefmut_for_string", since = "1.3.0")] -impl ops::IndexMut<ops::RangeFrom<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeFrom<usize>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str { &mut self[..][index] } } #[stable(feature = "derefmut_for_string", since = "1.3.0")] -impl ops::IndexMut<ops::RangeFull> for String { +impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeFull> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str { unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) } } } #[stable(feature = "inclusive_range", since = "1.26.0")] -impl ops::IndexMut<ops::RangeInclusive<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeInclusive<usize>> + for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str { IndexMut::index_mut(&mut **self, index) } } #[stable(feature = "inclusive_range", since = "1.26.0")] -impl ops::IndexMut<ops::RangeToInclusive<usize>> for String { +impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeToInclusive<usize>> + for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str { IndexMut::index_mut(&mut **self, index) @@ -2452,7 +2586,10 @@ impl ops::IndexMut<ops::RangeToInclusive<usize>> for String { } #[stable(feature = "rust1", since = "1.0.0")] -impl ops::Deref for String { +impl<const COOP_PREFERRED: bool> ops::Deref for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Target = str; #[inline] @@ -2462,7 +2599,10 @@ impl ops::Deref for String { } #[stable(feature = "derefmut_for_string", since = "1.3.0")] -impl ops::DerefMut for String { +impl<const COOP_PREFERRED: bool> ops::DerefMut for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn deref_mut(&mut self) -> &mut str { unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) } @@ -2479,10 +2619,13 @@ pub type ParseError = core::convert::Infallible; #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl FromStr for String { +impl<const COOP_PREFERRED: bool> FromStr for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Err = core::convert::Infallible; #[inline] - fn from_str(s: &str) -> Result<String, Self::Err> { + fn from_str(s: &str) -> Result<String<COOP_PREFERRED>, Self::Err> { Ok(String::from(s)) } } @@ -2497,7 +2640,10 @@ impl FromStr for String { /// [`Display`]: fmt::Display #[cfg_attr(not(test), rustc_diagnostic_item = "ToString")] #[stable(feature = "rust1", since = "1.0.0")] -pub trait ToString { +pub trait ToString<const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts the given value to a `String`. /// /// # Examples @@ -2512,7 +2658,7 @@ pub trait ToString { /// ``` #[rustc_conversion_suggestion] #[stable(feature = "rust1", since = "1.0.0")] - fn to_string(&self) -> String; + fn to_string(&self) -> String<COOP_PREFERRED>; } /// # Panics @@ -2523,13 +2669,16 @@ pub trait ToString { /// since `fmt::Write for String` never returns an error itself. #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T: fmt::Display + ?Sized> ToString for T { +impl<T: fmt::Display + ?Sized, const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for T +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ // A common guideline is to not inline generic functions. However, // removing `#[inline]` from this method causes non-negligible regressions. // See <https://github.com/rust-lang/rust/pull/74852>, the last attempt // to try to remove it. #[inline] - default fn to_string(&self) -> String { + default fn to_string(&self) -> String<COOP_PREFERRED> { let mut buf = String::new(); let mut formatter = core::fmt::Formatter::new(&mut buf); // Bypass format_args!() to avoid write_str with zero-length strs @@ -2559,9 +2708,12 @@ impl ToString for bool { #[cfg(not(no_global_oom_handling))] #[stable(feature = "u8_to_string_specialization", since = "1.54.0")] -impl ToString for u8 { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for u8 +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] - fn to_string(&self) -> String { + fn to_string(&self) -> String<COOP_PREFERRED> { let mut buf = String::with_capacity(3); let mut n = *self; if n >= 10 { @@ -2579,9 +2731,12 @@ impl ToString for u8 { #[cfg(not(no_global_oom_handling))] #[stable(feature = "i8_to_string_specialization", since = "1.54.0")] -impl ToString for i8 { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for i8 +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] - fn to_string(&self) -> String { + fn to_string(&self) -> String<COOP_PREFERRED> { let mut buf = String::with_capacity(4); if self.is_negative() { buf.push('-'); @@ -2602,33 +2757,42 @@ impl ToString for i8 { #[cfg(not(no_global_oom_handling))] #[stable(feature = "str_to_string_specialization", since = "1.9.0")] -impl ToString for str { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for str +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] - fn to_string(&self) -> String { + fn to_string(&self) -> String<COOP_PREFERRED> { String::from(self) } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_str_to_string_specialization", since = "1.17.0")] -impl ToString for Cow<'_, str> { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for Cow<'_, str> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] - fn to_string(&self) -> String { + fn to_string(&self) -> String<COOP_PREFERRED> { self[..].to_owned() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "string_to_string_specialization", since = "1.17.0")] -impl ToString for String { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for String<COOP_PREFERRED> { #[inline] - fn to_string(&self) -> String { + fn to_string(&self) -> String<COOP_PREFERRED> { self.to_owned() } } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<str> for String { +impl<const COOP_PREFERRED: bool> AsRef<str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn as_ref(&self) -> &str { self @@ -2636,7 +2800,10 @@ impl AsRef<str> for String { } #[stable(feature = "string_as_mut", since = "1.43.0")] -impl AsMut<str> for String { +impl<const COOP_PREFERRED: bool> AsMut<str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn as_mut(&mut self) -> &mut str { self @@ -2644,7 +2811,10 @@ impl AsMut<str> for String { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<[u8]> for String { +impl<const COOP_PREFERRED: bool> AsRef<[u8]> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn as_ref(&self) -> &[u8] { self.as_bytes() @@ -2653,36 +2823,45 @@ impl AsRef<[u8]> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl From<&str> for String { +impl<const COOP_PREFERRED: bool> From<&str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts a `&str` into a [`String`]. /// /// The result is allocated on the heap. #[inline] - fn from(s: &str) -> String { + fn from(s: &str) -> String<COOP_PREFERRED> { s.to_owned() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "from_mut_str_for_string", since = "1.44.0")] -impl From<&mut str> for String { +impl<const COOP_PREFERRED: bool> From<&mut str> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts a `&mut str` into a [`String`]. /// /// The result is allocated on the heap. #[inline] - fn from(s: &mut str) -> String { + fn from(s: &mut str) -> String<COOP_PREFERRED> { s.to_owned() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "from_ref_string", since = "1.35.0")] -impl From<&String> for String { +impl<const COOP_PREFERRED: bool> From<&String<COOP_PREFERRED>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts a `&String` into a [`String`]. /// /// This clones `s` and returns the clone. #[inline] - fn from(s: &String) -> String { + fn from(s: &String<COOP_PREFERRED>) -> String<COOP_PREFERRED> { s.clone() } } @@ -2712,7 +2891,10 @@ impl From<Box<str>> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_from_str", since = "1.20.0")] -impl From<String> for Box<str> { +impl<const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Box<str> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts the given [`String`] to a boxed `str` slice that is owned. /// /// # Examples @@ -2726,14 +2908,17 @@ impl From<String> for Box<str> { /// /// assert_eq!("hello world", s3) /// ``` - fn from(s: String) -> Box<str> { + fn from(s: String<COOP_PREFERRED>) -> Box<str> { s.into_boxed_str() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "string_from_cow_str", since = "1.14.0")] -impl<'a> From<Cow<'a, str>> for String { +impl<'a, const COOP_PREFERRED: bool> From<Cow<'a, str>> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts a clone-on-write string to an owned /// instance of [`String`]. /// @@ -2750,7 +2935,7 @@ impl<'a> From<Cow<'a, str>> for String { /// let owned: String = String::from(cow); /// assert_eq!(&owned[..], "eggplant"); /// ``` - fn from(s: Cow<'a, str>) -> String { + fn from(s: Cow<'a, str>) -> String<COOP_PREFERRED> { s.into_owned() } } @@ -2778,7 +2963,10 @@ impl<'a> From<&'a str> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<String> for Cow<'a, str> { +impl<'a, const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Cow<'a, str> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts a [`String`] into an [`Owned`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -2794,14 +2982,17 @@ impl<'a> From<String> for Cow<'a, str> { /// /// [`Owned`]: crate::borrow::Cow::Owned "borrow::Cow::Owned" #[inline] - fn from(s: String) -> Cow<'a, str> { + fn from(s: String<COOP_PREFERRED>) -> Cow<'a, str> { Cow::Owned(s) } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_from_string_ref", since = "1.28.0")] -impl<'a> From<&'a String> for Cow<'a, str> { +impl<'a, const COOP_PREFERRED: bool> From<&'a String<COOP_PREFERRED>> for Cow<'a, str> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts a [`String`] reference into a [`Borrowed`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -2816,7 +3007,7 @@ impl<'a> From<&'a String> for Cow<'a, str> { /// /// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed" #[inline] - fn from(s: &'a String) -> Cow<'a, str> { + fn from(s: &'a String<COOP_PREFERRED>) -> Cow<'a, str> { Cow::Borrowed(s.as_str()) } } @@ -2839,14 +3030,20 @@ impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_str_from_iter", since = "1.12.0")] -impl<'a> FromIterator<String> for Cow<'a, str> { - fn from_iter<I: IntoIterator<Item = String>>(it: I) -> Cow<'a, str> { +impl<'a, const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for Cow<'a, str> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>(it: I) -> Cow<'a, str> { Cow::Owned(FromIterator::from_iter(it)) } } #[stable(feature = "from_string_for_vec_u8", since = "1.14.0")] -impl From<String> for Vec<u8> { +impl<const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Vec<u8, Global, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`]. /// /// # Examples @@ -2861,14 +3058,17 @@ impl From<String> for Vec<u8> { /// println!("{b}"); /// } /// ``` - fn from(string: String) -> Vec<u8> { + fn from(string: String<COOP_PREFERRED>) -> Vec<u8, Global, COOP_PREFERRED> { string.into_bytes() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Write for String { +impl<const COOP_PREFERRED: bool> fmt::Write for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn write_str(&mut self, s: &str) -> fmt::Result { self.push_str(s); @@ -2889,9 +3089,12 @@ impl fmt::Write for String { /// /// [`drain`]: String::drain #[stable(feature = "drain", since = "1.6.0")] -pub struct Drain<'a> { +pub struct Drain<'a, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Will be used as &'a mut String in the destructor - string: *mut String, + string: *mut String<COOP_PREFERRED>, /// Start of part to remove start: usize, /// End of part to remove @@ -2901,19 +3104,28 @@ pub struct Drain<'a> { } #[stable(feature = "collection_debug", since = "1.17.0")] -impl fmt::Debug for Drain<'_> { +impl<const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.as_str()).finish() } } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl Sync for Drain<'_> {} +unsafe impl<const COOP_PREFERRED: bool> Sync for Drain<'_, COOP_PREFERRED> where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: +{ +} #[stable(feature = "drain", since = "1.6.0")] -unsafe impl Send for Drain<'_> {} +unsafe impl<const COOP_PREFERRED: bool> Send for Drain<'_, COOP_PREFERRED> {} #[stable(feature = "drain", since = "1.6.0")] -impl Drop for Drain<'_> { +impl<const COOP_PREFERRED: bool> Drop for Drain<'_, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn drop(&mut self) { unsafe { // Use Vec::drain. "Reaffirm" the bounds checks to avoid @@ -2926,7 +3138,10 @@ impl Drop for Drain<'_> { } } -impl<'a> Drain<'a> { +impl<'a, const COOP_PREFERRED: bool> Drain<'a, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Returns the remaining (sub)string of this iterator as a slice. /// /// # Examples @@ -2946,21 +3161,30 @@ impl<'a> Drain<'a> { } #[stable(feature = "string_drain_as_str", since = "1.55.0")] -impl<'a> AsRef<str> for Drain<'a> { +impl<'a, const COOP_PREFERRED: bool> AsRef<str> for Drain<'a, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn as_ref(&self) -> &str { self.as_str() } } #[stable(feature = "string_drain_as_str", since = "1.55.0")] -impl<'a> AsRef<[u8]> for Drain<'a> { +impl<'a, const COOP_PREFERRED: bool> AsRef<[u8]> for Drain<'a, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn as_ref(&self) -> &[u8] { self.as_str().as_bytes() } } #[stable(feature = "drain", since = "1.6.0")] -impl Iterator for Drain<'_> { +impl<const COOP_PREFERRED: bool> Iterator for Drain<'_, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ type Item = char; #[inline] @@ -2979,7 +3203,10 @@ impl Iterator for Drain<'_> { } #[stable(feature = "drain", since = "1.6.0")] -impl DoubleEndedIterator for Drain<'_> { +impl<const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn next_back(&mut self) -> Option<char> { self.iter.next_back() @@ -2987,11 +3214,17 @@ impl DoubleEndedIterator for Drain<'_> { } #[stable(feature = "fused", since = "1.26.0")] -impl FusedIterator for Drain<'_> {} +impl<const COOP_PREFERRED: bool> FusedIterator for Drain<'_, COOP_PREFERRED> where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: +{ +} #[cfg(not(no_global_oom_handling))] #[stable(feature = "from_char_for_string", since = "1.46.0")] -impl From<char> for String { +impl<const COOP_PREFERRED: bool> From<char> for String<COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Allocates an owned [`String`] from a single character. /// /// # Example From 4bc88b4bfa07e1101dda4ea35488c46a42f45632 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 15 Dec 2022 08:43:09 -0800 Subject: [PATCH 15/47] Minor --- library/alloc/src/vec/mod.rs | 2 +- library/std/src/sys/windows/mod.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 66d1baafa5538..7db56260e6d9e 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -432,7 +432,7 @@ pub type DefVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Al /// = always pack; u8::MAX = always cooperate (if `Global` supports it). /// @FIXME A `pub const` threshold. #[unstable(feature = "global_co_alloc_vec", issue = "none")] -pub type WeVec<T, const weight: u8> = Vec<T, Global, { weight > 1 }>; +pub type WeVec<T, const weight: u8> = Vec<T, Global, { weight > 127 }>; //////////////////////////////////////////////////////////////////////////////// // Inherent methods diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index 77359abe42995..0296c2dab5552 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -204,6 +204,7 @@ where // incorrect size hints for some short paths: // https://github.com/dylni/normpath/issues/5 let mut stack_buf: [MaybeUninit<u16>; 512] = MaybeUninit::uninit_array(); + // @FIXME Use CoVec? let mut heap_buf: Vec<MaybeUninit<u16>> = Vec::new(); unsafe { let mut n = stack_buf.len(); From 017b05de100e7630598f36d2e62f327a313d84a6 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 15 Dec 2022 09:28:49 -0800 Subject: [PATCH 16/47] Moved co_alloc_metadata_num_slots_with_preference_global from ::core::alloc to ::alloc. --- library/alloc/src/boxed.rs | 2 +- .../alloc/src/collections/binary_heap/mod.rs | 4 +- library/alloc/src/lib.rs | 7 + library/alloc/src/raw_vec.rs | 2 +- library/alloc/src/rc.rs | 5 +- library/alloc/src/str.rs | 2 +- library/alloc/src/string.rs | 145 +++++++++--------- library/alloc/src/vec/mod.rs | 1 + library/core/src/alloc/mod.rs | 13 -- 9 files changed, 87 insertions(+), 94 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 65baed0056619..615744a2d60a3 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -1686,7 +1686,7 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> { impl<T, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, Global, COOP_PREFERRED>> for Box<[T; N]> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Error = Vec<T, Global, COOP_PREFERRED>; diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index 8409954c5dbc8..f1b0cc4a72424 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -1525,7 +1525,7 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {} #[derive(Debug)] pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { iter: vec::Drain<'a, T, Global, COOP_PREFERRED>, } @@ -1533,7 +1533,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Item = T; diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 5d856158c206c..9400be19d6130 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -284,4 +284,11 @@ pub(crate) mod test_helpers { let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap(); rand::SeedableRng::from_seed(seed) } + +use crate::alloc::Global; + +/// See also `core::alloc::co_alloc_metadata_num_slots_with_preference`. +#[unstable(feature = "global_co_alloc", issue = "none")] +pub const fn co_alloc_metadata_num_slots_with_preference_global(coop_preferred: bool) -> usize { + if Global::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } } diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index e195285511430..99c1b4c6dc13d 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -69,7 +69,7 @@ pub(crate) struct RawVec< impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so /// they cannot call `Self::new()`. diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index b1cffc74f283c..aeffaf7afbcd5 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -247,7 +247,6 @@ use crate::boxed::Box; #[cfg(test)] use std::boxed::Box; -use crate::alloc; use core::any::Any; use core::borrow; use core::cell::Cell; @@ -1990,7 +1989,7 @@ impl<T: ?Sized> From<Box<T>> for Rc<T> { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl<T, const COOP_PREFERRED: bool> From<Vec<T, Global, COOP_PREFERRED>> for Rc<[T]> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Allocate a reference-counted slice and move `v`'s items into it. /// @@ -2005,7 +2004,7 @@ where #[inline] fn from(mut v: Vec<T, Global, COOP_PREFERRED>) -> Rc<[T]> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { unsafe { let rc = Rc::copy_from_slice(&v); diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index eedf856f5d253..93cdeae87edc1 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -136,7 +136,7 @@ where T: Copy, B: AsRef<[T]> + ?Sized, S: Borrow<B>, - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { let sep_len = sep.len(); let mut iter = slice.iter(); diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index bbb320195839d..99253153f97f6 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -45,7 +45,6 @@ use crate::alloc::Global; -use crate::alloc; use crate::vec::PlVec; #[cfg(not(no_global_oom_handling))] use core::char::{decode_utf16, REPLACEMENT_CHARACTER}; @@ -372,7 +371,7 @@ use crate::vec::DEFAULT_COOP_PREFERRED; #[cfg_attr(not(test), lang = "String")] pub struct String<const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { vec: Vec<u8, Global, COOP_PREFERRED>, } @@ -440,7 +439,7 @@ pub struct FromUtf16Error(()); impl<const COOP_PREFERRED: bool> String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Creates a new empty `String`. /// @@ -521,7 +520,7 @@ where #[cfg(test)] pub fn from_str(_: &str) -> String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { panic!("not available with cfg(test)"); } @@ -886,7 +885,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] pub fn into_bytes(self) -> Vec<u8, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { self.vec } @@ -1636,7 +1635,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { &mut self.vec } @@ -1713,7 +1712,7 @@ where #[must_use = "use `.truncate()` if you don't need the other half"] pub fn split_off(&mut self, at: usize) -> String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { assert!(self.is_char_boundary(at)); let other = self.vec.split_off(at); @@ -1953,7 +1952,7 @@ impl FromUtf8Error { #[stable(feature = "rust1", since = "1.0.0")] pub fn into_bytes<const COOP_PREFERRED: bool>(self) -> Vec<u8, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { self.bytes } @@ -2022,7 +2021,7 @@ impl Error for FromUtf16Error { #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> Clone for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn clone(&self) -> Self { String { vec: self.vec.clone() } @@ -2037,7 +2036,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> FromIterator<char> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2050,7 +2049,7 @@ where #[stable(feature = "string_from_iter_by_ref", since = "1.17.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<&'a char> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2063,7 +2062,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<&'a str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2076,7 +2075,7 @@ where #[stable(feature = "extend_string", since = "1.4.0")] impl<const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>( iter: I, @@ -2100,7 +2099,7 @@ where #[stable(feature = "box_str2", since = "1.45.0")] impl<const COOP_PREFERRED: bool> FromIterator<Box<str>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2113,7 +2112,7 @@ where #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<Cow<'a, str>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String<COOP_PREFERRED> { let mut iterator = iter.into_iter(); @@ -2136,7 +2135,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> Extend<char> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) { let iterator = iter.into_iter(); @@ -2160,7 +2159,7 @@ where #[stable(feature = "extend_ref", since = "1.2.0")] impl<'a, const COOP_PREFERRED: bool> Extend<&'a char> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) { self.extend(iter.into_iter().cloned()); @@ -2181,7 +2180,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<'a, const COOP_PREFERRED: bool> Extend<&'a str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(s)); @@ -2205,7 +2204,7 @@ impl Extend<Box<str>> for String { #[stable(feature = "extend_string", since = "1.4.0")] impl<const COOP_PREFERRED: bool> Extend<String> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); @@ -2221,7 +2220,7 @@ where #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a, const COOP_PREFERRED: bool> Extend<Cow<'a, str>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); @@ -2247,7 +2246,7 @@ where )] impl<'a, 'b, const COOP_PREFERRED: bool> Pattern<'a> for &'b String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Searcher = <&'b str as Pattern<'a>>::Searcher; @@ -2284,7 +2283,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> PartialEq for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn eq(&self, other: &String<COOP_PREFERRED>) -> bool { @@ -2340,7 +2339,7 @@ impl_eq! { Cow<'a, str>, String } #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] impl<const COOP_PREFERRED: bool> const Default for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Creates an empty `String`. #[inline] @@ -2352,7 +2351,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> fmt::Display for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -2363,7 +2362,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> fmt::Debug for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -2374,7 +2373,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> hash::Hash for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn hash<H: hash::Hasher>(&self, hasher: &mut H) { @@ -2423,7 +2422,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> Add<&str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Output = String<COOP_PREFERRED>; @@ -2441,7 +2440,7 @@ where #[stable(feature = "stringaddassign", since = "1.12.0")] impl<const COOP_PREFERRED: bool> AddAssign<&str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn add_assign(&mut self, other: &str) { @@ -2452,7 +2451,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::Range<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Output = str; @@ -2464,7 +2463,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeTo<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Output = str; @@ -2476,7 +2475,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeFrom<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Output = str; @@ -2488,7 +2487,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeFull> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Output = str; @@ -2500,7 +2499,7 @@ where #[stable(feature = "inclusive_range", since = "1.26.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeInclusive<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Output = str; @@ -2512,7 +2511,7 @@ where #[stable(feature = "inclusive_range", since = "1.26.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeToInclusive<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Output = str; @@ -2525,7 +2524,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::Range<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str { @@ -2535,7 +2534,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeTo<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str { @@ -2545,7 +2544,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeFrom<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str { @@ -2555,7 +2554,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeFull> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str { @@ -2566,7 +2565,7 @@ where impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeInclusive<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str { @@ -2577,7 +2576,7 @@ where impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeToInclusive<usize>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str { @@ -2588,7 +2587,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Deref for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Target = str; @@ -2601,7 +2600,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::DerefMut for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn deref_mut(&mut self) -> &mut str { @@ -2621,7 +2620,7 @@ pub type ParseError = core::convert::Infallible; #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> FromStr for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Err = core::convert::Infallible; #[inline] @@ -2642,7 +2641,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] pub trait ToString<const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts the given value to a `String`. /// @@ -2671,7 +2670,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: fmt::Display + ?Sized, const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for T where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { // A common guideline is to not inline generic functions. However, // removing `#[inline]` from this method causes non-negligible regressions. @@ -2710,7 +2709,7 @@ impl ToString for bool { #[stable(feature = "u8_to_string_specialization", since = "1.54.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for u8 where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2733,7 +2732,7 @@ where #[stable(feature = "i8_to_string_specialization", since = "1.54.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for i8 where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2759,7 +2758,7 @@ where #[stable(feature = "str_to_string_specialization", since = "1.9.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for str where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2771,7 +2770,7 @@ where #[stable(feature = "cow_str_to_string_specialization", since = "1.17.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for Cow<'_, str> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2791,7 +2790,7 @@ impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for String<COOP_PREFER #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> AsRef<str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn as_ref(&self) -> &str { @@ -2802,7 +2801,7 @@ where #[stable(feature = "string_as_mut", since = "1.43.0")] impl<const COOP_PREFERRED: bool> AsMut<str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn as_mut(&mut self) -> &mut str { @@ -2813,7 +2812,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> AsRef<[u8]> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn as_ref(&self) -> &[u8] { @@ -2825,7 +2824,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> From<&str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts a `&str` into a [`String`]. /// @@ -2840,7 +2839,7 @@ where #[stable(feature = "from_mut_str_for_string", since = "1.44.0")] impl<const COOP_PREFERRED: bool> From<&mut str> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts a `&mut str` into a [`String`]. /// @@ -2855,7 +2854,7 @@ where #[stable(feature = "from_ref_string", since = "1.35.0")] impl<const COOP_PREFERRED: bool> From<&String<COOP_PREFERRED>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts a `&String` into a [`String`]. /// @@ -2893,7 +2892,7 @@ impl From<Box<str>> for String { #[stable(feature = "box_from_str", since = "1.20.0")] impl<const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Box<str> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts the given [`String`] to a boxed `str` slice that is owned. /// @@ -2917,7 +2916,7 @@ where #[stable(feature = "string_from_cow_str", since = "1.14.0")] impl<'a, const COOP_PREFERRED: bool> From<Cow<'a, str>> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts a clone-on-write string to an owned /// instance of [`String`]. @@ -2965,7 +2964,7 @@ impl<'a> From<&'a str> for Cow<'a, str> { #[stable(feature = "rust1", since = "1.0.0")] impl<'a, const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Cow<'a, str> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts a [`String`] into an [`Owned`] variant. /// No heap allocation is performed, and the string @@ -2991,7 +2990,7 @@ where #[stable(feature = "cow_from_string_ref", since = "1.28.0")] impl<'a, const COOP_PREFERRED: bool> From<&'a String<COOP_PREFERRED>> for Cow<'a, str> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts a [`String`] reference into a [`Borrowed`] variant. /// No heap allocation is performed, and the string @@ -3032,7 +3031,7 @@ impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> { #[stable(feature = "cow_str_from_iter", since = "1.12.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for Cow<'a, str> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>(it: I) -> Cow<'a, str> { Cow::Owned(FromIterator::from_iter(it)) @@ -3042,7 +3041,7 @@ where #[stable(feature = "from_string_for_vec_u8", since = "1.14.0")] impl<const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Vec<u8, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`]. /// @@ -3067,7 +3066,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> fmt::Write for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn write_str(&mut self, s: &str) -> fmt::Result { @@ -3091,7 +3090,7 @@ where #[stable(feature = "drain", since = "1.6.0")] pub struct Drain<'a, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Will be used as &'a mut String in the destructor string: *mut String<COOP_PREFERRED>, @@ -3106,7 +3105,7 @@ where #[stable(feature = "collection_debug", since = "1.17.0")] impl<const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.as_str()).finish() @@ -3115,7 +3114,7 @@ where #[stable(feature = "drain", since = "1.6.0")] unsafe impl<const COOP_PREFERRED: bool> Sync for Drain<'_, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: { } #[stable(feature = "drain", since = "1.6.0")] @@ -3124,7 +3123,7 @@ unsafe impl<const COOP_PREFERRED: bool> Send for Drain<'_, COOP_PREFERRED> {} #[stable(feature = "drain", since = "1.6.0")] impl<const COOP_PREFERRED: bool> Drop for Drain<'_, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { @@ -3140,7 +3139,7 @@ where impl<'a, const COOP_PREFERRED: bool> Drain<'a, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Returns the remaining (sub)string of this iterator as a slice. /// @@ -3163,7 +3162,7 @@ where #[stable(feature = "string_drain_as_str", since = "1.55.0")] impl<'a, const COOP_PREFERRED: bool> AsRef<str> for Drain<'a, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn as_ref(&self) -> &str { self.as_str() @@ -3173,7 +3172,7 @@ where #[stable(feature = "string_drain_as_str", since = "1.55.0")] impl<'a, const COOP_PREFERRED: bool> AsRef<[u8]> for Drain<'a, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[u8] { self.as_str().as_bytes() @@ -3183,7 +3182,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<const COOP_PREFERRED: bool> Iterator for Drain<'_, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { type Item = char; @@ -3205,7 +3204,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<char> { @@ -3215,7 +3214,7 @@ where #[stable(feature = "fused", since = "1.26.0")] impl<const COOP_PREFERRED: bool> FusedIterator for Drain<'_, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: { } @@ -3223,7 +3222,7 @@ impl<const COOP_PREFERRED: bool> FusedIterator for Drain<'_, COOP_PREFERRED> whe #[stable(feature = "from_char_for_string", since = "1.46.0")] impl<const COOP_PREFERRED: bool> From<char> for String<COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Allocates an owned [`String`] from a single character. /// diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 7db56260e6d9e..30b57b8d32e95 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -400,6 +400,7 @@ mod spec_extend; pub struct Vec< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, + //@FIXME: #[unstable(feature ="global_co_alloc_vec", issue="none")] const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, > where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 807c61effb819..491bc45a19128 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -105,19 +105,6 @@ pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>( if A::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } } -#[unstable(feature = "global_co_alloc", issue = "none")] -/// Param `coop_preferred` - if false, then this returns `0`, regardless of -/// whether allocator `A` is cooperative. -pub const fn co_alloc_metadata_num_slots_with_preference_global(_coop_preferred: bool) -> usize { - if true { - panic!("FIXME"); - } - // @FIXME Move these functions to :alloc instead. - // Then: - 0 - //if Global::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } -} - /// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of /// data described via [`Layout`][]. /// From 5a9e6bc50475d6b8b9bb8cd0283d890c5a13bfeb Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 15 Dec 2022 09:31:11 -0800 Subject: [PATCH 17/47] Renamed ::core::alloc::co_alloc_metadata_num_slots_with_preference to ::core::alloc::co_alloc_metadata_num_slots_with_preference_specific. --- .../alloc/src/collections/vec_deque/drain.rs | 24 ++--- .../src/collections/vec_deque/into_iter.rs | 2 +- .../alloc/src/collections/vec_deque/macros.rs | 2 +- .../alloc/src/collections/vec_deque/mod.rs | 56 ++++++------ .../src/collections/vec_deque/spec_extend.rs | 12 +-- .../collections/vec_deque/spec_from_iter.rs | 6 +- library/alloc/src/raw_vec.rs | 20 +++-- library/alloc/src/slice.rs | 40 ++++++--- library/alloc/src/vec/drain.rs | 28 +++--- library/alloc/src/vec/drain_filter.rs | 12 +-- library/alloc/src/vec/into_iter.rs | 36 ++++---- library/alloc/src/vec/mod.rs | 90 ++++++++++--------- library/alloc/src/vec/partial_eq.rs | 20 ++--- library/alloc/src/vec/spec_extend.rs | 10 +-- library/alloc/src/vec/spec_from_elem.rs | 8 +- library/core/src/alloc/mod.rs | 2 +- library/std/src/io/cursor.rs | 12 +-- 17 files changed, 206 insertions(+), 174 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index 0bcf82e3ceff5..2004060c4bb96 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -21,7 +21,7 @@ pub struct Drain< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { // We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T // and we want it to be covariant instead @@ -39,7 +39,7 @@ pub struct Drain< impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { pub(super) unsafe fn new( deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, @@ -97,7 +97,7 @@ where impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain") @@ -113,33 +113,33 @@ where unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool>( &'r mut Drain<'a, T, A, COOP_PREFERRED>, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:; impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { if self.0.remaining != 0 { @@ -223,7 +223,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -249,7 +249,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<T> { @@ -266,12 +266,12 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "fused", since = "1.26.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: { } diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index 7924098c14db2..1c3bedd21e7e2 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -19,7 +19,7 @@ pub struct IntoIter< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = true, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { inner: VecDeque<T, A, COOP_PREFERRED>, } diff --git a/library/alloc/src/collections/vec_deque/macros.rs b/library/alloc/src/collections/vec_deque/macros.rs index 68ee43152b5b5..77885e6811a3b 100644 --- a/library/alloc/src/collections/vec_deque/macros.rs +++ b/library/alloc/src/collections/vec_deque/macros.rs @@ -4,7 +4,7 @@ macro_rules! __impl_slice_eq1 { impl<T, U, A: Allocator, const COOP_PREFERRED: bool, $($vars)*> PartialEq<$rhs> for $lhs where T: PartialEq<U>, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, $($constraints)* { fn eq(&self, other: &$rhs) -> bool { diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 422d9287ab4a7..9107526f23cf5 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -98,7 +98,7 @@ pub struct VecDeque< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { // `self[0]`, if it exists, is `buf[head]`. // `head < buf.capacity()`, unless `buf.capacity() == 0` when `head == 0`. @@ -114,7 +114,7 @@ pub struct VecDeque< impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn clone(&self) -> Self { let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone()); @@ -132,7 +132,7 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { /// Runs the destructor for all items in the slice when it gets dropped (normally or @@ -168,7 +168,7 @@ impl<T, A: Allocator, const COOP_PREFERRED: bool> Default for VecDeque<T, A, COO impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Marginally more convenient #[inline] @@ -460,7 +460,7 @@ where ) -> usize { struct Guard<'a, T, A: Allocator, const COOP_PREFERRED: bool> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, written: usize, @@ -468,7 +468,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for Guard<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { self.deque.len += self.written; @@ -549,7 +549,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Creates an empty deque. /// @@ -588,7 +588,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Creates an empty deque. /// @@ -2626,7 +2626,7 @@ where impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Modifies the deque in-place so that `len()` is equal to new_len, /// either by removing excess elements from the back or by appending clones of `value` @@ -2675,7 +2675,7 @@ fn wrap_index(logical_index: usize, capacity: usize) -> usize { impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> PartialEq for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn eq(&self, other: &Self) -> bool { if self.len != other.len() { @@ -2716,7 +2716,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: { } @@ -2731,7 +2731,7 @@ __impl_slice_eq1! { [const N: usize] VecDeque<T, A, COOP_PREFERRED>, &mut [U; N] impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.iter().partial_cmp(other.iter()) @@ -2741,7 +2741,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn cmp(&self, other: &Self) -> Ordering { @@ -2752,7 +2752,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn hash<H: Hasher>(&self, state: &mut H) { state.write_length_prefix(self.len); @@ -2769,7 +2769,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Index<usize> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Output = T; @@ -2782,7 +2782,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IndexMut<usize> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: usize) -> &mut T { @@ -2793,7 +2793,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> { SpecFromIter::spec_from_iter(iter.into_iter()) @@ -2803,7 +2803,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2819,7 +2819,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -2833,7 +2833,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; @@ -2846,7 +2846,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()); @@ -2867,7 +2867,7 @@ where impl<'a, T: 'a + Copy, A: Allocator, const COOP_PREFERRED: bool> Extend<&'a T> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()); @@ -2888,7 +2888,7 @@ where impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() @@ -2899,7 +2899,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Turn a [`Vec<T>`] into a [`VecDeque<T>`]. /// @@ -2912,7 +2912,9 @@ where #[inline] fn from<const OTHER_COOP_PREFERRED: bool>(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(OTHER_COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + OTHER_COOP_PREFERRED, + )]:, { let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } @@ -2923,7 +2925,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> From<VecDeque<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Turn a [`VecDeque<T>`] into a [`Vec<T>`]. /// @@ -2979,7 +2981,7 @@ where impl<T, const N: usize, A: Allocator, const COOP_PREFERRED: bool> From<[T; N]> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Converts a `[T; N]` into a `VecDeque<T>`. /// diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index b1a9d23fd4a99..973de88ffb172 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -17,7 +17,7 @@ impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, mut iter: I) { // This function should be the moral equivalent of: @@ -31,7 +31,7 @@ where deque: &mut VecDeque<T, A, COOP_PREFERRED>, element: T, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { // SAFETY: Because of the precondition, it's guaranteed that there is space // in the logical array after the last element. @@ -63,7 +63,7 @@ impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iter: I) { // This is the case for a TrustedLen iterator. @@ -99,7 +99,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) { let slice = iterator.as_slice(); @@ -118,7 +118,7 @@ impl<'a, T: 'a, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, I where I: Iterator<Item = &'a T>, T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.copied()) @@ -129,7 +129,7 @@ impl<'a, T: 'a, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, slic for VecDeque<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index 85dd756337350..086a1fa74566c 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -11,7 +11,7 @@ impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn spec_from_iter(iterator: I) -> Self { // Since converting is O(1) now, just re-use the `Vec` logic for @@ -25,7 +25,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self { @@ -36,7 +36,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn spec_from_iter(iterator: IntoIter<T>) -> Self { diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 99c1b4c6dc13d..f74e876b6cb9c 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -55,7 +55,7 @@ pub(crate) struct RawVec< A: Allocator = Global, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { ptr: Unique<T>, cap: usize, @@ -64,7 +64,7 @@ pub(crate) struct RawVec< //pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}], //pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()], pub(crate) metas: [GlobalCoAllocMeta; - alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], + alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)], } impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED> @@ -119,7 +119,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { // Tiny Vecs are dumb. Skip to: // - 8 if the element size is 1, because any heap allocators is likely @@ -143,7 +143,7 @@ where cap: 0, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/}; - alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], + alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)], } } @@ -222,7 +222,9 @@ where cap: capacity, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; - alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], + alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )], } } } @@ -244,7 +246,7 @@ where cap: capacity, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; - alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], + alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)], } } @@ -314,7 +316,7 @@ where len: usize, additional: usize, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { handle_reserve(slf.grow_amortized(len, additional)); } @@ -390,7 +392,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Returns if the buffer needs to grow to fulfill the needed extra capacity. /// Mainly used to make inlining reserve-calls possible without inlining `grow`. @@ -512,7 +514,7 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for RawVec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. default fn drop(&mut self) { diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 696bb1454b22e..a4fb52d93b953 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -99,7 +99,9 @@ pub(crate) mod hack { b: Box<[T], A>, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { unsafe { let len = b.len(); @@ -115,7 +117,9 @@ pub(crate) mod hack { alloc: A, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { T::to_vec(s, alloc) } @@ -128,7 +132,9 @@ pub(crate) mod hack { ) -> Vec<Self, A, COOP_PREFERRED> where Self: Sized, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:; } #[cfg(not(no_global_oom_handling))] @@ -139,18 +145,24 @@ pub(crate) mod hack { alloc: A, ) -> Vec<Self, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { vec: &'a mut Vec<T, A, COOP_PREFERRED>, num_init: usize, } impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'a, T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { #[inline] fn drop(&mut self) { @@ -188,7 +200,9 @@ pub(crate) mod hack { alloc: A, ) -> Vec<Self, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { let mut v = Vec::with_capacity_in(s.len(), alloc); // SAFETY: @@ -470,7 +484,9 @@ impl<T> [T] { ) -> Vec<T, A, COOP_PREFERRED> where T: Clone, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { // N.B., see the `hack` module in this file for more details. hack::to_vec(self, alloc) @@ -497,7 +513,9 @@ impl<T> [T] { self: Box<Self, A>, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { // N.B., see the `hack` module in this file for more details. hack::into_vec(self) @@ -814,7 +832,7 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Borrow<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn borrow(&self) -> &[T] { &self[..] @@ -824,7 +842,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> BorrowMut<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn borrow_mut(&mut self) -> &mut [T] { &mut self[..] diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index d7f7292e4f1cf..2159edc7b1a23 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -25,7 +25,7 @@ pub struct Drain< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Index of tail to preserve pub(super) tail_start: usize, @@ -40,7 +40,7 @@ pub struct Drain< impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() @@ -49,7 +49,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Returns the remaining items of this iterator as a slice. /// @@ -151,7 +151,7 @@ where #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Drain<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[T] { self.as_slice() @@ -162,21 +162,21 @@ where unsafe impl<T: Sync, A: Sync + Allocator, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] unsafe impl<T: Send, A: Send + Allocator, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -194,7 +194,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<T> { @@ -205,7 +205,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { /// Moves back the un-`Drain`ed elements to restore the original `Vec`. @@ -213,12 +213,12 @@ where &'r mut Drain<'a, T, A, COOP_PREFERRED>, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:; impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { if self.0.tail_len > 0 { @@ -286,7 +286,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn is_empty(&self) -> bool { self.iter.is_empty() @@ -297,12 +297,12 @@ where unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "fused", since = "1.26.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: { } diff --git a/library/alloc/src/vec/drain_filter.rs b/library/alloc/src/vec/drain_filter.rs index 89baafca46729..29cf29e2532d9 100644 --- a/library/alloc/src/vec/drain_filter.rs +++ b/library/alloc/src/vec/drain_filter.rs @@ -27,7 +27,7 @@ pub struct DrainFilter< const COOP_PREFERRED: bool = true, > where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { pub(super) vec: &'a mut Vec<T, A, COOP_PREFERRED>, /// The index of the item that will be inspected by the next call to `next`. @@ -49,7 +49,7 @@ pub struct DrainFilter< impl<T, F, A: Allocator, const COOP_PREFERRED: bool> DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Returns a reference to the underlying allocator. #[unstable(feature = "allocator_api", issue = "32838")] @@ -119,7 +119,7 @@ impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Iterator for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -159,13 +159,13 @@ impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Drop for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { struct BackshiftOnDrop<'a, 'b, T, F, A: Allocator, const COOP_PREFERRED: bool> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { drain: &'b mut DrainFilter<'a, T, F, A, COOP_PREFERRED>, } @@ -174,7 +174,7 @@ where for BackshiftOnDrop<'a, 'b, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 790e6323f514e..98b7a7cda6731 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -33,7 +33,7 @@ pub struct IntoIter< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { pub(super) buf: NonNull<T>, pub(super) phantom: PhantomData<T>, @@ -51,7 +51,7 @@ pub struct IntoIter< impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.as_slice()).finish() @@ -60,7 +60,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Returns the remaining items of this iterator as a slice. /// @@ -179,7 +179,7 @@ where #[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[T] { self.as_slice() @@ -190,21 +190,21 @@ where unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -322,7 +322,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<T> { @@ -367,7 +367,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn is_empty(&self) -> bool { self.ptr == self.end @@ -376,7 +376,7 @@ where #[stable(feature = "fused", since = "1.26.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: { } @@ -384,7 +384,7 @@ impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } @@ -406,7 +406,7 @@ unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedRandomAccessNoCo for IntoIter<T, A, COOP_PREFERRED> where T: NonDrop, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { const MAY_HAVE_SIDE_EFFECT: bool = false; } @@ -416,7 +416,7 @@ where impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[cfg(not(test))] fn clone(&self) -> Self { @@ -432,18 +432,18 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool>( &'a mut IntoIter<T, A, COOP_PREFERRED>, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:; impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { @@ -475,7 +475,7 @@ where unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> InPlaceIterable for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { } @@ -484,7 +484,7 @@ where unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> SourceIter for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Source = Self; diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 30b57b8d32e95..3692169f7ae6e 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -403,7 +403,7 @@ pub struct Vec< //@FIXME: #[unstable(feature ="global_co_alloc_vec", issue="none")] const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, > where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { buf: RawVec<T, A, COOP_PREFERRED>, len: usize, @@ -628,7 +628,7 @@ impl<T> Vec<T> { impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Constructs a new, empty `Vec<T, A>`. /// @@ -1644,7 +1644,9 @@ where // In cases when predicate and `drop` never panick, it will be optimized out. struct BackshiftOnDrop<'a, T, A: Allocator, const VEC_IS_COOP: bool = true> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + VEC_IS_COOP, + )]:, { v: &'a mut Vec<T, A, VEC_IS_COOP>, processed_len: usize, @@ -1654,7 +1656,9 @@ where impl<T, A: Allocator, const VEC_IS_COOP: bool> Drop for BackshiftOnDrop<'_, T, A, VEC_IS_COOP> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + VEC_IS_COOP, + )]:, { fn drop(&mut self) { if self.deleted_cnt > 0 { @@ -1687,7 +1691,9 @@ where g: &mut BackshiftOnDrop<'_, T, A, VEC_IS_COOP>, ) where F: FnMut(&mut T) -> bool, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(VEC_IS_COOP)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + VEC_IS_COOP, + )]:, { while g.processed_len != original_len { // SAFETY: Unchecked element must be valid. @@ -1782,7 +1788,9 @@ where /* INVARIANT: vec.len() > read >= write > write-1 >= 0 */ struct FillGapOnDrop<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { /* Offset of the element we want to check if it is duplicate */ read: usize, @@ -1798,7 +1806,9 @@ where impl<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> Drop for FillGapOnDrop<'a, T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + COOP_PREFERRED, + )]:, { fn drop(&mut self) { /* This code gets executed when `same_bucket` panics */ @@ -2394,7 +2404,7 @@ where impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. /// @@ -2496,7 +2506,7 @@ where impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> Vec<[T; N], A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`. /// @@ -2560,7 +2570,7 @@ impl<T: Clone> ExtendWith<T> for ExtendElement<T> { impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[cfg(not(no_global_oom_handling))] /// Extend the vector by `n` values, using the given generator. @@ -2595,7 +2605,7 @@ where impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Removes consecutive repeated elements in the vector according to the /// [`PartialEq`] trait implementation. @@ -2638,7 +2648,7 @@ pub fn from_elem_in<T: Clone, A: Allocator, const COOP_PREFERRED: bool>( alloc: A, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { <T as SpecFromElem>::from_elem(elem, n, alloc) } @@ -2654,7 +2664,7 @@ trait ExtendFromWithinSpec { impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { // SAFETY: @@ -2677,7 +2687,7 @@ where impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { let count = src.len(); @@ -2713,7 +2723,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::Deref for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Target = [T]; @@ -2726,7 +2736,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::DerefMut for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn deref_mut(&mut self) -> &mut [T] { @@ -2742,7 +2752,7 @@ trait SpecCloneFrom { #[cfg(not(no_global_oom_handling))] impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn clone_from(this: &mut Self, other: &Self) { // drop anything that will not be overwritten @@ -2761,7 +2771,7 @@ where #[cfg(not(no_global_oom_handling))] impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn clone_from(this: &mut Self, other: &Self) { this.clear(); @@ -2773,7 +2783,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[cfg(not(test))] fn clone(&self) -> Self { @@ -2811,7 +2821,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { @@ -2827,7 +2837,7 @@ where impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> Index<I> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Output = I::Output; @@ -2845,7 +2855,7 @@ where impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> IndexMut<I> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { @@ -2865,7 +2875,7 @@ impl<T> FromIterator<T> for Vec<T> { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2913,7 +2923,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; @@ -2927,7 +2937,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; @@ -2941,7 +2951,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { @@ -2961,7 +2971,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { // leaf method to which various SpecFrom/SpecExtend implementations delegate when // they have no further optimizations to apply @@ -3139,7 +3149,7 @@ where impl<'a, T: Copy + 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> Extend<&'a T> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()) @@ -3161,7 +3171,7 @@ where impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { @@ -3171,7 +3181,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: { } @@ -3179,7 +3189,7 @@ impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for Vec<T, A, COOP_PREF #[stable(feature = "rust1", since = "1.0.0")] impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { #[inline] fn cmp(&self, other: &Self) -> Ordering { @@ -3191,7 +3201,7 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { @@ -3219,7 +3229,7 @@ impl<T> const Default for Vec<T> { impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) @@ -3230,7 +3240,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &Vec<T, A, COOP_PREFERRED> { self @@ -3241,7 +3251,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn as_mut(&mut self) -> &mut Vec<T, A, COOP_PREFERRED> { self @@ -3251,7 +3261,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[T] { self @@ -3261,7 +3271,7 @@ where #[stable(feature = "vec_as_mut", since = "1.5.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn as_mut(&mut self) -> &mut [T] { self @@ -3361,7 +3371,7 @@ where #[stable(feature = "vec_from_box", since = "1.18.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Box<[T], A>> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Convert a boxed slice into a vector by transferring ownership of /// the existing heap allocation. @@ -3383,7 +3393,7 @@ where #[stable(feature = "box_from_vec", since = "1.20.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for Box<[T], A> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { /// Convert a vector into a boxed slice. /// @@ -3427,7 +3437,7 @@ impl From<&str> for Vec<u8> { impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, A, COOP_PREFERRED>> for [T; N] where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { type Error = Vec<T, A, COOP_PREFERRED>; diff --git a/library/alloc/src/vec/partial_eq.rs b/library/alloc/src/vec/partial_eq.rs index bcf52b7333218..7b364e8d4bacd 100644 --- a/library/alloc/src/vec/partial_eq.rs +++ b/library/alloc/src/vec/partial_eq.rs @@ -21,21 +21,21 @@ macro_rules! __impl_slice_eq1 { } } -__impl_slice_eq1! { [A1: Allocator, A2: Allocator, const COOP_PREFERRED1: bool, const COOP_PREFERRED2: bool] Vec<T, A1, COOP_PREFERRED1>, Vec<U, A2, COOP_PREFERRED2>, #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A1>(COOP_PREFERRED1)]:, [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A2>(COOP_PREFERRED2)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &[U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &[T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &mut [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, [U], #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A1: Allocator, A2: Allocator, const COOP_PREFERRED1: bool, const COOP_PREFERRED2: bool] Vec<T, A1, COOP_PREFERRED1>, Vec<U, A2, COOP_PREFERRED2>, #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A1>(COOP_PREFERRED1)]:, [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A2>(COOP_PREFERRED2)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &[U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &[T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &mut [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, [U], #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } #[cfg(not(no_global_oom_handling))] -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Cow<'_, [T]>, Vec<U, A, COOP_PREFERRED>, #[stable(feature = "rust1", since = "1.0.0")], T: Clone, [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Cow<'_, [T]>, Vec<U, A, COOP_PREFERRED>, #[stable(feature = "rust1", since = "1.0.0")], T: Clone, [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } #[cfg(not(no_global_oom_handling))] __impl_slice_eq1! { [] Cow<'_, [T]>, &[U], #[stable(feature = "rust1", since = "1.0.0")], T: Clone } #[cfg(not(no_global_oom_handling))] __impl_slice_eq1! { [] Cow<'_, [T]>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], T: Clone } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, [U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, &[U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, [U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, &[U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } // NOTE: some less important impls are omitted to reduce code bloat // FIXME(Centril): Reconsider this? diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index 73cf325889290..ea91491f62558 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -13,7 +13,7 @@ pub(super) trait SpecExtend<T, I> { impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iter: I) { self.extend_desugared(iter) @@ -23,7 +23,7 @@ where impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.extend_trusted(iterator) @@ -33,7 +33,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, IntoIter<T>> for Vec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, mut iterator: IntoIter<T>) { unsafe { @@ -48,7 +48,7 @@ impl<'a, T: 'a, I, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a where I: Iterator<Item = &'a T>, T: Clone, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.cloned()) @@ -59,7 +59,7 @@ impl<'a, T: 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, for Vec<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index 3766f6de061bb..4c29f6184c149 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -24,7 +24,7 @@ impl<T: Clone> SpecFromElem for T { alloc: A, ) -> Vec<Self, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { let mut v = Vec::with_capacity_in(n, alloc); v.extend_with(n, ExtendElement(elem)); @@ -40,7 +40,7 @@ impl<T: Clone + IsZero> SpecFromElem for T { alloc: A, ) -> Vec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { if elem.is_zero() { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; @@ -59,7 +59,7 @@ impl SpecFromElem for i8 { alloc: A, ) -> Vec<i8, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; @@ -81,7 +81,7 @@ impl SpecFromElem for u8 { alloc: A, ) -> Vec<u8, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 491bc45a19128..c268dd9368bc3 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -99,7 +99,7 @@ pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { #[unstable(feature = "global_co_alloc", issue = "none")] /// Param `coop_preferred` - if false, then this returns `0`, regardless of /// whether allocator `A` is cooperative. -pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>( +pub const fn co_alloc_metadata_num_slots_with_preference_specific<A: Allocator>( coop_preferred: bool, ) -> usize { if A::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index 63045ebcb8157..f8d2ca1d3e74c 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -404,7 +404,7 @@ fn reserve_and_pad<A: Allocator, const COOP_PREFERRED: bool>( buf_len: usize, ) -> io::Result<usize> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { let pos: usize = (*pos_mut).try_into().map_err(|_| { io::const_io_error!( @@ -451,7 +451,7 @@ unsafe fn vec_write_unchecked<A, const COOP_PREFERRED: bool>( ) -> usize where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { debug_assert!(vec.capacity() >= pos + buf.len()); vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()); @@ -474,7 +474,7 @@ fn vec_write<A, const COOP_PREFERRED: bool>( ) -> io::Result<usize> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { let buf_len = buf.len(); let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?; @@ -510,7 +510,7 @@ fn vec_write_vectored<A, const COOP_PREFERRED: bool>( ) -> io::Result<usize> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { // For safety reasons, we don't want this sum to overflow ever. // If this saturates, the reserve should panic to avoid any unsound writing. @@ -561,7 +561,7 @@ impl Write for Cursor<&mut [u8]> { impl<A, const COOP_PREFERRED: bool> Write for Cursor<&mut Vec<u8, A, COOP_PREFERRED>> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, self.inner, buf) @@ -586,7 +586,7 @@ where impl<A, const COOP_PREFERRED: bool> Write for Cursor<Vec<u8, A, COOP_PREFERRED>> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, &mut self.inner, buf) From d0704782d6f39df37ead03a0ee25d238cc7cb6be Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 15 Dec 2022 16:43:07 -0800 Subject: [PATCH 18/47] Minor; nightly still dies --- library/alloc/src/collections/vec_deque/mod.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 9107526f23cf5..95ca394c67fb9 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -158,10 +158,13 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> Default for VecDeque<T, A, COOP_PREFERRED> { +impl<T, const COOP_PREFERRED: bool> Default for VecDeque<T, Global, COOP_PREFERRED> +where + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Creates an empty deque. #[inline] - fn default() -> VecDeque<T, A, COOP_PREFERRED> { + fn default() -> VecDeque<T, Global, COOP_PREFERRED> { VecDeque::new() } } @@ -2978,10 +2981,10 @@ where } #[stable(feature = "std_collections_from_array", since = "1.56.0")] -impl<T, const N: usize, A: Allocator, const COOP_PREFERRED: bool> From<[T; N]> - for VecDeque<T, A, COOP_PREFERRED> +impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]> + for VecDeque<T, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { /// Converts a `[T; N]` into a `VecDeque<T>`. /// From 738d4f4f13aac58169a8f5dba17afc870de28c0a Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Fri, 16 Dec 2022 16:36:22 -0800 Subject: [PATCH 19/47] Tidy --- library/alloc/src/vec/mod.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 3692169f7ae6e..b92d2da5a6693 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -439,7 +439,10 @@ pub type WeVec<T, const weight: u8> = Vec<T, Global, { weight > 127 }>; // Inherent methods //////////////////////////////////////////////////////////////////////////////// -impl<T> Vec<T> { +impl<T, const COOP_PREFERRED: bool> Vec<T, Global, COOP_PREFERRED> +where + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Constructs a new, empty `Vec<T>`. /// /// The vector will not allocate until elements are pushed onto it. @@ -3216,11 +3219,14 @@ where #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] -impl<T> const Default for Vec<T> { +impl<T, const COOP_PREFERRED: bool> const Default for Vec<T, Global, COOP_PREFERRED> +where + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Creates an empty `Vec<T>`. /// /// The vector will not allocate until elements are pushed onto it. - fn default() -> Vec<T> { + fn default() -> Vec<T, Global, COOP_PREFERRED> { Vec::new() } } From adfb4167ef0f466dad54f18c74ac45e0c664b324 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 5 Jan 2023 18:05:07 -0800 Subject: [PATCH 20/47] Working around ICE rust-lang/rust issue #106473. WIP --- library/alloc/src/collections/binary_heap/mod.rs | 2 +- library/alloc/src/ffi/c_str.rs | 2 +- library/alloc/src/vec/in_place_collect.rs | 2 +- library/alloc/src/vec/mod.rs | 12 ++++++------ library/alloc/src/vec/spec_from_iter.rs | 4 ++-- library/alloc/src/vec/spec_from_iter_nested.rs | 4 ++-- library/proc_macro/src/bridge/mod.rs | 4 ++-- library/proc_macro/src/bridge/rpc.rs | 4 ++-- library/proc_macro/src/diagnostic.rs | 2 +- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index f1b0cc4a72424..0a4a52e25c48f 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -1652,7 +1652,7 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> { } #[stable(feature = "binary_heap_extras_15", since = "1.5.0")] -impl<T> From<BinaryHeap<T>> for Vec<T> { +impl<T> From<BinaryHeap<T>> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { /// Converts a `BinaryHeap<T>` into a `Vec<T>`. /// /// This conversion requires no data movement or allocation, and has diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 11bd4c4dc1ba8..5948995f90c13 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -723,7 +723,7 @@ impl fmt::Debug for CString { } #[stable(feature = "cstring_into", since = "1.7.0")] -impl From<CString> for Vec<u8> { +impl From<CString> for Vec<u8, Global, DEFAULT_COOP_PREFERRED> { /// Converts a [`CString`] into a <code>[Vec]<[u8]></code>. /// /// The conversion consumes the [`CString`], and removes the terminating NUL byte. diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs index 87d61deb1eb2f..cdaf3c43e824d 100644 --- a/library/alloc/src/vec/in_place_collect.rs +++ b/library/alloc/src/vec/in_place_collect.rs @@ -150,7 +150,7 @@ pub(super) trait InPlaceIterableMarker {} impl<T> InPlaceIterableMarker for T where T: InPlaceIterable {} -impl<T, I> SpecFromIter<T, I> for Vec<T> +impl<T, I> SpecFromIter<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> where I: Iterator<Item = T> + SourceIter<Source: AsVecIntoIter> + InPlaceIterableMarker, { diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index b92d2da5a6693..74784a09e4edb 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2868,7 +2868,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T> FromIterator<T> for Vec<T> { +impl<T> FromIterator<T> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { #[inline] fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> { <Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter()) @@ -3286,7 +3286,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone> From<&[T]> for Vec<T> { +impl<T: Clone> From<&[T]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { /// Allocate a `Vec<T>` and fill it by cloning `s`'s items. /// /// # Examples @@ -3306,7 +3306,7 @@ impl<T: Clone> From<&[T]> for Vec<T> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_from_mut", since = "1.19.0")] -impl<T: Clone> From<&mut [T]> for Vec<T> { +impl<T: Clone> From<&mut [T]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { /// Allocate a `Vec<T>` and fill it by cloning `s`'s items. /// /// # Examples @@ -3326,7 +3326,7 @@ impl<T: Clone> From<&mut [T]> for Vec<T> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_from_array", since = "1.44.0")] -impl<T, const N: usize> From<[T; N]> for Vec<T> { +impl<T, const N: usize> From<[T; N]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { /// Allocate a `Vec<T>` and move `s`'s items into it. /// /// # Examples @@ -3349,7 +3349,7 @@ impl<T, const N: usize> From<[T; N]> for Vec<T> { } #[stable(feature = "vec_from_cow_slice", since = "1.14.0")] -impl<'a, T> From<Cow<'a, [T]>> for Vec<T> +impl<'a, T> From<Cow<'a, [T]>> for Vec<T, Global, DEFAULT_COOP_PREFERRED> where [T]: ToOwned<Owned = Vec<T>>, { @@ -3426,7 +3426,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl From<&str> for Vec<u8> { +impl From<&str> for Vec<u8, Global, DEFAULT_COOP_PREFERRED> { /// Allocate a `Vec<u8>` and fill it with a UTF-8 string. /// /// # Examples diff --git a/library/alloc/src/vec/spec_from_iter.rs b/library/alloc/src/vec/spec_from_iter.rs index efa6868473e49..7db120f1ff6c2 100644 --- a/library/alloc/src/vec/spec_from_iter.rs +++ b/library/alloc/src/vec/spec_from_iter.rs @@ -25,7 +25,7 @@ pub(super) trait SpecFromIter<T, I> { fn from_iter(iter: I) -> Self; } -impl<T, I> SpecFromIter<T, I> for Vec<T> +impl<T, I> SpecFromIter<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> where I: Iterator<Item = T>, { @@ -34,7 +34,7 @@ where } } -impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T> { +impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { fn from_iter(iterator: IntoIter<T>) -> Self { // A common case is passing a vector into a function which immediately // re-collects into a vector. We can short circuit this if the IntoIter diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index f915ebb86e5a5..9453f2dcd9828 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -13,7 +13,7 @@ pub(super) trait SpecFromIterNested<T, I> { fn from_iter(iter: I) -> Self; } -impl<T, I> SpecFromIterNested<T, I> for Vec<T> +impl<T, I> SpecFromIterNested<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> where I: Iterator<Item = T>, { @@ -45,7 +45,7 @@ where } } -impl<T, I> SpecFromIterNested<T, I> for Vec<T> +impl<T, I> SpecFromIterNested<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> where I: TrustedLen<Item = T>, { diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 4c1e196b5ad16..aae141dc2a118 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -252,14 +252,14 @@ impl<'a, T, M> Unmark for &'a mut Marked<T, M> { } } -impl<T: Mark> Mark for Vec<T> { +impl<T: Mark> Mark for Vec<T, Global, DEFAULT_COOP_PREFERRED> { type Unmarked = Vec<T::Unmarked>; fn mark(unmarked: Self::Unmarked) -> Self { // Should be a no-op due to std's in-place collect optimizations. unmarked.into_iter().map(T::mark).collect() } } -impl<T: Unmark> Unmark for Vec<T> { +impl<T: Unmark> Unmark for Vec<T, Global, DEFAULT_COOP_PREFERRED> { type Unmarked = Vec<T::Unmarked>; fn unmark(self) -> Self::Unmarked { // Should be a no-op due to std's in-place collect optimizations. diff --git a/library/proc_macro/src/bridge/rpc.rs b/library/proc_macro/src/bridge/rpc.rs index e9d7a46c06f6d..c50eb9c620626 100644 --- a/library/proc_macro/src/bridge/rpc.rs +++ b/library/proc_macro/src/bridge/rpc.rs @@ -225,7 +225,7 @@ impl<S> DecodeMut<'_, '_, S> for String { } } -impl<S, T: Encode<S>> Encode<S> for Vec<T> { +impl<S, T: Encode<S>> Encode<S> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { fn encode(self, w: &mut Writer, s: &mut S) { self.len().encode(w, s); for x in self { @@ -234,7 +234,7 @@ impl<S, T: Encode<S>> Encode<S> for Vec<T> { } } -impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T> { +impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { let len = usize::decode(r, s); let mut vec = Vec::with_capacity(len); diff --git a/library/proc_macro/src/diagnostic.rs b/library/proc_macro/src/diagnostic.rs index 5a209f7c7aa18..41a11b1003b84 100644 --- a/library/proc_macro/src/diagnostic.rs +++ b/library/proc_macro/src/diagnostic.rs @@ -30,7 +30,7 @@ impl MultiSpan for Span { } #[unstable(feature = "proc_macro_diagnostic", issue = "54140")] -impl MultiSpan for Vec<Span> { +impl MultiSpan for Vec<Span, Global, DEFAULT_COOP_PREFERRED> { fn into_spans(self) -> Vec<Span> { self } From ea538d23750b75a5ec2522ad7c8864e2b21240da Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Fri, 6 Jan 2023 17:03:38 -0800 Subject: [PATCH 21/47] INTERIM: Macros instead of const for global COOP defaults. --- library/alloc/src/boxed.rs | 7 ++-- .../alloc/src/collections/binary_heap/mod.rs | 9 +++-- .../alloc/src/collections/vec_deque/drain.rs | 3 +- .../alloc/src/collections/vec_deque/mod.rs | 5 +-- library/alloc/src/ffi/c_str.rs | 5 ++- library/alloc/src/lib.rs | 1 + library/alloc/src/raw_vec.rs | 5 +-- library/alloc/src/string.rs | 11 +++--- library/alloc/src/vec/drain.rs | 3 +- library/alloc/src/vec/in_place_collect.rs | 6 +++- library/alloc/src/vec/into_iter.rs | 3 +- library/alloc/src/vec/mod.rs | 36 ++++++++++++------- library/alloc/src/vec/spec_from_iter.rs | 8 +++-- .../alloc/src/vec/spec_from_iter_nested.rs | 8 +++-- library/core/src/alloc/mod.rs | 6 +++- 15 files changed, 80 insertions(+), 36 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 615744a2d60a3..86e9c96c5f42f 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -146,7 +146,6 @@ #![stable(feature = "rust1", since = "1.0.0")] -use core::alloc; use core::any::Any; use core::async_iter::AsyncIterator; use core::borrow; @@ -776,9 +775,10 @@ where #[unstable(feature = "allocator_api", issue = "32838")] // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] + #[allow(unused_braces)] pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { unsafe { - RawVec::<T, A, { alloc::SHORT_TERM_VEC_PREFERS_COOP }>::with_capacity_in(len, alloc) + RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_in(len, alloc) .into_box(len) } } @@ -807,9 +807,10 @@ where #[unstable(feature = "allocator_api", issue = "32838")] // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] + #[allow(unused_braces)] pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { unsafe { - RawVec::<T, A, { alloc::SHORT_TERM_VEC_PREFERS_COOP }>::with_capacity_zeroed_in( + RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_zeroed_in( len, alloc, ) .into_box(len) diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index 0a4a52e25c48f..bc08a604b8cd0 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -148,13 +148,14 @@ use core::mem::{self, swap, ManuallyDrop}; use core::num::NonZeroUsize; use core::ops::{Deref, DerefMut}; use core::ptr; -use core::{alloc, fmt}; +use core::fmt; use crate::alloc::Global; use crate::collections::TryReserveError; use crate::slice; use crate::vec::{self, AsVecIntoIter, Vec}; +use crate::DEFAULT_COOP_PREFERRED; use super::SpecExtend; @@ -1243,7 +1244,8 @@ impl<T> BinaryHeap<T> { /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] - pub fn drain(&mut self) -> Drain<'_, T, { alloc::SHORT_TERM_VEC_PREFERS_COOP }> { + #[allow(unused_braces)] + pub fn drain(&mut self) -> Drain<'_, T, { SHORT_TERM_VEC_PREFERS_COOP!() }> { Drain { iter: self.data.drain(..) } } @@ -1652,7 +1654,8 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> { } #[stable(feature = "binary_heap_extras_15", since = "1.5.0")] -impl<T> From<BinaryHeap<T>> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl<T> From<BinaryHeap<T>> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { /// Converts a `BinaryHeap<T>` into a `Vec<T>`. /// /// This conversion requires no data movement or allocation, and has diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index 2004060c4bb96..1bfeaf5a5d3fc 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -15,11 +15,12 @@ use super::VecDeque; /// /// [`drain`]: VecDeque::drain #[stable(feature = "drain", since = "1.6.0")] +#[allow(unused_braces)] pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, + const COOP_PREFERRED: bool = { SHORT_TERM_VEC_PREFERS_COOP!() }, > where [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 95ca394c67fb9..93ae1f7d1b6ce 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -7,7 +7,7 @@ #![feature(global_co_alloc)] #![stable(feature = "rust1", since = "1.0.0")] -use crate::vec::DEFAULT_COOP_PREFERRED; +use crate::DEFAULT_COOP_PREFERRED; use core::alloc; use core::cmp::{self, Ordering}; use core::fmt; @@ -93,10 +93,11 @@ mod tests; #[cfg_attr(not(test), rustc_diagnostic_item = "VecDeque")] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_insignificant_dtor] +#[allow(unused_braces)] pub struct VecDeque< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, + const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}, > where [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 5948995f90c13..dd2e6baab2776 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -17,6 +17,8 @@ use core::ptr; use core::slice; use core::slice::memchr; use core::str::{self, Utf8Error}; +use crate::Global; +use crate::DEFAULT_COOP_PREFERRED; #[cfg(target_has_atomic = "ptr")] use crate::sync::Arc; @@ -723,7 +725,8 @@ impl fmt::Debug for CString { } #[stable(feature = "cstring_into", since = "1.7.0")] -impl From<CString> for Vec<u8, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl From<CString> for Vec<u8, Global, {DEFAULT_COOP_PREFERRED!()}> { /// Converts a [`CString`] into a <code>[Vec]<[u8]></code>. /// /// The conversion consumes the [`CString`], and removes the terminating NUL byte. diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 9400be19d6130..026e23e19b34a 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -185,6 +185,7 @@ #![feature(global_co_alloc_short_term_pref)] #![feature(hashmap_internals)] #![feature(lang_items)] +#![feature(global_co_alloc_def)] // When we used min_specialization instead of specialization, library/alloc/src/vec/mod.rs was failing with: // - cannot specialize on predicate `the constant `core::alloc::co_alloc_metadata_num_slots::<A>()` can be evaluated` // - cannot specialize on predicate `[(); _] well-formed` diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index f74e876b6cb9c..6a861b80c78f4 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -14,7 +14,7 @@ use crate::alloc::{Allocator, Global, Layout}; use crate::boxed::Box; use crate::collections::TryReserveError; use crate::collections::TryReserveErrorKind::*; -use crate::vec::DEFAULT_COOP_PREFERRED; +use crate::DEFAULT_COOP_PREFERRED; #[cfg(test)] mod tests; @@ -50,10 +50,11 @@ enum AllocInit { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] +#[allow(unused_braces)] pub(crate) struct RawVec< T, A: Allocator = Global, - const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, + const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}, > where [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 99253153f97f6..c899da6d4937a 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -75,7 +75,7 @@ use crate::str::{self, from_utf8_unchecked_mut, Chars, Utf8Error}; #[cfg(not(no_global_oom_handling))] use crate::str::{from_boxed_utf8_unchecked, FromStr}; use crate::vec::Vec; -use crate::vec::DEFAULT_COOP_PREFERRED; +use crate::DEFAULT_COOP_PREFERRED; /// A UTF-8–encoded, growable string. /// @@ -369,7 +369,8 @@ use crate::vec::DEFAULT_COOP_PREFERRED; #[derive(PartialOrd, Eq, Ord)] #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), lang = "String")] -pub struct String<const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +pub struct String<const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { @@ -2639,7 +2640,8 @@ where /// [`Display`]: fmt::Display #[cfg_attr(not(test), rustc_diagnostic_item = "ToString")] #[stable(feature = "rust1", since = "1.0.0")] -pub trait ToString<const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +pub trait ToString<const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { @@ -3088,7 +3090,8 @@ where /// /// [`drain`]: String::drain #[stable(feature = "drain", since = "1.6.0")] -pub struct Drain<'a, const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +pub struct Drain<'a, const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, { diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 2159edc7b1a23..72153029287bd 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -19,11 +19,12 @@ use super::Vec; /// let iter: std::vec::Drain<_> = v.drain(..); /// ``` #[stable(feature = "drain", since = "1.6.0")] +#[allow(unused_braces)] pub struct Drain< 'a, T: 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, - const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, + const COOP_PREFERRED: bool = { SHORT_TERM_VEC_PREFERS_COOP!() }, > where [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs index cdaf3c43e824d..6f3c44a8aa8b6 100644 --- a/library/alloc/src/vec/in_place_collect.rs +++ b/library/alloc/src/vec/in_place_collect.rs @@ -141,6 +141,9 @@ use core::iter::{InPlaceIterable, SourceIter, TrustedRandomAccessNoCoerce}; use core::mem::{self, ManuallyDrop, SizedTypeProperties}; use core::ptr::{self}; +use crate::Global; +use crate::DEFAULT_COOP_PREFERRED; + use super::{InPlaceDrop, InPlaceDstBufDrop, SpecFromIter, SpecFromIterNested, Vec}; /// Specialization marker for collecting an iterator pipeline into a Vec while reusing the @@ -150,7 +153,8 @@ pub(super) trait InPlaceIterableMarker {} impl<T> InPlaceIterableMarker for T where T: InPlaceIterable {} -impl<T, I> SpecFromIter<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +impl<T, I> SpecFromIter<T, I> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> where I: Iterator<Item = T> + SourceIter<Source: AsVecIntoIter> + InPlaceIterableMarker, { diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 98b7a7cda6731..7ca48fba02f06 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -28,10 +28,11 @@ use core::{alloc, array, fmt}; /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_insignificant_dtor] +#[allow(unused_braces)] pub struct IntoIter< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, - const COOP_PREFERRED: bool = { alloc::SHORT_TERM_VEC_PREFERS_COOP }, + const COOP_PREFERRED: bool = { SHORT_TERM_VEC_PREFERS_COOP!() }, > where [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 74784a09e4edb..161adb40fdda5 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -148,6 +148,14 @@ use self::spec_extend::SpecExtend; #[cfg(not(no_global_oom_handling))] mod spec_extend; +/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter. +#[unstable(feature = "global_co_alloc_def", issue = "none")] +// pub const DEFAULT_COOP_PREFERRED: bool = true; +#[macro_export] +macro_rules! DEFAULT_COOP_PREFERRED { + () => {true} +} + /// A contiguous growable array type, written as `Vec<T>`, short for 'vector'. /// /// # Examples @@ -397,11 +405,12 @@ mod spec_extend; #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")] #[rustc_insignificant_dtor] +#[allow(unused_braces)] pub struct Vec< T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, //@FIXME: #[unstable(feature ="global_co_alloc_vec", issue="none")] - const COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED, + const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}, > where [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, { @@ -409,10 +418,6 @@ pub struct Vec< len: usize, } -/// Default `Vec`, `DefVec`, `DecVeque`, `DefDecVeq` "cooperation" (`COOP_PREFERRED`) generic parameter. -#[unstable(feature = "global_co_alloc_def", issue = "none")] -pub const DEFAULT_COOP_PREFERRED: bool = true; - #[unstable(feature = "global_co_alloc_covec", issue = "none")] pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, true>; @@ -426,8 +431,9 @@ pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: All /// difference to `Vec` (used without specifying `COOP_PREFERRED`): `DefVec` indicates that the /// author considered using `CoVec` or `PlVec`, but left it to default instead. #[unstable(feature = "global_co_alloc_defvec", issue = "none")] +#[allow(unused_braces)] pub type DefVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = - Vec<T, A, DEFAULT_COOP_PREFERRED>; + Vec<T, A, {DEFAULT_COOP_PREFERRED!()}>; /// "Weighted cooperative" Vec. Weight means how much it wants to cooperate (with the allocator). 0 /// = always pack; u8::MAX = always cooperate (if `Global` supports it). @@ -2868,7 +2874,8 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T> FromIterator<T> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl<T> FromIterator<T> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { #[inline] fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> { <Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter()) @@ -3286,7 +3293,8 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone> From<&[T]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl<T: Clone> From<&[T]> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { /// Allocate a `Vec<T>` and fill it by cloning `s`'s items. /// /// # Examples @@ -3306,7 +3314,8 @@ impl<T: Clone> From<&[T]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_from_mut", since = "1.19.0")] -impl<T: Clone> From<&mut [T]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl<T: Clone> From<&mut [T]> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { /// Allocate a `Vec<T>` and fill it by cloning `s`'s items. /// /// # Examples @@ -3326,7 +3335,8 @@ impl<T: Clone> From<&mut [T]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_from_array", since = "1.44.0")] -impl<T, const N: usize> From<[T; N]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl<T, const N: usize> From<[T; N]> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { /// Allocate a `Vec<T>` and move `s`'s items into it. /// /// # Examples @@ -3349,7 +3359,8 @@ impl<T, const N: usize> From<[T; N]> for Vec<T, Global, DEFAULT_COOP_PREFERRED> } #[stable(feature = "vec_from_cow_slice", since = "1.14.0")] -impl<'a, T> From<Cow<'a, [T]>> for Vec<T, Global, DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +impl<'a, T> From<Cow<'a, [T]>> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> where [T]: ToOwned<Owned = Vec<T>>, { @@ -3426,7 +3437,8 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl From<&str> for Vec<u8, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl From<&str> for Vec<u8, Global, {DEFAULT_COOP_PREFERRED!()}> { /// Allocate a `Vec<u8>` and fill it with a UTF-8 string. /// /// # Examples diff --git a/library/alloc/src/vec/spec_from_iter.rs b/library/alloc/src/vec/spec_from_iter.rs index 7db120f1ff6c2..06da451fe3932 100644 --- a/library/alloc/src/vec/spec_from_iter.rs +++ b/library/alloc/src/vec/spec_from_iter.rs @@ -1,5 +1,7 @@ use core::mem::ManuallyDrop; use core::ptr::{self}; +use crate::Global; +use crate::DEFAULT_COOP_PREFERRED; use super::{IntoIter, SpecExtend, SpecFromIterNested, Vec}; @@ -25,7 +27,8 @@ pub(super) trait SpecFromIter<T, I> { fn from_iter(iter: I) -> Self; } -impl<T, I> SpecFromIter<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +impl<T, I> SpecFromIter<T, I> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> where I: Iterator<Item = T>, { @@ -34,7 +37,8 @@ where } } -impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T, Global, DEFAULT_COOP_PREFERRED> { +#[allow(unused_braces)] +impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { fn from_iter(iterator: IntoIter<T>) -> Self { // A common case is passing a vector into a function which immediately // re-collects into a vector. We can short circuit this if the IntoIter diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index 9453f2dcd9828..7408155ff870a 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -3,6 +3,8 @@ use core::iter::TrustedLen; use core::ptr; use crate::raw_vec::RawVec; +use crate::Global; +use crate::DEFAULT_COOP_PREFERRED; use super::{SpecExtend, Vec}; @@ -13,7 +15,8 @@ pub(super) trait SpecFromIterNested<T, I> { fn from_iter(iter: I) -> Self; } -impl<T, I> SpecFromIterNested<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +impl<T, I> SpecFromIterNested<T, I> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> where I: Iterator<Item = T>, { @@ -45,7 +48,8 @@ where } } -impl<T, I> SpecFromIterNested<T, I> for Vec<T, Global, DEFAULT_COOP_PREFERRED> +#[allow(unused_braces)] +impl<T, I> SpecFromIterNested<T, I> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> where I: TrustedLen<Item = T>, { diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index c268dd9368bc3..7a88f41873e80 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -82,7 +82,11 @@ pub struct SliceAndMeta { } #[unstable(feature = "global_co_alloc_short_term_pref", issue = "none")] -pub const SHORT_TERM_VEC_PREFERS_COOP: bool = true; +//pub const SHORT_TERM_VEC_PREFERS_COOP: bool = true; +#[macro_export] +macro_rules! SHORT_TERM_VEC_PREFERS_COOP { + () => {true} +} #[unstable(feature = "global_co_alloc_meta", issue = "none")] #[allow(missing_debug_implementations)] From 7d05058e67bf5f004a6801ff2cd05e9e2d333c02 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Fri, 6 Jan 2023 17:18:07 -0800 Subject: [PATCH 22/47] TODO replace co_alloc_metadata_num_slots_with_preference_global(...) --- library/alloc/src/lib.rs | 3 ++- library/alloc/src/raw_vec.rs | 2 +- library/core/src/alloc/mod.rs | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 026e23e19b34a..a775d995500ee 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -291,5 +291,6 @@ use crate::alloc::Global; /// See also `core::alloc::co_alloc_metadata_num_slots_with_preference`. #[unstable(feature = "global_co_alloc", issue = "none")] pub const fn co_alloc_metadata_num_slots_with_preference_global(coop_preferred: bool) -> usize { - if Global::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } + // FIXME or replace any calls with core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<Global>(COOP_PREFERRED). Then rename co_alloc_metadata_num_slots_with_preference_specific to co_alloc_metadata_num_slots_with_preference. See raw_vec.rs. + if /*Global::IS_CO_ALLOCATOR*/ true && coop_preferred { 1 } else { 0 } } diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 6a861b80c78f4..c431b87ee4538 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -70,7 +70,7 @@ pub(crate) struct RawVec< impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<Global>(COOP_PREFERRED)]:, { /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so /// they cannot call `Self::new()`. diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 7a88f41873e80..815e2ffdfaa4a 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -94,8 +94,9 @@ pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>; #[unstable(feature = "global_co_alloc", issue = "none")] pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { - if true { - panic!("FIXME - consider removing co_alloc_metadata_num_slots()"); + // @FIXME later + if false { + panic!("FIXME - consider replacing co_alloc_metadata_num_slots() with co_alloc_metadata_num_slots_with_preference_specific(bool), and adding const flags as appropriate."); } if A::IS_CO_ALLOCATOR { 1 } else { 0 } } From 8bf943dd010f27bdd5441544c470b079cea8f059 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Fri, 6 Jan 2023 17:19:15 -0800 Subject: [PATCH 23/47] Hopefully fixed an impl conflict for ToString for String (itself) vs. ToString for Display + ?Sized. --- library/alloc/src/string.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index c899da6d4937a..badce0a83b184 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2195,7 +2195,9 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_str2", since = "1.45.0")] -impl Extend<Box<str>> for String { +impl<const COOP_PREFERRED: bool> Extend<Box<str>> for String<COOP_PREFERRED> +where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); } @@ -2782,7 +2784,10 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "string_to_string_specialization", since = "1.17.0")] -impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for String<COOP_PREFERRED> { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for String<COOP_PREFERRED> +where + [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { self.to_owned() @@ -2870,7 +2875,9 @@ where // note: test pulls in std, which causes errors here #[cfg(not(test))] #[stable(feature = "string_from_box", since = "1.18.0")] -impl From<Box<str>> for String { +impl<const COOP_PREFERRED: bool> From<Box<str>> for String<COOP_PREFERRED> +where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +{ /// Converts the given boxed `str` slice to a [`String`]. /// It is notable that the `str` slice is owned. /// From 4e14196741cf3a93ac0818098780493e586d9a16 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Fri, 6 Jan 2023 17:39:10 -0800 Subject: [PATCH 24/47] Renamed co_alloc_metadata_num_slots_with_preference_specific(...) to co_alloc_metadata_num_slots_with_preference(...) --- .../alloc/src/collections/vec_deque/drain.rs | 24 +++--- .../src/collections/vec_deque/into_iter.rs | 2 +- .../alloc/src/collections/vec_deque/mod.rs | 52 ++++++------ .../src/collections/vec_deque/spec_extend.rs | 12 +-- .../collections/vec_deque/spec_from_iter.rs | 6 +- library/alloc/src/raw_vec.rs | 20 ++--- library/alloc/src/slice.rs | 22 ++--- library/alloc/src/vec/drain.rs | 28 +++---- library/alloc/src/vec/drain_filter.rs | 12 +-- library/alloc/src/vec/into_iter.rs | 36 ++++----- library/alloc/src/vec/mod.rs | 80 +++++++++---------- library/alloc/src/vec/partial_eq.rs | 20 ++--- library/alloc/src/vec/spec_extend.rs | 10 +-- library/alloc/src/vec/spec_from_elem.rs | 8 +- library/core/src/alloc/mod.rs | 2 +- library/std/src/io/cursor.rs | 12 +-- 16 files changed, 173 insertions(+), 173 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index 1bfeaf5a5d3fc..cd0e6caace3f4 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -22,7 +22,7 @@ pub struct Drain< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = { SHORT_TERM_VEC_PREFERS_COOP!() }, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T // and we want it to be covariant instead @@ -40,7 +40,7 @@ pub struct Drain< impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { pub(super) unsafe fn new( deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, @@ -98,7 +98,7 @@ where impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain") @@ -114,33 +114,33 @@ where unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool>( &'r mut Drain<'a, T, A, COOP_PREFERRED>, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:; + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { if self.0.remaining != 0 { @@ -224,7 +224,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -250,7 +250,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<T> { @@ -267,12 +267,12 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "fused", since = "1.26.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { } diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index 1c3bedd21e7e2..7924098c14db2 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -19,7 +19,7 @@ pub struct IntoIter< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = true, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { inner: VecDeque<T, A, COOP_PREFERRED>, } diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 93ae1f7d1b6ce..ffceae18a6cff 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -99,7 +99,7 @@ pub struct VecDeque< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // `self[0]`, if it exists, is `buf[head]`. // `head < buf.capacity()`, unless `buf.capacity() == 0` when `head == 0`. @@ -115,7 +115,7 @@ pub struct VecDeque< impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn clone(&self) -> Self { let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone()); @@ -133,7 +133,7 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { /// Runs the destructor for all items in the slice when it gets dropped (normally or @@ -172,7 +172,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Marginally more convenient #[inline] @@ -464,7 +464,7 @@ where ) -> usize { struct Guard<'a, T, A: Allocator, const COOP_PREFERRED: bool> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { deque: &'a mut VecDeque<T, A, COOP_PREFERRED>, written: usize, @@ -472,7 +472,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for Guard<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { self.deque.len += self.written; @@ -553,7 +553,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Creates an empty deque. /// @@ -592,7 +592,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Creates an empty deque. /// @@ -2630,7 +2630,7 @@ where impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Modifies the deque in-place so that `len()` is equal to new_len, /// either by removing excess elements from the back or by appending clones of `value` @@ -2679,7 +2679,7 @@ fn wrap_index(logical_index: usize, capacity: usize) -> usize { impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> PartialEq for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn eq(&self, other: &Self) -> bool { if self.len != other.len() { @@ -2720,7 +2720,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { } @@ -2735,7 +2735,7 @@ __impl_slice_eq1! { [const N: usize] VecDeque<T, A, COOP_PREFERRED>, &mut [U; N] impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.iter().partial_cmp(other.iter()) @@ -2745,7 +2745,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn cmp(&self, other: &Self) -> Ordering { @@ -2756,7 +2756,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn hash<H: Hasher>(&self, state: &mut H) { state.write_length_prefix(self.len); @@ -2773,7 +2773,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Index<usize> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Output = T; @@ -2786,7 +2786,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IndexMut<usize> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: usize) -> &mut T { @@ -2797,7 +2797,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> { SpecFromIter::spec_from_iter(iter.into_iter()) @@ -2807,7 +2807,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2823,7 +2823,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -2837,7 +2837,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; @@ -2850,7 +2850,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()); @@ -2871,7 +2871,7 @@ where impl<'a, T: 'a + Copy, A: Allocator, const COOP_PREFERRED: bool> Extend<&'a T> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()); @@ -2892,7 +2892,7 @@ where impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() @@ -2903,7 +2903,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Turn a [`Vec<T>`] into a [`VecDeque<T>`]. /// @@ -2916,7 +2916,7 @@ where #[inline] fn from<const OTHER_COOP_PREFERRED: bool>(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>( OTHER_COOP_PREFERRED, )]:, { @@ -2929,7 +2929,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> From<VecDeque<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Turn a [`VecDeque<T>`] into a [`Vec<T>`]. /// diff --git a/library/alloc/src/collections/vec_deque/spec_extend.rs b/library/alloc/src/collections/vec_deque/spec_extend.rs index 973de88ffb172..b1a9d23fd4a99 100644 --- a/library/alloc/src/collections/vec_deque/spec_extend.rs +++ b/library/alloc/src/collections/vec_deque/spec_extend.rs @@ -17,7 +17,7 @@ impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, mut iter: I) { // This function should be the moral equivalent of: @@ -31,7 +31,7 @@ where deque: &mut VecDeque<T, A, COOP_PREFERRED>, element: T, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // SAFETY: Because of the precondition, it's guaranteed that there is space // in the logical array after the last element. @@ -63,7 +63,7 @@ impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iter: I) { // This is the case for a TrustedLen iterator. @@ -99,7 +99,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) { let slice = iterator.as_slice(); @@ -118,7 +118,7 @@ impl<'a, T: 'a, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, I where I: Iterator<Item = &'a T>, T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.copied()) @@ -129,7 +129,7 @@ impl<'a, T: 'a, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<&'a T, slic for VecDeque<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index 086a1fa74566c..85dd756337350 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -11,7 +11,7 @@ impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for VecDeque<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_from_iter(iterator: I) -> Self { // Since converting is O(1) now, just re-use the `Vec` logic for @@ -25,7 +25,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self { @@ -36,7 +36,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn spec_from_iter(iterator: IntoIter<T>) -> Self { diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index c431b87ee4538..cb0cb4216a5a9 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -56,7 +56,7 @@ pub(crate) struct RawVec< A: Allocator = Global, const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { ptr: Unique<T>, cap: usize, @@ -65,12 +65,12 @@ pub(crate) struct RawVec< //pub(crate) meta: [GlobalCoAllocMeta; {if core::any::TypeId::of::<A>()==core::any::TypeId::of::<Global>() {1} else {0}}], //pub(crate) meta: [GlobalCoAllocMeta; mem::size_of::<A::IsCoAllocator>()], pub(crate) metas: [GlobalCoAllocMeta; - alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)], + alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], } impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<Global>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so /// they cannot call `Self::new()`. @@ -120,7 +120,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // Tiny Vecs are dumb. Skip to: // - 8 if the element size is 1, because any heap allocators is likely @@ -144,7 +144,7 @@ where cap: 0, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /* , two: 2, three: 3, four: 4*/}; - alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)], + alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], } } @@ -223,7 +223,7 @@ where cap: capacity, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; - alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )], } @@ -247,7 +247,7 @@ where cap: capacity, alloc, metas: [GlobalCoAllocMeta {/*one: 1*/ /*, two: 2, three: 3, four: 4*/}; - alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)], + alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)], } } @@ -317,7 +317,7 @@ where len: usize, additional: usize, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { handle_reserve(slf.grow_amortized(len, additional)); } @@ -393,7 +393,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> RawVec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Returns if the buffer needs to grow to fulfill the needed extra capacity. /// Mainly used to make inlining reserve-calls possible without inlining `grow`. @@ -515,7 +515,7 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for RawVec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. default fn drop(&mut self) { diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index a4fb52d93b953..e5bf6a59bf6bf 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -99,7 +99,7 @@ pub(crate) mod hack { b: Box<[T], A>, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -117,7 +117,7 @@ pub(crate) mod hack { alloc: A, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -132,7 +132,7 @@ pub(crate) mod hack { ) -> Vec<Self, A, COOP_PREFERRED> where Self: Sized, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:; } @@ -145,13 +145,13 @@ pub(crate) mod hack { alloc: A, ) -> Vec<Self, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -160,7 +160,7 @@ pub(crate) mod hack { } impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'a, T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -200,7 +200,7 @@ pub(crate) mod hack { alloc: A, ) -> Vec<Self, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -484,7 +484,7 @@ impl<T> [T] { ) -> Vec<T, A, COOP_PREFERRED> where T: Clone, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -513,7 +513,7 @@ impl<T> [T] { self: Box<Self, A>, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -832,7 +832,7 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Borrow<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn borrow(&self) -> &[T] { &self[..] @@ -842,7 +842,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> BorrowMut<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn borrow_mut(&mut self) -> &mut [T] { &mut self[..] diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 72153029287bd..05fee754beec8 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -26,7 +26,7 @@ pub struct Drain< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, const COOP_PREFERRED: bool = { SHORT_TERM_VEC_PREFERS_COOP!() }, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Index of tail to preserve pub(super) tail_start: usize, @@ -41,7 +41,7 @@ pub struct Drain< impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() @@ -50,7 +50,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Returns the remaining items of this iterator as a slice. /// @@ -152,7 +152,7 @@ where #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Drain<'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[T] { self.as_slice() @@ -163,21 +163,21 @@ where unsafe impl<T: Sync, A: Sync + Allocator, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] unsafe impl<T: Send, A: Send + Allocator, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -195,7 +195,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<T> { @@ -206,7 +206,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { /// Moves back the un-`Drain`ed elements to restore the original `Vec`. @@ -214,12 +214,12 @@ where &'r mut Drain<'a, T, A, COOP_PREFERRED>, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:; + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { if self.0.tail_len > 0 { @@ -287,7 +287,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn is_empty(&self) -> bool { self.iter.is_empty() @@ -298,12 +298,12 @@ where unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "fused", since = "1.26.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { } diff --git a/library/alloc/src/vec/drain_filter.rs b/library/alloc/src/vec/drain_filter.rs index 29cf29e2532d9..89baafca46729 100644 --- a/library/alloc/src/vec/drain_filter.rs +++ b/library/alloc/src/vec/drain_filter.rs @@ -27,7 +27,7 @@ pub struct DrainFilter< const COOP_PREFERRED: bool = true, > where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { pub(super) vec: &'a mut Vec<T, A, COOP_PREFERRED>, /// The index of the item that will be inspected by the next call to `next`. @@ -49,7 +49,7 @@ pub struct DrainFilter< impl<T, F, A: Allocator, const COOP_PREFERRED: bool> DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Returns a reference to the underlying allocator. #[unstable(feature = "allocator_api", issue = "32838")] @@ -119,7 +119,7 @@ impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Iterator for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -159,13 +159,13 @@ impl<T, F, A: Allocator, const COOP_PREFERRED: bool> Drop for DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { struct BackshiftOnDrop<'a, 'b, T, F, A: Allocator, const COOP_PREFERRED: bool> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { drain: &'b mut DrainFilter<'a, T, F, A, COOP_PREFERRED>, } @@ -174,7 +174,7 @@ where for BackshiftOnDrop<'a, 'b, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 7ca48fba02f06..5afb11ee7ba24 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -34,7 +34,7 @@ pub struct IntoIter< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, const COOP_PREFERRED: bool = { SHORT_TERM_VEC_PREFERS_COOP!() }, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { pub(super) buf: NonNull<T>, pub(super) phantom: PhantomData<T>, @@ -52,7 +52,7 @@ pub struct IntoIter< impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.as_slice()).finish() @@ -61,7 +61,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Returns the remaining items of this iterator as a slice. /// @@ -180,7 +180,7 @@ where #[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[T] { self.as_slice() @@ -191,21 +191,21 @@ where unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -323,7 +323,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<T> { @@ -368,7 +368,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn is_empty(&self) -> bool { self.ptr == self.end @@ -377,7 +377,7 @@ where #[stable(feature = "fused", since = "1.26.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { } @@ -385,7 +385,7 @@ impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedLen for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } @@ -407,7 +407,7 @@ unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> TrustedRandomAccessNoCo for IntoIter<T, A, COOP_PREFERRED> where T: NonDrop, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { const MAY_HAVE_SIDE_EFFECT: bool = false; } @@ -417,7 +417,7 @@ where impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[cfg(not(test))] fn clone(&self) -> Self { @@ -433,18 +433,18 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool>( &'a mut IntoIter<T, A, COOP_PREFERRED>, ) where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:; + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { @@ -476,7 +476,7 @@ where unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> InPlaceIterable for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } @@ -485,7 +485,7 @@ where unsafe impl<T, A: Allocator, const COOP_PREFERRED: bool> SourceIter for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Source = Self; diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 161adb40fdda5..1116cc50e4275 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -412,7 +412,7 @@ pub struct Vec< //@FIXME: #[unstable(feature ="global_co_alloc_vec", issue="none")] const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}, > where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { buf: RawVec<T, A, COOP_PREFERRED>, len: usize, @@ -637,7 +637,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Constructs a new, empty `Vec<T, A>`. /// @@ -1653,7 +1653,7 @@ where // In cases when predicate and `drop` never panick, it will be optimized out. struct BackshiftOnDrop<'a, T, A: Allocator, const VEC_IS_COOP: bool = true> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( VEC_IS_COOP, )]:, { @@ -1665,7 +1665,7 @@ where impl<T, A: Allocator, const VEC_IS_COOP: bool> Drop for BackshiftOnDrop<'_, T, A, VEC_IS_COOP> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( VEC_IS_COOP, )]:, { @@ -1700,7 +1700,7 @@ where g: &mut BackshiftOnDrop<'_, T, A, VEC_IS_COOP>, ) where F: FnMut(&mut T) -> bool, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( VEC_IS_COOP, )]:, { @@ -1797,7 +1797,7 @@ where /* INVARIANT: vec.len() > read >= write > write-1 >= 0 */ struct FillGapOnDrop<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -1815,7 +1815,7 @@ where impl<'a, T, A: core::alloc::Allocator, const COOP_PREFERRED: bool> Drop for FillGapOnDrop<'a, T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>( + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>( COOP_PREFERRED, )]:, { @@ -2413,7 +2413,7 @@ where impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. /// @@ -2515,7 +2515,7 @@ where impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> Vec<[T; N], A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`. /// @@ -2579,7 +2579,7 @@ impl<T: Clone> ExtendWith<T> for ExtendElement<T> { impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[cfg(not(no_global_oom_handling))] /// Extend the vector by `n` values, using the given generator. @@ -2614,7 +2614,7 @@ where impl<T: PartialEq, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Removes consecutive repeated elements in the vector according to the /// [`PartialEq`] trait implementation. @@ -2657,7 +2657,7 @@ pub fn from_elem_in<T: Clone, A: Allocator, const COOP_PREFERRED: bool>( alloc: A, ) -> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { <T as SpecFromElem>::from_elem(elem, n, alloc) } @@ -2673,7 +2673,7 @@ trait ExtendFromWithinSpec { impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { // SAFETY: @@ -2696,7 +2696,7 @@ where impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> ExtendFromWithinSpec for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) { let count = src.len(); @@ -2732,7 +2732,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::Deref for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Target = [T]; @@ -2745,7 +2745,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> ops::DerefMut for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn deref_mut(&mut self) -> &mut [T] { @@ -2761,7 +2761,7 @@ trait SpecCloneFrom { #[cfg(not(no_global_oom_handling))] impl<T: Clone, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn clone_from(this: &mut Self, other: &Self) { // drop anything that will not be overwritten @@ -2780,7 +2780,7 @@ where #[cfg(not(no_global_oom_handling))] impl<T: Copy, A: Allocator, const COOP_PREFERRED: bool> SpecCloneFrom for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn clone_from(this: &mut Self, other: &Self) { this.clear(); @@ -2792,7 +2792,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Clone, A: Allocator + Clone, const COOP_PREFERRED: bool> Clone for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[cfg(not(test))] fn clone(&self) -> Self { @@ -2830,7 +2830,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Hash, A: Allocator, const COOP_PREFERRED: bool> Hash for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { @@ -2846,7 +2846,7 @@ where impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> Index<I> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Output = I::Output; @@ -2864,7 +2864,7 @@ where impl<T, I: SliceIndex<[T]>, A: Allocator, const COOP_PREFERRED: bool> IndexMut<I> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { @@ -2885,7 +2885,7 @@ impl<T> FromIterator<T> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; type IntoIter = IntoIter<T, A>; @@ -2933,7 +2933,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; @@ -2947,7 +2947,7 @@ where impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for &'a mut Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; @@ -2961,7 +2961,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> Extend<T> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { @@ -2981,7 +2981,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // leaf method to which various SpecFrom/SpecExtend implementations delegate when // they have no further optimizations to apply @@ -3159,7 +3159,7 @@ where impl<'a, T: Copy + 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> Extend<&'a T> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { self.spec_extend(iter.into_iter()) @@ -3181,7 +3181,7 @@ where impl<T: PartialOrd, A: Allocator, const COOP_PREFERRED: bool> PartialOrd for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { @@ -3191,7 +3191,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { } @@ -3199,7 +3199,7 @@ impl<T: Eq, A: Allocator, const COOP_PREFERRED: bool> Eq for Vec<T, A, COOP_PREF #[stable(feature = "rust1", since = "1.0.0")] impl<T: Ord, A: Allocator, const COOP_PREFERRED: bool> Ord for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn cmp(&self, other: &Self) -> Ordering { @@ -3211,7 +3211,7 @@ where unsafe impl<#[may_dangle] T, A: Allocator, const COOP_PREFERRED: bool> Drop for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { @@ -3242,7 +3242,7 @@ where impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) @@ -3253,7 +3253,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &Vec<T, A, COOP_PREFERRED> { self @@ -3264,7 +3264,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn as_mut(&mut self) -> &mut Vec<T, A, COOP_PREFERRED> { self @@ -3274,7 +3274,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[T] { self @@ -3284,7 +3284,7 @@ where #[stable(feature = "vec_as_mut", since = "1.5.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<[T]> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn as_mut(&mut self) -> &mut [T] { self @@ -3388,7 +3388,7 @@ where #[stable(feature = "vec_from_box", since = "1.18.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Box<[T], A>> for Vec<T, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Convert a boxed slice into a vector by transferring ownership of /// the existing heap allocation. @@ -3410,7 +3410,7 @@ where #[stable(feature = "box_from_vec", since = "1.20.0")] impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> for Box<[T], A> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Convert a vector into a boxed slice. /// @@ -3455,7 +3455,7 @@ impl From<&str> for Vec<u8, Global, {DEFAULT_COOP_PREFERRED!()}> { impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, A, COOP_PREFERRED>> for [T; N] where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Error = Vec<T, A, COOP_PREFERRED>; diff --git a/library/alloc/src/vec/partial_eq.rs b/library/alloc/src/vec/partial_eq.rs index 7b364e8d4bacd..bcf52b7333218 100644 --- a/library/alloc/src/vec/partial_eq.rs +++ b/library/alloc/src/vec/partial_eq.rs @@ -21,21 +21,21 @@ macro_rules! __impl_slice_eq1 { } } -__impl_slice_eq1! { [A1: Allocator, A2: Allocator, const COOP_PREFERRED1: bool, const COOP_PREFERRED2: bool] Vec<T, A1, COOP_PREFERRED1>, Vec<U, A2, COOP_PREFERRED2>, #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A1>(COOP_PREFERRED1)]:, [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A2>(COOP_PREFERRED2)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &[U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &[T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &mut [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, [U], #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A1: Allocator, A2: Allocator, const COOP_PREFERRED1: bool, const COOP_PREFERRED2: bool] Vec<T, A1, COOP_PREFERRED1>, Vec<U, A2, COOP_PREFERRED2>, #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A1>(COOP_PREFERRED1)]:, [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A2>(COOP_PREFERRED2)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &[U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &[T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] &mut [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Vec<T, A, COOP_PREFERRED>, [U], #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] [T], Vec<U, A, COOP_PREFERRED>, #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } #[cfg(not(no_global_oom_handling))] -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Cow<'_, [T]>, Vec<U, A, COOP_PREFERRED>, #[stable(feature = "rust1", since = "1.0.0")], T: Clone, [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool] Cow<'_, [T]>, Vec<U, A, COOP_PREFERRED>, #[stable(feature = "rust1", since = "1.0.0")], T: Clone, [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } #[cfg(not(no_global_oom_handling))] __impl_slice_eq1! { [] Cow<'_, [T]>, &[U], #[stable(feature = "rust1", since = "1.0.0")], T: Clone } #[cfg(not(no_global_oom_handling))] __impl_slice_eq1! { [] Cow<'_, [T]>, &mut [U], #[stable(feature = "rust1", since = "1.0.0")], T: Clone } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, [U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } -__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, &[U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, [U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } +__impl_slice_eq1! { [A: Allocator, const COOP_PREFERRED: bool, const N: usize] Vec<T, A, COOP_PREFERRED>, &[U; N], #[stable(feature = "rust1", since = "1.0.0")], [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: } // NOTE: some less important impls are omitted to reduce code bloat // FIXME(Centril): Reconsider this? diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index ea91491f62558..73cf325889290 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -13,7 +13,7 @@ pub(super) trait SpecExtend<T, I> { impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iter: I) { self.extend_desugared(iter) @@ -23,7 +23,7 @@ where impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED> where I: TrustedLen<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.extend_trusted(iterator) @@ -33,7 +33,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, IntoIter<T>> for Vec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, mut iterator: IntoIter<T>) { unsafe { @@ -48,7 +48,7 @@ impl<'a, T: 'a, I, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a where I: Iterator<Item = &'a T>, T: Clone, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.cloned()) @@ -59,7 +59,7 @@ impl<'a, T: 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, for Vec<T, A, COOP_PREFERRED> where T: Copy, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { let slice = iterator.as_slice(); diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index 4c29f6184c149..3766f6de061bb 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -24,7 +24,7 @@ impl<T: Clone> SpecFromElem for T { alloc: A, ) -> Vec<Self, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { let mut v = Vec::with_capacity_in(n, alloc); v.extend_with(n, ExtendElement(elem)); @@ -40,7 +40,7 @@ impl<T: Clone + IsZero> SpecFromElem for T { alloc: A, ) -> Vec<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { if elem.is_zero() { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; @@ -59,7 +59,7 @@ impl SpecFromElem for i8 { alloc: A, ) -> Vec<i8, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; @@ -81,7 +81,7 @@ impl SpecFromElem for u8 { alloc: A, ) -> Vec<u8, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 815e2ffdfaa4a..01eefe94565df 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -104,7 +104,7 @@ pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { #[unstable(feature = "global_co_alloc", issue = "none")] /// Param `coop_preferred` - if false, then this returns `0`, regardless of /// whether allocator `A` is cooperative. -pub const fn co_alloc_metadata_num_slots_with_preference_specific<A: Allocator>( +pub const fn co_alloc_metadata_num_slots_with_preference<A: Allocator>( coop_preferred: bool, ) -> usize { if A::IS_CO_ALLOCATOR && coop_preferred { 1 } else { 0 } diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index f8d2ca1d3e74c..63045ebcb8157 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -404,7 +404,7 @@ fn reserve_and_pad<A: Allocator, const COOP_PREFERRED: bool>( buf_len: usize, ) -> io::Result<usize> where - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { let pos: usize = (*pos_mut).try_into().map_err(|_| { io::const_io_error!( @@ -451,7 +451,7 @@ unsafe fn vec_write_unchecked<A, const COOP_PREFERRED: bool>( ) -> usize where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { debug_assert!(vec.capacity() >= pos + buf.len()); vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()); @@ -474,7 +474,7 @@ fn vec_write<A, const COOP_PREFERRED: bool>( ) -> io::Result<usize> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { let buf_len = buf.len(); let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?; @@ -510,7 +510,7 @@ fn vec_write_vectored<A, const COOP_PREFERRED: bool>( ) -> io::Result<usize> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { // For safety reasons, we don't want this sum to overflow ever. // If this saturates, the reserve should panic to avoid any unsound writing. @@ -561,7 +561,7 @@ impl Write for Cursor<&mut [u8]> { impl<A, const COOP_PREFERRED: bool> Write for Cursor<&mut Vec<u8, A, COOP_PREFERRED>> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, self.inner, buf) @@ -586,7 +586,7 @@ where impl<A, const COOP_PREFERRED: bool> Write for Cursor<Vec<u8, A, COOP_PREFERRED>> where A: Allocator, - [(); alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { vec_write(&mut self.pos, &mut self.inner, buf) From 20b1238c9d6abf5ad901c723470591cbfe609a50 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sat, 7 Jan 2023 21:10:52 -0800 Subject: [PATCH 25/47] Fixing COOP_PREFERRED (and similar). WIP. --- library/alloc/src/boxed.rs | 8 +- .../alloc/src/collections/binary_heap/mod.rs | 19 ++- .../src/collections/vec_deque/into_iter.rs | 28 ++-- .../alloc/src/collections/vec_deque/macros.rs | 2 +- .../alloc/src/collections/vec_deque/mod.rs | 38 +++-- library/alloc/src/lib.rs | 2 +- library/alloc/src/rc.rs | 4 +- library/alloc/src/str.rs | 2 +- library/alloc/src/string.rs | 157 +++++++++--------- library/alloc/src/vec/drain.rs | 2 +- library/alloc/src/vec/in_place_drop.rs | 3 +- library/alloc/src/vec/into_iter.rs | 3 +- library/alloc/src/vec/mod.rs | 20 ++- library/alloc/src/vec/spec_from_elem.rs | 2 +- library/alloc/src/vec/splice.rs | 24 +-- library/core/src/alloc/mod.rs | 2 +- 16 files changed, 174 insertions(+), 142 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 86e9c96c5f42f..9d455b0568364 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -641,7 +641,8 @@ impl<T> Box<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> { - unsafe { RawVec::with_capacity(len).into_box(len) } + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + unsafe { RawVec::<T, Global, false>::with_capacity(len).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents, with the memory @@ -666,7 +667,8 @@ impl<T> Box<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> { - unsafe { RawVec::with_capacity_zeroed(len).into_box(len) } + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + unsafe { RawVec::<T, Global, false>::with_capacity_zeroed(len).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents. Returns an error if @@ -1687,7 +1689,7 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> { impl<T, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, Global, COOP_PREFERRED>> for Box<[T; N]> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Error = Vec<T, Global, COOP_PREFERRED>; diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index bc08a604b8cd0..1290a449facd2 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -1527,7 +1527,7 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {} #[derive(Debug)] pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { iter: vec::Drain<'a, T, Global, COOP_PREFERRED>, } @@ -1535,7 +1535,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Item = T; @@ -1551,7 +1551,10 @@ where } #[stable(feature = "drain", since = "1.6.0")] -impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED> { +impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[inline] fn next_back(&mut self) -> Option<T> { self.iter.next_back() @@ -1559,14 +1562,20 @@ impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PR } #[stable(feature = "drain", since = "1.6.0")] -impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED> { +impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ fn is_empty(&self) -> bool { self.iter.is_empty() } } #[stable(feature = "fused", since = "1.26.0")] -impl<T, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, COOP_PREFERRED> {} +impl<T, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{} /// A draining iterator over the elements of a `BinaryHeap`. /// diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index 7924098c14db2..38aba07585a90 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -26,7 +26,7 @@ pub struct IntoIter< impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { pub(super) fn new(inner: VecDeque<T, A, COOP_PREFERRED>) -> Self { IntoIter { inner } @@ -38,9 +38,9 @@ where } #[stable(feature = "collection_debug", since = "1.17.0")] -impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> +impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoIter").field(&self.inner).finish() @@ -48,9 +48,9 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> Iterator for IntoIter<T, A> +impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; @@ -67,9 +67,9 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> +impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<T> { @@ -78,9 +78,9 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> +impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for IntoIter<T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn is_empty(&self) -> bool { self.inner.is_empty() @@ -88,13 +88,15 @@ where } #[stable(feature = "fused", since = "1.26.0")] -impl<T, A: Allocator> FusedIterator for IntoIter<T, A> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]: +impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } #[unstable(feature = "trusted_len", issue = "37572")] -unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]: +unsafe impl<T, A: Allocator, const COOP_PREFERRED : bool> TrustedLen for IntoIter<T, A, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { } diff --git a/library/alloc/src/collections/vec_deque/macros.rs b/library/alloc/src/collections/vec_deque/macros.rs index 77885e6811a3b..68ee43152b5b5 100644 --- a/library/alloc/src/collections/vec_deque/macros.rs +++ b/library/alloc/src/collections/vec_deque/macros.rs @@ -4,7 +4,7 @@ macro_rules! __impl_slice_eq1 { impl<T, U, A: Allocator, const COOP_PREFERRED: bool, $($vars)*> PartialEq<$rhs> for $lhs where T: PartialEq<U>, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, $($constraints)* { fn eq(&self, other: &$rhs) -> bool { diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index ffceae18a6cff..073bac85894d9 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -161,7 +161,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T, const COOP_PREFERRED: bool> Default for VecDeque<T, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Creates an empty deque. #[inline] @@ -568,7 +568,9 @@ where #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_vec_deque_new", since = "CURRENT_RUSTC_VERSION")] #[must_use] - pub const fn new() -> VecDeque<T, Global, COOP_PREFERRED> { + pub const fn new() -> VecDeque<T, Global, COOP_PREFERRED> + where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + { // FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable. VecDeque { head: 0, len: 0, buf: RawVec::NEW } } @@ -1403,6 +1405,7 @@ where pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A> where R: RangeBounds<usize>, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, { // Memory safety // @@ -2799,7 +2802,7 @@ impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { - fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> { + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, A, COOP_PREFERRED> { SpecFromIter::spec_from_iter(iter.into_iter()) } } @@ -2810,11 +2813,11 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = T; - type IntoIter = IntoIter<T, A>; + type IntoIter = IntoIter<T, A, COOP_PREFERRED>; /// Consumes the deque into a front-to-back iterator yielding elements by /// value. - fn into_iter(self) -> IntoIter<T, A> { + fn into_iter(self) -> IntoIter<T, A, COOP_PREFERRED> { IntoIter::new(self) } } @@ -2900,10 +2903,13 @@ where } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>> +impl<T, A: Allocator, const COOP_PREFERRED: bool, const OTHER_COOP_PREFERRED: bool> From<Vec<T, A, OTHER_COOP_PREFERRED>> for VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>( + OTHER_COOP_PREFERRED, + )]:, { /// Turn a [`Vec<T>`] into a [`VecDeque<T>`]. /// @@ -2914,11 +2920,7 @@ where /// and to not re-allocate the `Vec`'s buffer or allocate /// any additional memory. #[inline] - fn from<const OTHER_COOP_PREFERRED: bool>(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self - where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>( - OTHER_COOP_PREFERRED, - )]:, + fn from(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self { let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } @@ -2926,10 +2928,11 @@ where } #[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> From<VecDeque<T, A, COOP_PREFERRED>> +impl<T, A: Allocator, const COOP_PREFERRED: bool, const VECDEQUE_COOP_PREFERRED: bool> From<VecDeque<T, A, VECDEQUE_COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(VECDEQUE_COOP_PREFERRED)]:, { /// Turn a [`VecDeque<T>`] into a [`Vec<T>`]. /// @@ -2960,9 +2963,12 @@ where /// assert_eq!(vec, [8, 9, 1, 2, 3, 4]); /// assert_eq!(vec.as_ptr(), ptr); /// ``` - fn from<const _VECDEQUE_COOP_PREFERRED: bool>( - mut other: VecDeque<T, A, _VECDEQUE_COOP_PREFERRED>, - ) -> Self { + fn from( + mut other: VecDeque<T, A, VECDEQUE_COOP_PREFERRED>, + ) -> Self + where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(VECDEQUE_COOP_PREFERRED)]:, + { other.make_contiguous(); unsafe { @@ -2985,7 +2991,7 @@ where impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]> for VecDeque<T, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts a `[T; N]` into a `VecDeque<T>`. /// diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index a775d995500ee..c8035bd295f58 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -291,6 +291,6 @@ use crate::alloc::Global; /// See also `core::alloc::co_alloc_metadata_num_slots_with_preference`. #[unstable(feature = "global_co_alloc", issue = "none")] pub const fn co_alloc_metadata_num_slots_with_preference_global(coop_preferred: bool) -> usize { - // FIXME or replace any calls with core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<Global>(COOP_PREFERRED). Then rename co_alloc_metadata_num_slots_with_preference_specific to co_alloc_metadata_num_slots_with_preference. See raw_vec.rs. + // FIXME or replace any calls with core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED). See raw_vec.rs. if /*Global::IS_CO_ALLOCATOR*/ true && coop_preferred { 1 } else { 0 } } diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index aeffaf7afbcd5..e4bf9d5fe2fba 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1989,7 +1989,7 @@ impl<T: ?Sized> From<Box<T>> for Rc<T> { #[stable(feature = "shared_from_slice", since = "1.21.0")] impl<T, const COOP_PREFERRED: bool> From<Vec<T, Global, COOP_PREFERRED>> for Rc<[T]> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Allocate a reference-counted slice and move `v`'s items into it. /// @@ -2004,7 +2004,7 @@ where #[inline] fn from(mut v: Vec<T, Global, COOP_PREFERRED>) -> Rc<[T]> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { unsafe { let rc = Rc::copy_from_slice(&v); diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 93cdeae87edc1..1a1a344f2fed3 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -136,7 +136,7 @@ where T: Copy, B: AsRef<[T]> + ?Sized, S: Borrow<B>, - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { let sep_len = sep.len(); let mut iter = slice.iter(); diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index badce0a83b184..448316cb4feed 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -372,7 +372,7 @@ use crate::DEFAULT_COOP_PREFERRED; #[allow(unused_braces)] pub struct String<const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { vec: Vec<u8, Global, COOP_PREFERRED>, } @@ -440,7 +440,7 @@ pub struct FromUtf16Error(()); impl<const COOP_PREFERRED: bool> String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Creates a new empty `String`. /// @@ -521,7 +521,7 @@ where #[cfg(test)] pub fn from_str(_: &str) -> String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { panic!("not available with cfg(test)"); } @@ -886,7 +886,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] pub fn into_bytes(self) -> Vec<u8, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { self.vec } @@ -1636,7 +1636,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { &mut self.vec } @@ -1713,7 +1713,7 @@ where #[must_use = "use `.truncate()` if you don't need the other half"] pub fn split_off(&mut self, at: usize) -> String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { assert!(self.is_char_boundary(at)); let other = self.vec.split_off(at); @@ -1953,7 +1953,7 @@ impl FromUtf8Error { #[stable(feature = "rust1", since = "1.0.0")] pub fn into_bytes<const COOP_PREFERRED: bool>(self) -> Vec<u8, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { self.bytes } @@ -2022,7 +2022,7 @@ impl Error for FromUtf16Error { #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> Clone for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn clone(&self) -> Self { String { vec: self.vec.clone() } @@ -2037,7 +2037,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> FromIterator<char> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2050,7 +2050,7 @@ where #[stable(feature = "string_from_iter_by_ref", since = "1.17.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<&'a char> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2063,7 +2063,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<&'a str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2076,7 +2076,7 @@ where #[stable(feature = "extend_string", since = "1.4.0")] impl<const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>( iter: I, @@ -2100,7 +2100,7 @@ where #[stable(feature = "box_str2", since = "1.45.0")] impl<const COOP_PREFERRED: bool> FromIterator<Box<str>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String<COOP_PREFERRED> { let mut buf = String::new(); @@ -2113,7 +2113,7 @@ where #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<Cow<'a, str>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String<COOP_PREFERRED> { let mut iterator = iter.into_iter(); @@ -2136,7 +2136,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> Extend<char> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) { let iterator = iter.into_iter(); @@ -2160,7 +2160,7 @@ where #[stable(feature = "extend_ref", since = "1.2.0")] impl<'a, const COOP_PREFERRED: bool> Extend<&'a char> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) { self.extend(iter.into_iter().cloned()); @@ -2181,7 +2181,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<'a, const COOP_PREFERRED: bool> Extend<&'a str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(s)); @@ -2196,7 +2196,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_str2", since = "1.45.0")] impl<const COOP_PREFERRED: bool> Extend<Box<str>> for String<COOP_PREFERRED> -where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); @@ -2207,7 +2207,7 @@ where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFER #[stable(feature = "extend_string", since = "1.4.0")] impl<const COOP_PREFERRED: bool> Extend<String> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); @@ -2223,7 +2223,7 @@ where #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a, const COOP_PREFERRED: bool> Extend<Cow<'a, str>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); @@ -2249,7 +2249,7 @@ where )] impl<'a, 'b, const COOP_PREFERRED: bool> Pattern<'a> for &'b String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Searcher = <&'b str as Pattern<'a>>::Searcher; @@ -2286,7 +2286,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> PartialEq for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn eq(&self, other: &String<COOP_PREFERRED>) -> bool { @@ -2342,7 +2342,7 @@ impl_eq! { Cow<'a, str>, String } #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] impl<const COOP_PREFERRED: bool> const Default for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Creates an empty `String`. #[inline] @@ -2354,7 +2354,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> fmt::Display for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -2365,7 +2365,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> fmt::Debug for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -2376,7 +2376,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> hash::Hash for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn hash<H: hash::Hasher>(&self, hasher: &mut H) { @@ -2425,7 +2425,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> Add<&str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Output = String<COOP_PREFERRED>; @@ -2443,7 +2443,7 @@ where #[stable(feature = "stringaddassign", since = "1.12.0")] impl<const COOP_PREFERRED: bool> AddAssign<&str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn add_assign(&mut self, other: &str) { @@ -2454,7 +2454,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::Range<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Output = str; @@ -2466,7 +2466,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeTo<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Output = str; @@ -2478,7 +2478,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeFrom<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Output = str; @@ -2490,7 +2490,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeFull> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Output = str; @@ -2502,7 +2502,7 @@ where #[stable(feature = "inclusive_range", since = "1.26.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeInclusive<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Output = str; @@ -2514,7 +2514,7 @@ where #[stable(feature = "inclusive_range", since = "1.26.0")] impl<const COOP_PREFERRED: bool> ops::Index<ops::RangeToInclusive<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Output = str; @@ -2527,7 +2527,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::Range<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str { @@ -2537,7 +2537,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeTo<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str { @@ -2547,7 +2547,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeFrom<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str { @@ -2557,7 +2557,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeFull> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str { @@ -2568,7 +2568,7 @@ where impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeInclusive<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str { @@ -2579,7 +2579,7 @@ where impl<const COOP_PREFERRED: bool> ops::IndexMut<ops::RangeToInclusive<usize>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str { @@ -2590,7 +2590,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> ops::Deref for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Target = str; @@ -2603,7 +2603,7 @@ where #[stable(feature = "derefmut_for_string", since = "1.3.0")] impl<const COOP_PREFERRED: bool> ops::DerefMut for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn deref_mut(&mut self) -> &mut str { @@ -2623,7 +2623,7 @@ pub type ParseError = core::convert::Infallible; #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> FromStr for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Err = core::convert::Infallible; #[inline] @@ -2645,7 +2645,7 @@ where #[allow(unused_braces)] pub trait ToString<const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts the given value to a `String`. /// @@ -2674,7 +2674,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<T: fmt::Display + ?Sized, const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for T where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { // A common guideline is to not inline generic functions. However, // removing `#[inline]` from this method causes non-negligible regressions. @@ -2713,7 +2713,7 @@ impl ToString for bool { #[stable(feature = "u8_to_string_specialization", since = "1.54.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for u8 where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2736,7 +2736,7 @@ where #[stable(feature = "i8_to_string_specialization", since = "1.54.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for i8 where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2762,7 +2762,7 @@ where #[stable(feature = "str_to_string_specialization", since = "1.9.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for str where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2774,7 +2774,7 @@ where #[stable(feature = "cow_str_to_string_specialization", since = "1.17.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for Cow<'_, str> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2786,7 +2786,7 @@ where #[stable(feature = "string_to_string_specialization", since = "1.17.0")] impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn to_string(&self) -> String<COOP_PREFERRED> { @@ -2797,7 +2797,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> AsRef<str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn as_ref(&self) -> &str { @@ -2808,7 +2808,7 @@ where #[stable(feature = "string_as_mut", since = "1.43.0")] impl<const COOP_PREFERRED: bool> AsMut<str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn as_mut(&mut self) -> &mut str { @@ -2819,7 +2819,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> AsRef<[u8]> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn as_ref(&self) -> &[u8] { @@ -2831,7 +2831,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> From<&str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts a `&str` into a [`String`]. /// @@ -2846,7 +2846,7 @@ where #[stable(feature = "from_mut_str_for_string", since = "1.44.0")] impl<const COOP_PREFERRED: bool> From<&mut str> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts a `&mut str` into a [`String`]. /// @@ -2861,7 +2861,7 @@ where #[stable(feature = "from_ref_string", since = "1.35.0")] impl<const COOP_PREFERRED: bool> From<&String<COOP_PREFERRED>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts a `&String` into a [`String`]. /// @@ -2876,7 +2876,7 @@ where #[cfg(not(test))] #[stable(feature = "string_from_box", since = "1.18.0")] impl<const COOP_PREFERRED: bool> From<Box<str>> for String<COOP_PREFERRED> -where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts the given boxed `str` slice to a [`String`]. /// It is notable that the `str` slice is owned. @@ -2892,7 +2892,7 @@ where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PR /// /// assert_eq!("hello world", s3) /// ``` - fn from(s: Box<str>) -> String { + fn from(s: Box<str>) -> String<COOP_PREFERRED> { s.into_string() } } @@ -2901,7 +2901,7 @@ where [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PR #[stable(feature = "box_from_str", since = "1.20.0")] impl<const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Box<str> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts the given [`String`] to a boxed `str` slice that is owned. /// @@ -2925,7 +2925,7 @@ where #[stable(feature = "string_from_cow_str", since = "1.14.0")] impl<'a, const COOP_PREFERRED: bool> From<Cow<'a, str>> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts a clone-on-write string to an owned /// instance of [`String`]. @@ -2973,7 +2973,7 @@ impl<'a> From<&'a str> for Cow<'a, str> { #[stable(feature = "rust1", since = "1.0.0")] impl<'a, const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Cow<'a, str> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts a [`String`] into an [`Owned`] variant. /// No heap allocation is performed, and the string @@ -2999,7 +2999,7 @@ where #[stable(feature = "cow_from_string_ref", since = "1.28.0")] impl<'a, const COOP_PREFERRED: bool> From<&'a String<COOP_PREFERRED>> for Cow<'a, str> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts a [`String`] reference into a [`Borrowed`] variant. /// No heap allocation is performed, and the string @@ -3040,7 +3040,7 @@ impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> { #[stable(feature = "cow_str_from_iter", since = "1.12.0")] impl<'a, const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for Cow<'a, str> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>(it: I) -> Cow<'a, str> { Cow::Owned(FromIterator::from_iter(it)) @@ -3050,7 +3050,7 @@ where #[stable(feature = "from_string_for_vec_u8", since = "1.14.0")] impl<const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Vec<u8, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`]. /// @@ -3075,7 +3075,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> fmt::Write for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn write_str(&mut self, s: &str) -> fmt::Result { @@ -3100,7 +3100,7 @@ where #[allow(unused_braces)] pub struct Drain<'a, const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Will be used as &'a mut String in the destructor string: *mut String<COOP_PREFERRED>, @@ -3115,7 +3115,7 @@ where #[stable(feature = "collection_debug", since = "1.17.0")] impl<const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Drain").field(&self.as_str()).finish() @@ -3124,16 +3124,19 @@ where #[stable(feature = "drain", since = "1.6.0")] unsafe impl<const COOP_PREFERRED: bool> Sync for Drain<'_, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { } #[stable(feature = "drain", since = "1.6.0")] -unsafe impl<const COOP_PREFERRED: bool> Send for Drain<'_, COOP_PREFERRED> {} +unsafe impl<const COOP_PREFERRED: bool> Send for Drain<'_, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{} #[stable(feature = "drain", since = "1.6.0")] impl<const COOP_PREFERRED: bool> Drop for Drain<'_, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn drop(&mut self) { unsafe { @@ -3149,7 +3152,7 @@ where impl<'a, const COOP_PREFERRED: bool> Drain<'a, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Returns the remaining (sub)string of this iterator as a slice. /// @@ -3172,7 +3175,7 @@ where #[stable(feature = "string_drain_as_str", since = "1.55.0")] impl<'a, const COOP_PREFERRED: bool> AsRef<str> for Drain<'a, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &str { self.as_str() @@ -3182,7 +3185,7 @@ where #[stable(feature = "string_drain_as_str", since = "1.55.0")] impl<'a, const COOP_PREFERRED: bool> AsRef<[u8]> for Drain<'a, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn as_ref(&self) -> &[u8] { self.as_str().as_bytes() @@ -3192,7 +3195,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<const COOP_PREFERRED: bool> Iterator for Drain<'_, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Item = char; @@ -3214,7 +3217,7 @@ where #[stable(feature = "drain", since = "1.6.0")] impl<const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn next_back(&mut self) -> Option<char> { @@ -3224,7 +3227,7 @@ where #[stable(feature = "fused", since = "1.26.0")] impl<const COOP_PREFERRED: bool> FusedIterator for Drain<'_, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]: + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: { } @@ -3232,7 +3235,7 @@ impl<const COOP_PREFERRED: bool> FusedIterator for Drain<'_, COOP_PREFERRED> whe #[stable(feature = "from_char_for_string", since = "1.46.0")] impl<const COOP_PREFERRED: bool> From<char> for String<COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Allocates an owned [`String`] from a single character. /// diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 05fee754beec8..a219ca302d4c4 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -26,7 +26,7 @@ pub struct Drain< #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, const COOP_PREFERRED: bool = { SHORT_TERM_VEC_PREFERS_COOP!() }, > where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// Index of tail to preserve pub(super) tail_start: usize, diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs index 25ca33c6a7bf0..6788b59f3a020 100644 --- a/library/alloc/src/vec/in_place_drop.rs +++ b/library/alloc/src/vec/in_place_drop.rs @@ -34,6 +34,7 @@ pub(super) struct InPlaceDstBufDrop<T> { impl<T> Drop for InPlaceDstBufDrop<T> { #[inline] fn drop(&mut self) { - unsafe { super::Vec::from_raw_parts(self.ptr, self.len, self.cap) }; + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + unsafe { super::Vec::<T, Global, false>::from_raw_parts(self.ptr, self.len, self.cap) }; } } diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 5afb11ee7ba24..569cc61635296 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -451,7 +451,8 @@ where // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec let alloc = ManuallyDrop::take(&mut self.0.alloc); // RawVec handles deallocation - let _ = RawVec::<T, A, true>::from_raw_parts_in( + // @FIXME pass true instead of COOP_PREFERRED - use e.g.: if COOP_PREFERRED {let _ = RawVec::<T, A, COOP_PREFERRED>::from_raw_parts_in(..) } else { let _ = from_raw_parts_in_coop(...)} } + let _ = RawVec::<T, A, COOP_PREFERRED>::from_raw_parts_in( self.0.buf.as_ptr(), self.0.cap, alloc, diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 1116cc50e4275..bb083cbe15a07 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -447,7 +447,7 @@ pub type WeVec<T, const weight: u8> = Vec<T, Global, { weight > 127 }>; impl<T, const COOP_PREFERRED: bool> Vec<T, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Constructs a new, empty `Vec<T>`. /// @@ -2060,9 +2060,11 @@ where /// assert_eq!(v, &[]); /// ``` #[stable(feature = "drain", since = "1.6.0")] - pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A> + pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A, COOP_PREFERRED> where R: RangeBounds<usize>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, + { // Memory safety // @@ -2557,7 +2559,7 @@ where // - `new_cap` refers to the same sized allocation as `cap` because // `new_cap * size_of::<T>()` == `cap * size_of::<[T; N]>()` // - `len` <= `cap`, so `len * N` <= `cap * N`. - unsafe { Vec::<T, A>::from_raw_parts_in(ptr.cast(), new_len, new_cap, alloc) } + unsafe { Vec::<T, A, COOP_PREFERRED>::from_raw_parts_in(ptr.cast(), new_len, new_cap, alloc) } } } @@ -2886,9 +2888,11 @@ impl<T> FromIterator<T> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for Vec<T, A, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, + { type Item = T; - type IntoIter = IntoIter<T, A>; + type IntoIter = IntoIter<T, A, COOP_PREFERRED>; /// Creates a consuming iterator, that is, one that moves each value out of /// the vector (from start to end). The vector cannot be used after calling @@ -3080,10 +3084,11 @@ where #[cfg(not(no_global_oom_handling))] #[inline] #[stable(feature = "vec_splice", since = "1.21.0")] - pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, A> + pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, A, COOP_PREFERRED> where R: RangeBounds<usize>, I: IntoIterator<Item = T>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(true)]:, { Splice { drain: self.drain(range), replace_with: replace_with.into_iter() } } @@ -3133,9 +3138,10 @@ where /// assert_eq!(odds, vec![1, 3, 5, 9, 11, 13, 15]); /// ``` #[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] - pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F, A> + pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F, A, COOP_PREFERRED> where F: FnMut(&mut T) -> bool, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(true)]:, { let old_len = self.len(); @@ -3228,7 +3234,7 @@ where #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] impl<T, const COOP_PREFERRED: bool> const Default for Vec<T, Global, COOP_PREFERRED> where - [(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Creates an empty `Vec<T>`. /// diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index 3766f6de061bb..bc4169a24f0b2 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -14,7 +14,7 @@ pub(super) trait SpecFromElem: Sized { alloc: A, ) -> Vec<Self, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>(COOP_PREFERRED)]:; + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:; } impl<T: Clone> SpecFromElem for T { diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index d8569a6e89c62..185f09be55287 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -23,17 +23,19 @@ pub struct Splice< 'a, I: Iterator + 'a, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global, + const COOP_PREFERRED: bool = false > where - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + //FIXME: If using SHORT_TERM_VEC_PREFERS_COOP!() instead of true, and the same for `drain` below, then this failed with: `derive` cannot be used on item swith type macros { - pub(super) drain: Drain<'a, I::Item, A>, + pub(super) drain: Drain<'a, I::Item, A, COOP_PREFERRED>, pub(super) replace_with: I, } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> Iterator for Splice<'_, I, A> +impl<I: Iterator, A: Allocator, const COOP_PREFERRED: bool> Iterator for Splice<'_, I, A, COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { type Item = I::Item; @@ -47,9 +49,9 @@ where } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> DoubleEndedIterator for Splice<'_, I, A> +impl<I: Iterator, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Splice<'_, I, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn next_back(&mut self) -> Option<Self::Item> { self.drain.next_back() @@ -57,15 +59,15 @@ where } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]: +impl<I: Iterator, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Splice<'_, I, A, COOP_PREFERRED> where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: { } #[stable(feature = "vec_splice", since = "1.21.0")] -impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> +impl<I: Iterator, A: Allocator, const COOP_PREFERRED: bool> Drop for Splice<'_, I, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { fn drop(&mut self) { self.drain.by_ref().for_each(drop); @@ -115,7 +117,7 @@ where /// Private helper methods for `Splice::drop` impl<T, A: Allocator> Drain<'_, T, A> where - [(); alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(true)]:, { /// The range from `self.vec.len` to `self.tail_start` contains elements /// that have been moved out. diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 01eefe94565df..0f4ede7468f32 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -96,7 +96,7 @@ pub type SliceAndMetaResult = Result<SliceAndMeta, AllocError>; pub const fn co_alloc_metadata_num_slots<A: Allocator>() -> usize { // @FIXME later if false { - panic!("FIXME - consider replacing co_alloc_metadata_num_slots() with co_alloc_metadata_num_slots_with_preference_specific(bool), and adding const flags as appropriate."); + panic!("FIXME - consider replacing co_alloc_metadata_num_slots() with co_alloc_metadata_num_slots_with_preference(bool), and adding const flags as appropriate."); } if A::IS_CO_ALLOCATOR { 1 } else { 0 } } From e3a5fb64d02c417f66fc09036844038833fb551f Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sun, 8 Jan 2023 21:56:38 -0800 Subject: [PATCH 26/47] Fixing A and COOP_PREFERRED generics. WIP. --- library/alloc/src/boxed.rs | 18 +++++++++++++----- .../collections/vec_deque/spec_from_iter.rs | 2 +- library/alloc/src/vec/in_place_drop.rs | 1 + library/alloc/src/vec/mod.rs | 8 ++++++-- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 9d455b0568364..32277063ac0b0 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -778,7 +778,10 @@ where // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] #[allow(unused_braces)] - pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { + pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, + { unsafe { RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_in(len, alloc) .into_box(len) @@ -810,7 +813,10 @@ where // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] #[allow(unused_braces)] - pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> { + pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, + { unsafe { RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_zeroed_in( len, alloc, @@ -1521,7 +1527,8 @@ impl<T: Copy> From<&[T]> for Box<[T]> { /// ``` fn from(slice: &[T]) -> Box<[T]> { let len = slice.len(); - let buf = RawVec::with_capacity(len); + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + let buf = RawVec::<T, Global, false>::with_capacity(len); unsafe { ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len); buf.into_box(slice.len()).assume_init() @@ -2050,11 +2057,12 @@ impl<I> FromIterator<I> for Box<[I]> { #[stable(feature = "box_slice_clone", since = "1.3.0")] impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> where - [(); core::alloc::co_alloc_metadata_num_slots::<A>()]:, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:, { fn clone(&self) -> Self { let alloc = Box::allocator(self).clone(); - self.to_vec_in(alloc).into_boxed_slice() + // false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice. + self.to_vec_in::<A, false>(alloc).into_boxed_slice() } fn clone_from(&mut self, other: &Self) { diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index 85dd756337350..b9f77ffa07503 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -18,7 +18,7 @@ where // anything where we can't do something extra-special for `VecDeque`, // especially as that could save us some monomorphiziation work // if one uses the same iterators (like slice ones) with both. - crate::vec::Vec::from_iter(iterator).into() + crate::vec::Vec::<T, A, COOP_PREFERRED>::from_iter(iterator).into() } } diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs index 6788b59f3a020..871fd08b1f6b1 100644 --- a/library/alloc/src/vec/in_place_drop.rs +++ b/library/alloc/src/vec/in_place_drop.rs @@ -1,5 +1,6 @@ use core::ptr::{self}; use core::slice::{self}; +use crate::alloc::Global; // A helper struct for in-place iteration that drops the destination slice of iteration, // i.e. the head. The source slice (the tail) is dropped by IntoIter. diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index bb083cbe15a07..a1e329679e6b7 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -54,6 +54,7 @@ #![stable(feature = "rust1", since = "1.0.0")] #[cfg(not(no_global_oom_handling))] +use core::alloc; use core::cmp; use core::cmp::Ordering; use core::convert::TryFrom; @@ -2877,9 +2878,12 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] #[allow(unused_braces)] -impl<T> FromIterator<T> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { +impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for Vec<T, A, COOP_PREFERRED> +where + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { #[inline] - fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> { + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T, A, COOP_PREFERRED> { <Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter()) } } From ac054b989b096ef7bc1e7bc34084919fde23dd47 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 9 Jan 2023 06:09:20 -0800 Subject: [PATCH 27/47] Fixing A and COOP_PREFERRED generics. WIP.. --- .../alloc/src/collections/vec_deque/spec_from_iter.rs | 6 +++--- library/alloc/src/vec/in_place_collect.rs | 8 ++++---- library/alloc/src/vec/spec_from_iter.rs | 11 +++++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index b9f77ffa07503..fe5a1f169f56f 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -22,18 +22,18 @@ where } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T>> +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T, A, COOP_PREFERRED>> for VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] - fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self { + fn spec_from_iter(iterator: crate::vec::IntoIter<T, A, COOP_PREFERRED>) -> Self { iterator.into_vecdeque() } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T, A, COOP_PREFERRED>> for VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs index 6f3c44a8aa8b6..6d9e3b825c376 100644 --- a/library/alloc/src/vec/in_place_collect.rs +++ b/library/alloc/src/vec/in_place_collect.rs @@ -137,13 +137,11 @@ //! } //! vec.truncate(write_idx); //! ``` +use core::alloc::{self, Allocator}; use core::iter::{InPlaceIterable, SourceIter, TrustedRandomAccessNoCoerce}; use core::mem::{self, ManuallyDrop, SizedTypeProperties}; use core::ptr::{self}; -use crate::Global; -use crate::DEFAULT_COOP_PREFERRED; - use super::{InPlaceDrop, InPlaceDstBufDrop, SpecFromIter, SpecFromIterNested, Vec}; /// Specialization marker for collecting an iterator pipeline into a Vec while reusing the @@ -154,9 +152,11 @@ pub(super) trait InPlaceIterableMarker {} impl<T> InPlaceIterableMarker for T where T: InPlaceIterable {} #[allow(unused_braces)] -impl<T, I> SpecFromIter<T, I> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = T> + SourceIter<Source: AsVecIntoIter> + InPlaceIterableMarker, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + { default fn from_iter(mut iterator: I) -> Self { // See "Layout constraints" section in the module documentation. We rely on const diff --git a/library/alloc/src/vec/spec_from_iter.rs b/library/alloc/src/vec/spec_from_iter.rs index 06da451fe3932..9ed63aceb77a0 100644 --- a/library/alloc/src/vec/spec_from_iter.rs +++ b/library/alloc/src/vec/spec_from_iter.rs @@ -1,7 +1,6 @@ +use core::alloc::{self, Allocator}; use core::mem::ManuallyDrop; use core::ptr::{self}; -use crate::Global; -use crate::DEFAULT_COOP_PREFERRED; use super::{IntoIter, SpecExtend, SpecFromIterNested, Vec}; @@ -28,9 +27,10 @@ pub(super) trait SpecFromIter<T, I> { } #[allow(unused_braces)] -impl<T, I> SpecFromIter<T, I> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> +impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for Vec<T, A, COOP_PREFERRED> where I: Iterator<Item = T>, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { default fn from_iter(iterator: I) -> Self { SpecFromIterNested::from_iter(iterator) @@ -38,7 +38,10 @@ where } #[allow(unused_braces)] -impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> { +impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for Vec<T, A, COOP_PREFERRED> +where +[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +{ fn from_iter(iterator: IntoIter<T>) -> Self { // A common case is passing a vector into a function which immediately // re-collects into a vector. We can short circuit this if the IntoIter From 1f04b072575389e4bbced7e85ae53bba89a7e0ac Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 9 Jan 2023 11:01:47 -0800 Subject: [PATCH 28/47] Fixing A and COOP_PREFERRED generics. WIP... --- library/alloc/src/collections/vec_deque/mod.rs | 15 ++++++++------- .../src/collections/vec_deque/spec_from_iter.rs | 2 +- library/alloc/src/ffi/c_str.rs | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 073bac85894d9..af17728fed5c0 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -166,7 +166,7 @@ where /// Creates an empty deque. #[inline] fn default() -> VecDeque<T, Global, COOP_PREFERRED> { - VecDeque::new() + VecDeque::<T, Global, COOP_PREFERRED>::new() } } @@ -554,6 +554,7 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> VecDeque<T, A, COOP_PREFERRED> where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Creates an empty deque. /// @@ -587,8 +588,8 @@ where #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] - pub fn with_capacity(capacity: usize) -> VecDeque<T, A, COOP_PREFERRED> { - Self::with_capacity_in(capacity, Global) + pub fn with_capacity(capacity: usize) -> VecDeque<T, Global, COOP_PREFERRED> { + VecDeque::<T, Global, COOP_PREFERRED>::with_capacity_in(capacity, Global) } } @@ -1402,7 +1403,7 @@ where /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] - pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A> + pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A, COOP_PREFERRED> where R: RangeBounds<usize>, [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, @@ -2923,7 +2924,7 @@ where fn from(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self { let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); - Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } + Self { head: 0, len, buf: unsafe { RawVec::<T, A, COOP_PREFERRED>::from_raw_parts_in(ptr, cap, alloc) } } } } @@ -2982,7 +2983,7 @@ where ptr::copy(buf.add(other.head), buf, len); } // @FIXME: COOP - Vec::from_raw_parts_in(buf, len, cap, alloc) + Vec::<T, A, COOP_PREFERRED>::from_raw_parts_in(buf, len, cap, alloc) } } } @@ -3003,7 +3004,7 @@ where /// assert_eq!(deq1, deq2); /// ``` fn from(arr: [T; N]) -> Self { - let mut deq = VecDeque::with_capacity(N); + let mut deq = VecDeque::<T, Global, COOP_PREFERRED>::with_capacity(N); let arr = ManuallyDrop::new(arr); if !<T>::IS_ZST { // SAFETY: VecDeque::with_capacity ensures that there is enough capacity. diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index fe5a1f169f56f..450a64b792c5b 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -39,7 +39,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { #[inline] - fn spec_from_iter(iterator: IntoIter<T>) -> Self { + fn spec_from_iter(iterator: IntoIter<T, A, COOP_PREFERRED>) -> Self { iterator.into_vecdeque() } } diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index dd2e6baab2776..007db2e82aafd 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -1103,7 +1103,8 @@ impl CStr { without modifying the original"] #[stable(feature = "cstr_to_str", since = "1.4.0")] pub fn to_string_lossy(&self) -> Cow<'_, str> { - String::from_utf8_lossy(self.to_bytes()) + // false = no need for co-alloc metadata, since it would get lost once converted to the slice. + String::<false>::from_utf8_lossy(self.to_bytes()) } /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating. From 9d055079b71c5d7865107ac730a3c6199cd8c9c0 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 9 Jan 2023 11:37:10 -0800 Subject: [PATCH 29/47] Fixing A and COOP_PREFERRED generics. WIP.... --- library/alloc/src/slice.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index e5bf6a59bf6bf..7be0eb544ad44 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -1109,13 +1109,16 @@ where // shallow copies of the contents of `v` without risking the dtors running on copies if // `is_less` panics. When merging two sorted runs, this buffer holds a copy of the shorter run, // which will always have length at most `len / 2`. - let mut buf = Vec::with_capacity(len / 2); + // `buf` is temporary = not passed around too much => using COOP_PREFERRED=true. + // @FIXME move definitions of `buf` and `runs` down, after while end > 0 {...}, just before they are used. Then benchmark if it makes (cache-related) difference. + let mut buf = Vec::<T, Global, true>::with_capacity(len / 2); // In order to identify natural runs in `v`, we traverse it backwards. That might seem like a // strange decision, but consider the fact that merges more often go in the opposite direction // (forwards). According to benchmarks, merging forwards is slightly faster than merging // backwards. To conclude, identifying runs by traversing backwards improves performance. - let mut runs = vec![]; + // `runs` is temporary = not passed around too much => using COOP_PREFERRED=true. + let mut runs: Vec<_, Global, true> = vec![]; let mut end = len; while end > 0 { // Find the next natural run, and reverse it if it's strictly descending. From e65b809d072155f4a52f491c28d40ad7113a2060 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 9 Jan 2023 15:41:23 -0800 Subject: [PATCH 30/47] Fixing A and COOP_PREFERRED generics. WIP..... --- library/alloc/src/string.rs | 27 +++++++++++++++++++-------- library/alloc/src/vec/mod.rs | 1 + 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 448316cb4feed..60217c3a6bdca 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -45,7 +45,7 @@ use crate::alloc::Global; -use crate::vec::PlVec; +use core::alloc; #[cfg(not(no_global_oom_handling))] use core::char::{decode_utf16, REPLACEMENT_CHARACTER}; use core::error::Error; @@ -413,8 +413,11 @@ where #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(no_global_oom_handling), derive(Clone))] #[derive(Debug, PartialEq, Eq)] -pub struct FromUtf8Error { - bytes: PlVec<u8>, +pub struct FromUtf8Error<const COOP_PREFERRED: bool> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + bytes: Vec<u8, Global, COOP_PREFERRED>, error: Utf8Error, } @@ -584,7 +587,7 @@ where /// [`into_bytes`]: String::into_bytes #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn from_utf8(vec: Vec<u8, Global, COOP_PREFERRED>) -> Result<String, FromUtf8Error> { + pub fn from_utf8(vec: Vec<u8, Global, COOP_PREFERRED>) -> Result<String<COOP_PREFERRED>, FromUtf8Error<COOP_PREFERRED>> { match str::from_utf8(&vec) { Ok(..) => Ok(String { vec }), Err(e) => Err(FromUtf8Error { bytes: vec, error: e }), @@ -1910,7 +1913,9 @@ where } } -impl FromUtf8Error { +impl <const COOP_PREFERRED: bool> FromUtf8Error<COOP_PREFERRED> +where +[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Returns a slice of [`u8`]s bytes that were attempted to convert to a `String`. /// /// # Examples @@ -1951,7 +1956,7 @@ impl FromUtf8Error { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "rust1", since = "1.0.0")] - pub fn into_bytes<const COOP_PREFERRED: bool>(self) -> Vec<u8, Global, COOP_PREFERRED> + pub fn into_bytes(self) -> Vec<u8, Global, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { @@ -1989,7 +1994,10 @@ impl FromUtf8Error { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Display for FromUtf8Error { +impl<const COOP_PREFERRED: bool> fmt::Display for FromUtf8Error<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.error, f) } @@ -2003,7 +2011,10 @@ impl fmt::Display for FromUtf16Error { } #[stable(feature = "rust1", since = "1.0.0")] -impl Error for FromUtf8Error { +impl<const COOP_PREFERRED: bool> Error for FromUtf8Error<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[allow(deprecated)] fn description(&self) -> &str { "invalid utf-8" diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index a1e329679e6b7..7935a71008aae 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -424,6 +424,7 @@ pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: All Vec<T, A, true>; /// "Plain" Vec. Not "cooperative" - not carrying extra data to assist the allocator. +/// FIXME after cleanup, see if we still use this in core:: and/or alloc:: #[unstable(feature = "global_co_alloc_plvec", issue = "none")] pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = Vec<T, A, false>; From 16c897e5271a34c5e2b6d6a4eb30e9ce6e597683 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Mon, 9 Jan 2023 17:35:19 -0800 Subject: [PATCH 31/47] Fixing A and COOP_PREFERRED generics. WIP --- library/alloc/src/lib.rs | 6 ------ library/alloc/src/string.rs | 2 +- library/alloc/src/vec/spec_from_iter.rs | 7 ++++--- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index c8035bd295f58..64cde38960319 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -288,9 +288,3 @@ pub(crate) mod test_helpers { use crate::alloc::Global; -/// See also `core::alloc::co_alloc_metadata_num_slots_with_preference`. -#[unstable(feature = "global_co_alloc", issue = "none")] -pub const fn co_alloc_metadata_num_slots_with_preference_global(coop_preferred: bool) -> usize { - // FIXME or replace any calls with core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED). See raw_vec.rs. - if /*Global::IS_CO_ALLOCATOR*/ true && coop_preferred { 1 } else { 0 } -} diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 60217c3a6bdca..69e0d5ac93ce0 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -662,7 +662,7 @@ where const REPLACEMENT: &str = "\u{FFFD}"; - let mut res = String::with_capacity(v.len()); + let mut res = String::<false>::with_capacity(v.len()); res.push_str(first_valid); res.push_str(REPLACEMENT); diff --git a/library/alloc/src/vec/spec_from_iter.rs b/library/alloc/src/vec/spec_from_iter.rs index 9ed63aceb77a0..2da172c8ea499 100644 --- a/library/alloc/src/vec/spec_from_iter.rs +++ b/library/alloc/src/vec/spec_from_iter.rs @@ -1,4 +1,5 @@ use core::alloc::{self, Allocator}; +use crate::alloc::Global; use core::mem::ManuallyDrop; use core::ptr::{self}; @@ -38,9 +39,9 @@ where } #[allow(unused_braces)] -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for Vec<T, A, COOP_PREFERRED> +impl<T, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for Vec<T, Global, COOP_PREFERRED> where -[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, +[(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn from_iter(iterator: IntoIter<T>) -> Self { // A common case is passing a vector into a function which immediately @@ -62,7 +63,7 @@ where } } - let mut vec = Vec::new(); + let mut vec = Vec::<T, Global, COOP_PREFERRED>::new(); // must delegate to spec_extend() since extend() itself delegates // to spec_from for empty Vecs vec.spec_extend(iterator); From 9c0e8e0cfc95b309925c2efeff9d18307ac031db Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 11 Jan 2023 13:27:17 -0800 Subject: [PATCH 32/47] Fixing A and COOP_PREFERRED generics. WIP. --- .../alloc/src/collections/vec_deque/mod.rs | 6 ++--- .../collections/vec_deque/spec_from_iter.rs | 26 +++++++++---------- library/alloc/src/vec/in_place_collect.rs | 7 ++--- library/alloc/src/vec/into_iter.rs | 2 +- library/alloc/src/vec/mod.rs | 6 ++--- library/alloc/src/vec/spec_from_iter.rs | 6 ++--- .../alloc/src/vec/spec_from_iter_nested.rs | 6 +++-- 7 files changed, 31 insertions(+), 28 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index af17728fed5c0..34f82acb166d4 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2799,11 +2799,11 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, A, COOP_PREFERRED> +impl<T, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { - fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, A, COOP_PREFERRED> { + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> { SpecFromIter::spec_from_iter(iter.into_iter()) } } diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index 450a64b792c5b..ecfe79799aa91 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -1,5 +1,5 @@ use super::{IntoIter, VecDeque}; -use crate::alloc::Allocator; +use crate::Global; use core::alloc; /// Specialization trait used for `VecDeque::from_iter` @@ -7,39 +7,39 @@ pub(super) trait SpecFromIter<T, I> { fn spec_from_iter(iter: I) -> Self; } -impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> - for VecDeque<T, A, COOP_PREFERRED> +impl<T, I, const COOP_PREFERRED: bool> SpecFromIter<T, I> + for VecDeque<T, Global, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { default fn spec_from_iter(iterator: I) -> Self { // Since converting is O(1) now, just re-use the `Vec` logic for // anything where we can't do something extra-special for `VecDeque`, // especially as that could save us some monomorphiziation work // if one uses the same iterators (like slice ones) with both. - crate::vec::Vec::<T, A, COOP_PREFERRED>::from_iter(iterator).into() + crate::vec::Vec::<T, Global, COOP_PREFERRED>::from_iter(iterator).into() } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T, A, COOP_PREFERRED>> - for VecDeque<T, A, COOP_PREFERRED> +impl<T, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T, Global, COOP_PREFERRED>> + for VecDeque<T, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] - fn spec_from_iter(iterator: crate::vec::IntoIter<T, A, COOP_PREFERRED>) -> Self { + fn spec_from_iter(iterator: crate::vec::IntoIter<T, Global, COOP_PREFERRED>) -> Self { iterator.into_vecdeque() } } -impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T, A, COOP_PREFERRED>> - for VecDeque<T, A, COOP_PREFERRED> +impl<T, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T, Global, COOP_PREFERRED>> + for VecDeque<T, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] - fn spec_from_iter(iterator: IntoIter<T, A, COOP_PREFERRED>) -> Self { + fn spec_from_iter(iterator: IntoIter<T, Global, COOP_PREFERRED>) -> Self { iterator.into_vecdeque() } } diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs index 6d9e3b825c376..69f8ea9a61d3b 100644 --- a/library/alloc/src/vec/in_place_collect.rs +++ b/library/alloc/src/vec/in_place_collect.rs @@ -137,7 +137,8 @@ //! } //! vec.truncate(write_idx); //! ``` -use core::alloc::{self, Allocator}; +use core::alloc; +use crate::alloc::Global; use core::iter::{InPlaceIterable, SourceIter, TrustedRandomAccessNoCoerce}; use core::mem::{self, ManuallyDrop, SizedTypeProperties}; use core::ptr::{self}; @@ -152,10 +153,10 @@ pub(super) trait InPlaceIterableMarker {} impl<T> InPlaceIterableMarker for T where T: InPlaceIterable {} #[allow(unused_braces)] -impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for Vec<T, A, COOP_PREFERRED> +impl<T, I, const COOP_PREFERRED: bool> SpecFromIter<T, I> for Vec<T, Global, COOP_PREFERRED> where I: Iterator<Item = T> + SourceIter<Source: AsVecIntoIter> + InPlaceIterableMarker, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { default fn from_iter(mut iterator: I) -> Self { diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 569cc61635296..9e54f6db5d34b 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -421,7 +421,7 @@ where { #[cfg(not(test))] fn clone(&self) -> Self { - self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter() + self.as_slice().to_vec_in::<A, COOP_PREFERRED>(self.alloc.deref().clone()).into_iter() } #[cfg(test)] fn clone(&self) -> Self { diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 7935a71008aae..b5b5836ee2101 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2879,12 +2879,12 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] #[allow(unused_braces)] -impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for Vec<T, A, COOP_PREFERRED> +impl<T, const COOP_PREFERRED: bool> FromIterator<T> for Vec<T, Global, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] - fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T, A, COOP_PREFERRED> { + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T, Global, COOP_PREFERRED> { <Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter()) } } diff --git a/library/alloc/src/vec/spec_from_iter.rs b/library/alloc/src/vec/spec_from_iter.rs index 2da172c8ea499..6f414082ab292 100644 --- a/library/alloc/src/vec/spec_from_iter.rs +++ b/library/alloc/src/vec/spec_from_iter.rs @@ -1,4 +1,4 @@ -use core::alloc::{self, Allocator}; +use core::alloc; use crate::alloc::Global; use core::mem::ManuallyDrop; use core::ptr::{self}; @@ -28,10 +28,10 @@ pub(super) trait SpecFromIter<T, I> { } #[allow(unused_braces)] -impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for Vec<T, A, COOP_PREFERRED> +impl<T, I, const COOP_PREFERRED: bool> SpecFromIter<T, I> for Vec<T, Global, COOP_PREFERRED> where I: Iterator<Item = T>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { default fn from_iter(iterator: I) -> Self { SpecFromIterNested::from_iter(iterator) diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index 7408155ff870a..bbfb9605a6d00 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -1,3 +1,4 @@ +use core::alloc; use core::cmp; use core::iter::TrustedLen; use core::ptr; @@ -16,9 +17,10 @@ pub(super) trait SpecFromIterNested<T, I> { } #[allow(unused_braces)] -impl<T, I> SpecFromIterNested<T, I> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> +impl<T, I, const COOP_PREFERRED: bool> SpecFromIterNested<T, I> for Vec<T, Global, COOP_PREFERRED> where I: Iterator<Item = T>, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { default fn from_iter(mut iterator: I) -> Self { // Unroll the first iteration, as the vector is going to be @@ -43,7 +45,7 @@ where }; // must delegate to spec_extend() since extend() itself delegates // to spec_from for empty Vecs - <Vec<T> as SpecExtend<T, I>>::spec_extend(&mut vector, iterator); + <Vec<T, Global, COOP_PREFERRED> as SpecExtend<T, I>>::spec_extend(&mut vector, iterator); vector } } From de1791e2b2f481ba4dadcc85da8cd908b1d0c653 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 11 Jan 2023 14:38:20 -0800 Subject: [PATCH 33/47] Cleanup of unused import --- library/alloc/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 64cde38960319..bf68c9773e72d 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -286,5 +286,4 @@ pub(crate) mod test_helpers { rand::SeedableRng::from_seed(seed) } -use crate::alloc::Global; From ac8406e30a39e7c7717d29a9143e40cda3eac43e Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 11 Jan 2023 14:39:44 -0800 Subject: [PATCH 34/47] Re-added a closing curly bracket in mod test_helpers = Fixing my earlier merge mess up. --- library/alloc/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index bf68c9773e72d..8e74717255203 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -285,5 +285,5 @@ pub(crate) mod test_helpers { let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap(); rand::SeedableRng::from_seed(seed) } - +} From 701135d334c3a806f6db38c67e9d4a91002b196d Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 11 Jan 2023 15:00:35 -0800 Subject: [PATCH 35/47] Fixing A and COOP_PREFERRED generics. WIP. --- library/alloc/src/collections/vec_deque/spec_from_iter.rs | 2 +- library/alloc/src/ffi/c_str.rs | 2 +- library/alloc/src/slice.rs | 7 +++++-- library/alloc/src/vec/spec_from_iter_nested.rs | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/spec_from_iter.rs b/library/alloc/src/collections/vec_deque/spec_from_iter.rs index ecfe79799aa91..5da3faaabe2c6 100644 --- a/library/alloc/src/collections/vec_deque/spec_from_iter.rs +++ b/library/alloc/src/collections/vec_deque/spec_from_iter.rs @@ -1,5 +1,5 @@ use super::{IntoIter, VecDeque}; -use crate::Global; +use crate::alloc::Global; use core::alloc; /// Specialization trait used for `VecDeque::from_iter` diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 007db2e82aafd..b800adb2fed9a 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -17,7 +17,7 @@ use core::ptr; use core::slice; use core::slice::memchr; use core::str::{self, Utf8Error}; -use crate::Global; +use crate::alloc::Global; use crate::DEFAULT_COOP_PREFERRED; #[cfg(target_has_atomic = "ptr")] diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 7be0eb544ad44..71149f88165e7 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -454,11 +454,14 @@ impl<T> [T] { #[rustc_conversion_suggestion] #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn to_vec(&self) -> Vec<T> + pub fn to_vec<const COOP_PREFERRED: bool>(&self) -> Vec<T, Global, COOP_PREFERRED> where T: Clone, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>( + COOP_PREFERRED, + )]:, { - self.to_vec_in(Global) + self.to_vec_in::<Global, COOP_PREFERRED>(Global) } /// Copies `self` into a new `Vec` with an allocator. diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index bbfb9605a6d00..3ccd2da20df49 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -4,7 +4,7 @@ use core::iter::TrustedLen; use core::ptr; use crate::raw_vec::RawVec; -use crate::Global; +use crate::alloc::Global; use crate::DEFAULT_COOP_PREFERRED; use super::{SpecExtend, Vec}; From e6cb4deaf549aa160bd64bcf612dfc27cb271ed4 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 11 Jan 2023 16:19:39 -0800 Subject: [PATCH 36/47] Fixing A and COOP_PREFERRED generics. WIP.. --- library/alloc/src/str.rs | 5 ++++- library/alloc/src/string.rs | 21 ++++++++++++++------- library/alloc/src/vec/into_iter.rs | 12 +++++++++++- library/alloc/src/vec/splice.rs | 4 ++-- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 1a1a344f2fed3..36b24614b0d3c 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -505,7 +505,10 @@ impl str { #[rustc_allow_incoherent_impl] #[must_use = "`self` will be dropped if the result is not used"] #[inline] - pub fn into_string(self: Box<str>) -> String { + pub fn into_string<const COOP_PREFERRED: bool>(self: Box<str>) -> String<COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + { let slice = Box::<[u8]>::from(self); unsafe { String::from_utf8_unchecked(slice.into_vec()) } } diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 69e0d5ac93ce0..6bb75be207d6f 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1480,13 +1480,19 @@ where where F: FnMut(char) -> bool, { - struct SetLenOnDrop<'a> { - s: &'a mut String, + struct SetLenOnDrop<'a, const LOCAL_COOP_PREFERRED: bool> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(LOCAL_COOP_PREFERRED)]:, + { + s: &'a mut String<LOCAL_COOP_PREFERRED>, idx: usize, del_bytes: usize, } - impl<'a> Drop for SetLenOnDrop<'a> { + impl<'a, const LOCAL_COOP_PREFERRED: bool> Drop for SetLenOnDrop<'a, LOCAL_COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(LOCAL_COOP_PREFERRED)]:, + { fn drop(&mut self) { let new_len = self.idx - self.del_bytes; debug_assert!(new_len <= self.s.len()); @@ -1783,7 +1789,7 @@ where /// assert_eq!(s, ""); /// ``` #[stable(feature = "drain", since = "1.6.0")] - pub fn drain<R>(&mut self, range: R) -> Drain<'_> + pub fn drain<R>(&mut self, range: R) -> Drain<'_, COOP_PREFERRED> where R: RangeBounds<usize>, { @@ -2216,16 +2222,16 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(CO #[cfg(not(no_global_oom_handling))] #[stable(feature = "extend_string", since = "1.4.0")] -impl<const COOP_PREFERRED: bool> Extend<String> for String<COOP_PREFERRED> +impl<const COOP_PREFERRED: bool> Extend<String<COOP_PREFERRED>> for String<COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { - fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) { + fn extend<I: IntoIterator<Item = String<COOP_PREFERRED>>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); } #[inline] - fn extend_one(&mut self, s: String) { + fn extend_one(&mut self, s: String<COOP_PREFERRED>) { self.push_str(&s); } } @@ -3109,6 +3115,7 @@ where /// [`drain`]: String::drain #[stable(feature = "drain", since = "1.6.0")] #[allow(unused_braces)] +// @FIXME Do we need to use DEFAULT_COOP_PREFERRED here? pub struct Drain<'a, const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 9e54f6db5d34b..2ec439748f68a 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -131,7 +131,17 @@ where // struct and then overwriting &mut self. // this creates less assembly self.cap = 0; - self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) }; + self.buf = unsafe { + // @FIXME The below if COOP_PREFERRED {..} else {..} + // branching exists, because the following fails. Otherwise we'd have a snowball effect of wide spread of where...Global... + // + // NonNull::new_unchecked(RawVec::<T, Global, COOP_PREFERRED>::NEW.ptr()) + if COOP_PREFERRED { + NonNull::new_unchecked(RawVec::<T, Global, true>::NEW.ptr()) + } else { + NonNull::new_unchecked(RawVec::<T, Global, false>::NEW.ptr()) + } + }; self.ptr = self.buf.as_ptr(); self.end = self.buf.as_ptr(); diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index 185f09be55287..96ca11edaf6a1 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -115,9 +115,9 @@ where } /// Private helper methods for `Splice::drop` -impl<T, A: Allocator> Drain<'_, T, A> +impl<T, A: Allocator, const COOP_PREFERRED: bool> Drain<'_, T, A, COOP_PREFERRED> where - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(true)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, { /// The range from `self.vec.len` to `self.tail_start` contains elements /// that have been moved out. From ab2b964d33e580a3c1b694f8330d6ee0de2d8849 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Wed, 11 Jan 2023 17:45:04 -0800 Subject: [PATCH 37/47] Fixing A and COOP_PREFERRED generics. WIP... --- library/alloc/src/borrow.rs | 8 ++++++-- library/alloc/src/str.rs | 11 +++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index 83a1385599bec..7f08395ce842c 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -11,7 +11,7 @@ use core::ops::{Add, AddAssign}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::borrow::{Borrow, BorrowMut}; -use crate::fmt; +use crate::{fmt, DEFAULT_COOP_PREFERRED, alloc::Global}; #[cfg(not(no_global_oom_handling))] use crate::string::String; @@ -35,7 +35,11 @@ where /// from any borrow of a given type. #[cfg_attr(not(test), rustc_diagnostic_item = "ToOwned")] #[stable(feature = "rust1", since = "1.0.0")] -pub trait ToOwned { +#[allow(unused_braces)] +pub trait ToOwned<const COOP_PREFERRED: bool = { DEFAULT_COOP_PREFERRED!() } > +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// The resulting type after obtaining ownership. #[stable(feature = "rust1", since = "1.0.0")] type Owned: Borrow<Self>; diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 36b24614b0d3c..342ad846ab99e 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -208,14 +208,17 @@ impl BorrowMut<str> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl ToOwned for str { - type Owned = String; +impl<const COOP_PREFERRED: bool> ToOwned<COOP_PREFERRED> for str +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + type Owned = String<COOP_PREFERRED>; #[inline] - fn to_owned(&self) -> String { + fn to_owned(&self) -> String<COOP_PREFERRED> { unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) } } - fn clone_into(&self, target: &mut String) { + fn clone_into(&self, target: &mut String<COOP_PREFERRED>) { let mut b = mem::take(target).into_bytes(); self.as_bytes().clone_into(&mut b); *target = unsafe { String::from_utf8_unchecked(b) } From 87499b8bced81835084dc79da3e57092f6ecfd73 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 12 Jan 2023 10:58:41 -0800 Subject: [PATCH 38/47] Adding COOP_PREFERRED to Cow, ToOwned.. --- library/alloc/src/borrow.rs | 48 ++++++++++++++++++++++------------ library/alloc/src/ffi/c_str.rs | 2 +- library/alloc/src/str.rs | 4 +-- library/alloc/src/string.rs | 14 +++++----- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index 7f08395ce842c..d420ca81e2a7c 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -18,9 +18,11 @@ use crate::string::String; use Cow::*; #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B> +impl<'a, B: ?Sized, const COOP_PREFERRED: bool> Borrow<B> for Cow<'a, B, COOP_PREFERRED> where - B: ToOwned, + B: ToOwned<COOP_PREFERRED>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + { fn borrow(&self) -> &B { &**self @@ -83,9 +85,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T> ToOwned for T +impl<T, const COOP_PREFERRED: bool> ToOwned<COOP_PREFERRED> for T where T: Clone, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Owned = T; fn to_owned(&self) -> T { @@ -180,9 +183,11 @@ where /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "Cow")] -pub enum Cow<'a, B: ?Sized + 'a> +#[allow(unused_braces)] +pub enum Cow<'a, B: ?Sized + 'a, const COOP_PREFERRED: bool = { DEFAULT_COOP_PREFERRED!() }> where - B: ToOwned, + B: ToOwned<COOP_PREFERRED>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Borrowed data. #[stable(feature = "rust1", since = "1.0.0")] @@ -190,11 +195,14 @@ where /// Owned data. #[stable(feature = "rust1", since = "1.0.0")] - Owned(#[stable(feature = "rust1", since = "1.0.0")] <B as ToOwned>::Owned), + Owned(#[stable(feature = "rust1", since = "1.0.0")] <B as ToOwned<COOP_PREFERRED>>::Owned), } #[stable(feature = "rust1", since = "1.0.0")] -impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> { +impl<B: ?Sized + ToOwned<COOP_PREFERRED>, const COOP_PREFERRED: bool> Clone for Cow<'_, B, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ fn clone(&self) -> Self { match *self { Borrowed(b) => Borrowed(b), @@ -213,7 +221,10 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> { } } -impl<B: ?Sized + ToOwned> Cow<'_, B> { +impl<B: ?Sized + ToOwned<COOP_PREFERRED>, const COOP_PREFERRED: bool> Cow<'_, B, COOP_PREFERRED> +where +[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Returns true if the data is borrowed, i.e. if `to_mut` would require additional work. /// /// # Examples @@ -275,7 +286,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> { /// ); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned { + pub fn to_mut(&mut self) -> &mut <B as ToOwned<COOP_PREFERRED>>::Owned { match *self { Borrowed(borrowed) => { *self = Owned(borrowed.to_owned()); @@ -323,7 +334,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> { /// ); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn into_owned(self) -> <B as ToOwned>::Owned { + pub fn into_owned(self) -> <B as ToOwned<COOP_PREFERRED>>::Owned { match self { Borrowed(borrowed) => borrowed.to_owned(), Owned(owned) => owned, @@ -385,9 +396,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<B: ?Sized> fmt::Debug for Cow<'_, B> +impl<B: ?Sized, const COOP_PREFERRED: bool> fmt::Debug for Cow<'_, B, COOP_PREFERRED> where - B: fmt::Debug + ToOwned<Owned: fmt::Debug>, + B: fmt::Debug + ToOwned<COOP_PREFERRED, Owned: fmt::Debug>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { @@ -398,9 +410,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<B: ?Sized> fmt::Display for Cow<'_, B> +impl<B: ?Sized, const COOP_PREFERRED: bool> fmt::Display for Cow<'_, B, COOP_PREFERRED> where - B: fmt::Display + ToOwned<Owned: fmt::Display>, + B: fmt::Display + ToOwned<COOP_PREFERRED, Owned: fmt::Display>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { @@ -411,13 +424,14 @@ where } #[stable(feature = "default", since = "1.11.0")] -impl<B: ?Sized> Default for Cow<'_, B> +impl<B: ?Sized, const COOP_PREFERRED: bool> Default for Cow<'_, B, COOP_PREFERRED> where - B: ToOwned<Owned: Default>, + B: ToOwned<COOP_PREFERRED, Owned: Default>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { /// Creates an owned Cow<'a, B> with the default value for the contained owned value. fn default() -> Self { - Owned(<B as ToOwned>::Owned::default()) + Owned(<B as ToOwned<COOP_PREFERRED>>::Owned::default()) } } diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index b800adb2fed9a..4c2d796f1a42d 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -1102,7 +1102,7 @@ impl CStr { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[stable(feature = "cstr_to_str", since = "1.4.0")] - pub fn to_string_lossy(&self) -> Cow<'_, str> { + pub fn to_string_lossy(&self) -> Cow<'_, str, false> { // false = no need for co-alloc metadata, since it would get lost once converted to the slice. String::<false>::from_utf8_lossy(self.to_bytes()) } diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 342ad846ab99e..e3484a6a2801a 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -215,13 +215,13 @@ where type Owned = String<COOP_PREFERRED>; #[inline] fn to_owned(&self) -> String<COOP_PREFERRED> { - unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) } + unsafe { String::<COOP_PREFERRED>::from_utf8_unchecked(self.as_bytes().to_owned()) } } fn clone_into(&self, target: &mut String<COOP_PREFERRED>) { let mut b = mem::take(target).into_bytes(); self.as_bytes().clone_into(&mut b); - *target = unsafe { String::from_utf8_unchecked(b) } + *target = unsafe { String::<COOP_PREFERRED>::from_utf8_unchecked(b) } } } diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 6bb75be207d6f..a3b49395d8509 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -646,7 +646,7 @@ where #[must_use] #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] - pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> { + pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> { let mut iter = Utf8Chunks::new(v); let first_valid = if let Some(chunk) = iter.next() { @@ -662,7 +662,7 @@ where const REPLACEMENT: &str = "\u{FFFD}"; - let mut res = String::<false>::with_capacity(v.len()); + let mut res = String::<COOP_PREFERRED>::with_capacity(v.len()); res.push_str(first_valid); res.push_str(REPLACEMENT); @@ -2132,7 +2132,7 @@ impl<'a, const COOP_PREFERRED: bool> FromIterator<Cow<'a, str>> for String<COOP_ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { - fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String<COOP_PREFERRED> { + fn from_iter<I: IntoIterator<Item = Cow<'a, str, COOP_PREFERRED>>>(iter: I) -> String<COOP_PREFERRED> { let mut iterator = iter.into_iter(); // Because we're iterating over CoWs, we can (potentially) avoid at least @@ -2960,7 +2960,7 @@ where /// let owned: String = String::from(cow); /// assert_eq!(&owned[..], "eggplant"); /// ``` - fn from(s: Cow<'a, str>) -> String<COOP_PREFERRED> { + fn from(s: Cow<'a, str, COOP_PREFERRED>) -> String<COOP_PREFERRED> { s.into_owned() } } @@ -3007,7 +3007,7 @@ where /// /// [`Owned`]: crate::borrow::Cow::Owned "borrow::Cow::Owned" #[inline] - fn from(s: String<COOP_PREFERRED>) -> Cow<'a, str> { + fn from(s: String<COOP_PREFERRED>) -> Cow<'a, str, COOP_PREFERRED> { Cow::Owned(s) } } @@ -3032,7 +3032,7 @@ where /// /// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed" #[inline] - fn from(s: &'a String<COOP_PREFERRED>) -> Cow<'a, str> { + fn from(s: &'a String<COOP_PREFERRED>) -> Cow<'a, str, COOP_PREFERRED> { Cow::Borrowed(s.as_str()) } } @@ -3059,7 +3059,7 @@ impl<'a, const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for Co where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { - fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>(it: I) -> Cow<'a, str> { + fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>(it: I) -> Cow<'a, str, COOP_PREFERRED> { Cow::Owned(FromIterator::from_iter(it)) } } From 4206d09b1bf6571f9f388fb5fd734f4420e9af63 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 12 Jan 2023 14:02:44 -0800 Subject: [PATCH 39/47] Adding COOP_PREFERRED to Cow, ToOwned... --- library/alloc/src/borrow.rs | 67 +++++++++++++++++++++++----------- library/alloc/src/ffi/c_str.rs | 58 ++++++++++++++++++++--------- library/alloc/src/slice.rs | 16 +++++--- library/alloc/src/str.rs | 10 ++++- library/alloc/src/string.rs | 4 +- library/alloc/tests/string.rs | 5 ++- library/std/src/ffi/os_str.rs | 5 ++- library/std/src/path.rs | 5 ++- 8 files changed, 117 insertions(+), 53 deletions(-) diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index d420ca81e2a7c..67eac3205a651 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -17,6 +17,9 @@ use crate::string::String; use Cow::*; +// @FIXME check if used & needed +pub(crate) const BORROW_COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED!(); + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, B: ?Sized, const COOP_PREFERRED: bool> Borrow<B> for Cow<'a, B, COOP_PREFERRED> where @@ -344,9 +347,10 @@ where #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_deref", issue = "88955")] -impl<B: ?Sized + ToOwned> const Deref for Cow<'_, B> +impl<B: ?Sized + ToOwned<COOP_PREFERRED>, const COOP_PREFERRED: bool> const Deref for Cow<'_, B, COOP_PREFERRED> where B::Owned: ~const Borrow<B>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { type Target = B; @@ -359,12 +363,18 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<B: ?Sized> Eq for Cow<'_, B> where B: Eq + ToOwned {} +impl<B: ?Sized, const COOP_PREFERRED: bool> Eq for Cow<'_, B, COOP_PREFERRED> +where + B: Eq + ToOwned<COOP_PREFERRED>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + + {} #[stable(feature = "rust1", since = "1.0.0")] -impl<B: ?Sized> Ord for Cow<'_, B> +impl<B: ?Sized, const COOP_PREFERRED: bool> Ord for Cow<'_, B, COOP_PREFERRED> where - B: Ord + ToOwned, + B: Ord + ToOwned<COOP_PREFERRED>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn cmp(&self, other: &Self) -> Ordering { @@ -373,24 +383,26 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq<Cow<'b, C>> for Cow<'a, B> +impl<'a, 'b, B: ?Sized, C: ?Sized, const COOP_PREFERRED: bool> PartialEq<Cow<'b, C, COOP_PREFERRED>> for Cow<'a, B, COOP_PREFERRED> where - B: PartialEq<C> + ToOwned, - C: ToOwned, + B: PartialEq<C> + ToOwned<COOP_PREFERRED>, + C: ToOwned<COOP_PREFERRED>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] - fn eq(&self, other: &Cow<'b, C>) -> bool { + fn eq(&self, other: &Cow<'b, C, COOP_PREFERRED>) -> bool { PartialEq::eq(&**self, &**other) } } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> +impl<'a, B: ?Sized, const COOP_PREFERRED: bool> PartialOrd for Cow<'a, B, COOP_PREFERRED> where - B: PartialOrd + ToOwned, + B: PartialOrd + ToOwned<COOP_PREFERRED>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] - fn partial_cmp(&self, other: &Cow<'a, B>) -> Option<Ordering> { + fn partial_cmp(&self, other: &Cow<'a, B, COOP_PREFERRED>) -> Option<Ordering> { PartialOrd::partial_cmp(&**self, &**other) } } @@ -436,9 +448,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<B: ?Sized> Hash for Cow<'_, B> +impl<B: ?Sized, const COOP_PREFERRED: bool> Hash for Cow<'_, B, COOP_PREFERRED> where - B: Hash + ToOwned, + B: Hash + ToOwned<COOP_PREFERRED>, + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { @@ -447,7 +460,9 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized + ToOwned> AsRef<T> for Cow<'_, T> { +impl<T: ?Sized + ToOwned<COOP_PREFERRED>, const COOP_PREFERRED: bool> AsRef<T> for Cow<'_, T, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ fn as_ref(&self) -> &T { self } @@ -455,8 +470,10 @@ impl<T: ?Sized + ToOwned> AsRef<T> for Cow<'_, T> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_add", since = "1.14.0")] -impl<'a> Add<&'a str> for Cow<'a, str> { - type Output = Cow<'a, str>; +impl<'a, const COOP_PREFERRED: bool> Add<&'a str> for Cow<'a, str, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + type Output = Cow<'a, str, COOP_PREFERRED>; #[inline] fn add(mut self, rhs: &'a str) -> Self::Output { @@ -467,11 +484,13 @@ impl<'a> Add<&'a str> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_add", since = "1.14.0")] -impl<'a> Add<Cow<'a, str>> for Cow<'a, str> { - type Output = Cow<'a, str>; +impl<'a, const COOP_PREFERRED: bool> Add<Cow<'a, str, COOP_PREFERRED>> for Cow<'a, str, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + type Output = Cow<'a, str, COOP_PREFERRED>; #[inline] - fn add(mut self, rhs: Cow<'a, str>) -> Self::Output { + fn add(mut self, rhs: Cow<'a, str, COOP_PREFERRED>) -> Self::Output { self += rhs; self } @@ -479,7 +498,9 @@ impl<'a> Add<Cow<'a, str>> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_add", since = "1.14.0")] -impl<'a> AddAssign<&'a str> for Cow<'a, str> { +impl<'a, const COOP_PREFERRED: bool> AddAssign<&'a str> for Cow<'a, str, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ fn add_assign(&mut self, rhs: &'a str) { if self.is_empty() { *self = Cow::Borrowed(rhs) @@ -496,8 +517,10 @@ impl<'a> AddAssign<&'a str> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_add", since = "1.14.0")] -impl<'a> AddAssign<Cow<'a, str>> for Cow<'a, str> { - fn add_assign(&mut self, rhs: Cow<'a, str>) { +impl<'a, const COOP_PREFERRED: bool> AddAssign<Cow<'a, str, COOP_PREFERRED>> for Cow<'a, str, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + fn add_assign(&mut self, rhs: Cow<'a, str, COOP_PREFERRED>) { if self.is_empty() { *self = rhs } else if !rhs.is_empty() { diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 4c2d796f1a42d..28bc011241931 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -496,7 +496,10 @@ impl CString { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "cstring_into", since = "1.7.0")] - pub fn into_bytes(self) -> Vec<u8> { + pub fn into_bytes<const COOP_PREFERRED: bool>(self) -> Vec<u8, Global, COOP_PREFERRED> + where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + { let mut vec = into_vec(self.into_inner()); let _nul = vec.pop(); debug_assert_eq!(_nul, Some(0u8)); @@ -726,12 +729,14 @@ impl fmt::Debug for CString { #[stable(feature = "cstring_into", since = "1.7.0")] #[allow(unused_braces)] -impl From<CString> for Vec<u8, Global, {DEFAULT_COOP_PREFERRED!()}> { +impl<const COOP_PREFERRED: bool> From<CString> for Vec<u8, Global, COOP_PREFERRED> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Converts a [`CString`] into a <code>[Vec]<[u8]></code>. /// /// The conversion consumes the [`CString`], and removes the terminating NUL byte. #[inline] - fn from(s: CString) -> Vec<u8> { + fn from(s: CString) -> Vec<u8, Global, COOP_PREFERRED> { s.into_bytes() } } @@ -741,7 +746,8 @@ impl Default for CString { /// Creates an empty `CString`. fn default() -> CString { let a: &CStr = Default::default(); - a.to_owned() + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + <CStr as ToOwned<false>>::to_owned(a) } } @@ -754,11 +760,13 @@ impl Borrow<CStr> for CString { } #[stable(feature = "cstring_from_cow_cstr", since = "1.28.0")] -impl<'a> From<Cow<'a, CStr>> for CString { +impl<'a, const COOP_PREFERRED: bool> From<Cow<'a, CStr, COOP_PREFERRED>> for CString +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Converts a `Cow<'a, CStr>` into a `CString`, by copying the contents if they are /// borrowed. #[inline] - fn from(s: Cow<'a, CStr>) -> Self { + fn from(s: Cow<'a, CStr, COOP_PREFERRED>) -> Self { s.into_owned() } } @@ -775,11 +783,13 @@ impl From<&CStr> for Box<CStr> { } #[stable(feature = "box_from_cow", since = "1.45.0")] -impl From<Cow<'_, CStr>> for Box<CStr> { +impl<const COOP_PREFERRED: bool> From<Cow<'_, CStr, COOP_PREFERRED>> for Box<CStr> +where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Converts a `Cow<'a, CStr>` into a `Box<CStr>`, /// by copying the contents if they are borrowed. #[inline] - fn from(cow: Cow<'_, CStr>) -> Box<CStr> { + fn from(cow: Cow<'_, CStr, COOP_PREFERRED>) -> Box<CStr> { match cow { Cow::Borrowed(s) => Box::from(s), Cow::Owned(s) => Box::from(s), @@ -838,28 +848,33 @@ impl From<CString> for Box<CStr> { } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From<CString> for Cow<'a, CStr> { +#[allow(unused_braces)] +impl<'a> From<CString> for Cow<'a, CStr, { DEFAULT_COOP_PREFERRED!() }> { /// Converts a [`CString`] into an owned [`Cow`] without copying or allocating. #[inline] - fn from(s: CString) -> Cow<'a, CStr> { + fn from(s: CString) -> Cow<'a, CStr, { DEFAULT_COOP_PREFERRED!() }> { Cow::Owned(s) } } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From<&'a CStr> for Cow<'a, CStr> { +#[allow(unused_braces)] +impl<'a> From<&'a CStr> for Cow<'a, CStr, { DEFAULT_COOP_PREFERRED!() }> { /// Converts a [`CStr`] into a borrowed [`Cow`] without copying or allocating. #[inline] - fn from(s: &'a CStr) -> Cow<'a, CStr> { + fn from(s: &'a CStr) -> Cow<'a, CStr, { DEFAULT_COOP_PREFERRED!() }> { Cow::Borrowed(s) } } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From<&'a CString> for Cow<'a, CStr> { +impl<'a, const COOP_PREFERRED: bool> From<&'a CString> for Cow<'a, CStr, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Converts a `&`[`CString`] into a borrowed [`Cow`] without copying or allocating. #[inline] - fn from(s: &'a CString) -> Cow<'a, CStr> { + fn from(s: &'a CString) -> Cow<'a, CStr, COOP_PREFERRED> { Cow::Borrowed(s.as_c_str()) } } @@ -1017,7 +1032,11 @@ impl fmt::Display for IntoStringError { } #[stable(feature = "cstr_borrow", since = "1.3.0")] -impl ToOwned for CStr { +// @FIXME try remove COOP_PREFERRED and have ToOwned<_> +impl<const COOP_PREFERRED: bool> ToOwned<COOP_PREFERRED> for CStr +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ type Owned = CString; fn to_owned(&self) -> CString { @@ -1034,7 +1053,8 @@ impl ToOwned for CStr { #[stable(feature = "cstring_asref", since = "1.7.0")] impl From<&CStr> for CString { fn from(s: &CStr) -> CString { - s.to_owned() + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + <CStr as ToOwned<false>>::to_owned(s) } } @@ -1102,9 +1122,11 @@ impl CStr { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[stable(feature = "cstr_to_str", since = "1.4.0")] - pub fn to_string_lossy(&self) -> Cow<'_, str, false> { + pub fn to_string_lossy<const COOP_PREFERRED: bool>(&self) -> Cow<'_, str, COOP_PREFERRED> + where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + { // false = no need for co-alloc metadata, since it would get lost once converted to the slice. - String::<false>::from_utf8_lossy(self.to_bytes()) + String::<COOP_PREFERRED>::from_utf8_lossy(self.to_bytes()) } /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating. diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 71149f88165e7..6f0c6a52b6dc8 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -852,22 +852,26 @@ where } } -// COOP_NOT_POSSIBLE #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<T: Clone> ToOwned for [T] { - type Owned = Vec<T>; +impl<T: Clone, const COOP_PREFERRED: bool> ToOwned<COOP_PREFERRED> for [T] +where +[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>( + COOP_PREFERRED, +)]:, +{ + type Owned = Vec<T, Global, COOP_PREFERRED>; #[cfg(not(test))] - fn to_owned(&self) -> Vec<T> { + fn to_owned(&self) -> Vec<T, Global, COOP_PREFERRED> { self.to_vec() } #[cfg(test)] - fn to_owned(&self) -> Vec<T> { + fn to_owned(&self) -> Vec<T, Global, COOP_PREFERRED> { hack::to_vec(self, Global) } - fn clone_into(&self, target: &mut Vec<T>) { + fn clone_into(&self, target: &mut Vec<T, Global, COOP_PREFERRED>) { // drop anything in target that will not be overwritten target.truncate(self.len()); diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index e3484a6a2801a..06fa07eefa9be 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -191,7 +191,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl Borrow<str> for String { +impl<const COOP_PREFERRED: bool> Borrow<str> for String<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[inline] fn borrow(&self) -> &str { &self[..] @@ -199,7 +202,10 @@ impl Borrow<str> for String { } #[stable(feature = "string_borrow_mut", since = "1.36.0")] -impl BorrowMut<str> for String { +impl<const COOP_PREFERRED: bool> BorrowMut<str> for String<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[inline] fn borrow_mut(&mut self) -> &mut str { &mut self[..] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index a3b49395d8509..b9f6d34d2ed05 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -3014,7 +3014,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_from_string_ref", since = "1.28.0")] -impl<'a, const COOP_PREFERRED: bool> From<&'a String<COOP_PREFERRED>> for Cow<'a, str> +impl<'a, const COOP_PREFERRED: bool> From<&'a String<COOP_PREFERRED>> for Cow<'a, str, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { @@ -3055,7 +3055,7 @@ impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_str_from_iter", since = "1.12.0")] -impl<'a, const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for Cow<'a, str> +impl<'a, const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for Cow<'a, str, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { diff --git a/library/alloc/tests/string.rs b/library/alloc/tests/string.rs index 99d1296a4c925..4adbef3b21332 100644 --- a/library/alloc/tests/string.rs +++ b/library/alloc/tests/string.rs @@ -15,7 +15,10 @@ where fn into_cow(self) -> Cow<'a, B>; } -impl<'a> IntoCow<'a, str> for String { +impl<'a, const COOP_PREFERRED: bool> IntoCow<'a, str> for String<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ fn into_cow(self) -> Cow<'a, str> { Cow::Owned(self) } diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 80ed34157e6dc..a0da25a75da4a 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -1324,7 +1324,10 @@ impl AsRef<OsStr> for str { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<OsStr> for String { +impl<const COOP_PREFERRED: bool> AsRef<OsStr> for String<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[inline] fn as_ref(&self) -> &OsStr { (&**self).as_ref() diff --git a/library/std/src/path.rs b/library/std/src/path.rs index c3593264e520b..90112d66ec1a4 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -3141,7 +3141,10 @@ impl AsRef<Path> for str { } #[stable(feature = "rust1", since = "1.0.0")] -impl AsRef<Path> for String { +impl<const COOP_PREFERRED: bool> AsRef<Path> for String<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[inline] fn as_ref(&self) -> &Path { Path::new(self) From f5d5b8a23f6dd4bb942edb1b1612aafb3b38f16f Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 12 Jan 2023 16:02:32 -0800 Subject: [PATCH 40/47] Adding COOP_PREFERRED to Cow, ToOwned, String. --- library/alloc/src/string.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b9f6d34d2ed05..9d284a7ad05a5 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2128,7 +2128,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "herd_cows", since = "1.19.0")] -impl<'a, const COOP_PREFERRED: bool> FromIterator<Cow<'a, str>> for String<COOP_PREFERRED> +impl<'a, const COOP_PREFERRED: bool> FromIterator<Cow<'a, str, COOP_PREFERRED>> for String<COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { @@ -2238,16 +2238,16 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "herd_cows", since = "1.19.0")] -impl<'a, const COOP_PREFERRED: bool> Extend<Cow<'a, str>> for String<COOP_PREFERRED> +impl<'a, const COOP_PREFERRED: bool> Extend<Cow<'a, str, COOP_PREFERRED>> for String<COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { - fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) { + fn extend<I: IntoIterator<Item = Cow<'a, str, COOP_PREFERRED>>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); } #[inline] - fn extend_one(&mut self, s: Cow<'a, str>) { + fn extend_one(&mut self, s: Cow<'a, str, COOP_PREFERRED>) { self.push_str(&s); } } @@ -2789,7 +2789,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_str_to_string_specialization", since = "1.17.0")] -impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for Cow<'_, str> +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for Cow<'_, str, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { @@ -2940,7 +2940,7 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "string_from_cow_str", since = "1.14.0")] -impl<'a, const COOP_PREFERRED: bool> From<Cow<'a, str>> for String<COOP_PREFERRED> +impl<'a, const COOP_PREFERRED: bool> From<Cow<'a, str, COOP_PREFERRED>> for String<COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { @@ -2967,7 +2967,10 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a str> for Cow<'a, str> { +impl<'a, const COOP_PREFERRED: bool> From<&'a str> for Cow<'a, str, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Converts a string slice into a [`Borrowed`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -2981,14 +2984,14 @@ impl<'a> From<&'a str> for Cow<'a, str> { /// /// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed" #[inline] - fn from(s: &'a str) -> Cow<'a, str> { + fn from(s: &'a str) -> Cow<'a, str, COOP_PREFERRED> { Cow::Borrowed(s) } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Cow<'a, str> +impl<'a, const COOP_PREFERRED: bool> From<String<COOP_PREFERRED>> for Cow<'a, str, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { @@ -3039,16 +3042,22 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_str_from_iter", since = "1.12.0")] -impl<'a> FromIterator<char> for Cow<'a, str> { - fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> { +impl<'a, const COOP_PREFERRED: bool> FromIterator<char> for Cow<'a, str, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str, COOP_PREFERRED> { Cow::Owned(FromIterator::from_iter(it)) } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_str_from_iter", since = "1.12.0")] -impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> { - fn from_iter<I: IntoIterator<Item = &'b str>>(it: I) -> Cow<'a, str> { +impl<'a, 'b, const COOP_PREFERRED: bool> FromIterator<&'b str> for Cow<'a, str, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + fn from_iter<I: IntoIterator<Item = &'b str>>(it: I) -> Cow<'a, str, COOP_PREFERRED> { Cow::Owned(FromIterator::from_iter(it)) } } From 023bdd1107e975780ded90f7eea8174e43926678 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 12 Jan 2023 16:18:58 -0800 Subject: [PATCH 41/47] Adding COOP_PREFERRED to Cow, ToOwned.. --- library/alloc/src/boxed.rs | 10 ++++++---- library/alloc/src/collections/vec_deque/mod.rs | 1 - library/alloc/src/vec/into_iter.rs | 8 +++++++- library/alloc/src/vec/mod.rs | 4 ---- library/alloc/src/vec/splice.rs | 1 - 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 32277063ac0b0..3806d42973c86 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -780,10 +780,11 @@ where #[allow(unused_braces)] pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:, { unsafe { - RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_in(len, alloc) + RawVec::<T, A, false>::with_capacity_in(len, alloc) .into_box(len) } } @@ -815,10 +816,11 @@ where #[allow(unused_braces)] pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, + // false = no need for co-alloc metadata, since it would get lost once converted to Box. + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:, { unsafe { - RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_zeroed_in( + RawVec::<T, A, false>::with_capacity_zeroed_in( len, alloc, ) .into_box(len) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 34f82acb166d4..3a64f52ffb925 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -1406,7 +1406,6 @@ where pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A, COOP_PREFERRED> where R: RangeBounds<usize>, - [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, { // Memory safety // diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 2ec439748f68a..f4d5676f01e55 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -431,7 +431,13 @@ where { #[cfg(not(test))] fn clone(&self) -> Self { - self.as_slice().to_vec_in::<A, COOP_PREFERRED>(self.alloc.deref().clone()).into_iter() + // @FIXME Remove the following extras - used for type checks only + let slice = self.as_slice(); + let vec: crate::vec::Vec<T, A, COOP_PREFERRED> = slice.to_vec_in::<A, COOP_PREFERRED>(self.alloc.deref().clone()); + let _iter: IntoIter<T, A, COOP_PREFERRED> = vec.into_iter(); + + //self.as_slice().to_vec_in::<A, COOP_PREFERRED>(self.alloc.deref().clone()).into_iter() + loop {} } #[cfg(test)] fn clone(&self) -> Self { diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index b5b5836ee2101..22721680e7522 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2065,8 +2065,6 @@ where pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A, COOP_PREFERRED> where R: RangeBounds<usize>, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, - { // Memory safety // @@ -2893,8 +2891,6 @@ where impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIterator for Vec<T, A, COOP_PREFERRED> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:, - { type Item = T; type IntoIter = IntoIter<T, A, COOP_PREFERRED>; diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index 96ca11edaf6a1..92fcaf4382dca 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -26,7 +26,6 @@ pub struct Splice< const COOP_PREFERRED: bool = false > where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:, - //FIXME: If using SHORT_TERM_VEC_PREFERS_COOP!() instead of true, and the same for `drain` below, then this failed with: `derive` cannot be used on item swith type macros { pub(super) drain: Drain<'a, I::Item, A, COOP_PREFERRED>, pub(super) replace_with: I, From 05374786b3185eef16c34fa2e284250147a0f9de Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 12 Jan 2023 17:35:21 -0800 Subject: [PATCH 42/47] CoVec, PlVec, DefVec, WeVec. Cleanup. --- library/alloc/src/borrow.rs | 3 --- library/alloc/src/vec/mod.rs | 9 +++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index 67eac3205a651..b733651c09721 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -17,9 +17,6 @@ use crate::string::String; use Cow::*; -// @FIXME check if used & needed -pub(crate) const BORROW_COOP_PREFERRED: bool = DEFAULT_COOP_PREFERRED!(); - #[stable(feature = "rust1", since = "1.0.0")] impl<'a, B: ?Sized, const COOP_PREFERRED: bool> Borrow<B> for Cow<'a, B, COOP_PREFERRED> where diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 22721680e7522..97d1845171937 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -419,14 +419,15 @@ pub struct Vec< len: usize, } +/// "Cooperative" Vector. Preferring co-alloc API (if Global alloc supports it). #[unstable(feature = "global_co_alloc_covec", issue = "none")] -pub type CoVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = +pub type CoVec<T, A = Global> = Vec<T, A, true>; /// "Plain" Vec. Not "cooperative" - not carrying extra data to assist the allocator. /// FIXME after cleanup, see if we still use this in core:: and/or alloc:: #[unstable(feature = "global_co_alloc_plvec", issue = "none")] -pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = +pub type PlVec<T, A = Global> = Vec<T, A, false>; /// "Default" Vec. Either "cooperative" or not - as specified by `DEFAULT_COOP_PREFERRED`. The @@ -434,14 +435,14 @@ pub type PlVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: All /// author considered using `CoVec` or `PlVec`, but left it to default instead. #[unstable(feature = "global_co_alloc_defvec", issue = "none")] #[allow(unused_braces)] -pub type DefVec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> = +pub type DefVec<T, A = Global> = Vec<T, A, {DEFAULT_COOP_PREFERRED!()}>; /// "Weighted cooperative" Vec. Weight means how much it wants to cooperate (with the allocator). 0 /// = always pack; u8::MAX = always cooperate (if `Global` supports it). /// @FIXME A `pub const` threshold. #[unstable(feature = "global_co_alloc_vec", issue = "none")] -pub type WeVec<T, const weight: u8> = Vec<T, Global, { weight > 127 }>; +pub type WeVec<T, const WEIGHT: u8> = Vec<T, Global, { WEIGHT > 127 }>; //////////////////////////////////////////////////////////////////////////////// // Inherent methods From 0a4d9a52c088b6643f359d2961c8f36233822da8 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 12 Jan 2023 23:14:49 -0800 Subject: [PATCH 43/47] COOP_PREFERRED for String and Cow of CStr etc. --- library/alloc/src/ffi/c_str.rs | 2 +- library/alloc/src/string.rs | 74 +++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 28bc011241931..d7a0d19f130ae 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -1126,7 +1126,7 @@ impl CStr { where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { // false = no need for co-alloc metadata, since it would get lost once converted to the slice. - String::<COOP_PREFERRED>::from_utf8_lossy(self.to_bytes()) + String::<COOP_PREFERRED>::from_utf8_lossy_coop_or_not(self.to_bytes()) } /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating. diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 9d284a7ad05a5..fdab7f73133bb 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -594,6 +594,41 @@ where } } + /// Like `from_utf8_lossy`, but this honors COOP_PREFERRED. + #[unstable(feature = "compile_check_struct_with_const_generic_added_associated_function", reason = "confirm_or_fix_the_function_name", issue = "none")] + pub fn from_utf8_lossy_coop_or_not(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> { + let mut iter = Utf8Chunks::new(v); + + let first_valid = if let Some(chunk) = iter.next() { + let valid = chunk.valid(); + if chunk.invalid().is_empty() { + debug_assert_eq!(valid.len(), v.len()); + return Cow::Borrowed(valid); + } + valid + } else { + return Cow::Borrowed(""); + }; + + const REPLACEMENT: &str = "\u{FFFD}"; + + #[allow(unused_braces)] + let mut res = String::<COOP_PREFERRED>::with_capacity(v.len()); + res.push_str(first_valid); + res.push_str(REPLACEMENT); + + for chunk in iter { + res.push_str(chunk.valid()); + if !chunk.invalid().is_empty() { + res.push_str(REPLACEMENT); + } + } + + Cow::Owned(res) + } +} + +impl String { /// Converts a slice of bytes to a string, including invalid characters. /// /// Strings are made of bytes ([`u8`]), and a slice of bytes @@ -643,39 +678,22 @@ where /// /// assert_eq!("Hello �World", output); /// ``` + /// @FIXME Should this return Cow<'_, str, false> instead, so it's both + /// - backwards compatible, and + /// - independent of DEFAULT_COOP_PREFERRED (for backwards compatbility)? But `String` (and others) use default, too. #[must_use] #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] - pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> { - let mut iter = Utf8Chunks::new(v); - - let first_valid = if let Some(chunk) = iter.next() { - let valid = chunk.valid(); - if chunk.invalid().is_empty() { - debug_assert_eq!(valid.len(), v.len()); - return Cow::Borrowed(valid); - } - valid - } else { - return Cow::Borrowed(""); - }; - - const REPLACEMENT: &str = "\u{FFFD}"; - - let mut res = String::<COOP_PREFERRED>::with_capacity(v.len()); - res.push_str(first_valid); - res.push_str(REPLACEMENT); - - for chunk in iter { - res.push_str(chunk.valid()); - if !chunk.invalid().is_empty() { - res.push_str(REPLACEMENT); - } - } - - Cow::Owned(res) + #[allow(unused_braces)] + pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str, {DEFAULT_COOP_PREFERRED!()}> { + String::<{DEFAULT_COOP_PREFERRED!()}>::from_utf8_lossy_coop_or_not(v) } +} +impl<const COOP_PREFERRED: bool> String<COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ /// Decode a UTF-16–encoded vector `v` into a `String`, returning [`Err`] /// if `v` contains any invalid data. /// From aa30184ada8ed427798fd8a5b2626364f0c09554 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Thu, 12 Jan 2023 23:16:40 -0800 Subject: [PATCH 44/47] Uncommenting assert of BorrowType::TRAVERSAL_PERMIT. FIXME: Undo once compilable. --- library/alloc/src/collections/btree/node.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/collections/btree/node.rs b/library/alloc/src/collections/btree/node.rs index 6912466448fab..ec616fdf79fcf 100644 --- a/library/alloc/src/collections/btree/node.rs +++ b/library/alloc/src/collections/btree/node.rs @@ -319,7 +319,8 @@ impl<BorrowType: marker::BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> self, ) -> Result<Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::Edge>, Self> { const { - assert!(BorrowType::TRAVERSAL_PERMIT); + //@FIXME uncomment once compilable + //assert!(BorrowType::TRAVERSAL_PERMIT); } // We need to use raw pointers to nodes because, if BorrowType is marker::ValMut, @@ -1007,7 +1008,8 @@ impl<BorrowType: marker::BorrowType, K, V> /// both, upon success, do nothing. pub fn descend(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> { const { - assert!(BorrowType::TRAVERSAL_PERMIT); + // @FIXME uncomment once compilable + //assert!(BorrowType::TRAVERSAL_PERMIT); } // We need to use raw pointers to nodes because, if BorrowType is From 301d090118692686a87b766155613108ae6adc74 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Tue, 17 Jan 2023 11:06:41 -0800 Subject: [PATCH 45/47] Renamed String::from_utf8_lossy_coop_or_not(..) to from_utf8_lossy_co(..). Added Vec::new_co() --- library/alloc/src/ffi/c_str.rs | 2 +- library/alloc/src/macros.rs | 2 +- library/alloc/src/string.rs | 8 ++--- library/alloc/src/vec/mod.rs | 30 +++++++++++++------ library/alloc/src/vec/spec_from_iter.rs | 2 +- .../alloc/src/vec/spec_from_iter_nested.rs | 2 +- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index d7a0d19f130ae..e3344cbbf98f9 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -1126,7 +1126,7 @@ impl CStr { where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { // false = no need for co-alloc metadata, since it would get lost once converted to the slice. - String::<COOP_PREFERRED>::from_utf8_lossy_coop_or_not(self.to_bytes()) + String::<COOP_PREFERRED>::from_utf8_lossy_co(self.to_bytes()) } /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating. diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index 5198bf297d925..c2f1e9df41c11 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -41,7 +41,7 @@ #[allow_internal_unstable(rustc_attrs, liballoc_internals)] macro_rules! vec { () => ( - $crate::__rust_force_expr!($crate::vec::Vec::new()) + $crate::__rust_force_expr!($crate::vec::Vec::new_co()) ); ($elem:expr; $n:expr) => ( $crate::__rust_force_expr!($crate::vec::from_elem($elem, $n)) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index fdab7f73133bb..b445845e110fc 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -468,7 +468,7 @@ where #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub const fn new() -> String<COOP_PREFERRED> { - String { vec: Vec::new() } + String { vec: Vec::new_co() } } /// Creates a new empty `String` with at least the specified capacity. @@ -595,8 +595,8 @@ where } /// Like `from_utf8_lossy`, but this honors COOP_PREFERRED. - #[unstable(feature = "compile_check_struct_with_const_generic_added_associated_function", reason = "confirm_or_fix_the_function_name", issue = "none")] - pub fn from_utf8_lossy_coop_or_not(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> { + #[unstable(feature = "string_from_utf8_lossy_co", reason = "confirm_or_fix_the_function_name", issue = "none")] + pub fn from_utf8_lossy_co(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> { let mut iter = Utf8Chunks::new(v); let first_valid = if let Some(chunk) = iter.next() { @@ -686,7 +686,7 @@ impl String { #[stable(feature = "rust1", since = "1.0.0")] #[allow(unused_braces)] pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str, {DEFAULT_COOP_PREFERRED!()}> { - String::<{DEFAULT_COOP_PREFERRED!()}>::from_utf8_lossy_coop_or_not(v) + String::<{DEFAULT_COOP_PREFERRED!()}>::from_utf8_lossy_co(v) } } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 97d1845171937..6abf1d820ec1a 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -444,14 +444,7 @@ pub type DefVec<T, A = Global> = #[unstable(feature = "global_co_alloc_vec", issue = "none")] pub type WeVec<T, const WEIGHT: u8> = Vec<T, Global, { WEIGHT > 127 }>; -//////////////////////////////////////////////////////////////////////////////// -// Inherent methods -//////////////////////////////////////////////////////////////////////////////// - -impl<T, const COOP_PREFERRED: bool> Vec<T, Global, COOP_PREFERRED> -where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, -{ +impl<T> Vec<T> { /// Constructs a new, empty `Vec<T>`. /// /// The vector will not allocate until elements are pushed onto it. @@ -467,6 +460,25 @@ where #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub const fn new() -> Self { + #[allow(unused_braces)] + Vec::<T, Global, {DEFAULT_COOP_PREFERRED!()}>::new_co() + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Inherent methods +//////////////////////////////////////////////////////////////////////////////// + +impl<T, const COOP_PREFERRED: bool> Vec<T, Global, COOP_PREFERRED> +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ + /// Like new(), but it respects COOP_PREFERRED. + #[inline] + #[rustc_const_stable(feature = "const_vec_new_co", since = "1.60.0")] //@FIXME This is `rustc_const_stable`, so that String::new() can be const and can call this. + #[unstable(feature = "vec_new_co", reason = "confirm_or_fix_the_function_name", issue = "none")] + #[must_use] + pub const fn new_co() -> Self { Vec { buf: RawVec::NEW, len: 0 } } @@ -3242,7 +3254,7 @@ where /// /// The vector will not allocate until elements are pushed onto it. fn default() -> Vec<T, Global, COOP_PREFERRED> { - Vec::new() + Vec::new_co() } } diff --git a/library/alloc/src/vec/spec_from_iter.rs b/library/alloc/src/vec/spec_from_iter.rs index 6f414082ab292..e0befdb22962d 100644 --- a/library/alloc/src/vec/spec_from_iter.rs +++ b/library/alloc/src/vec/spec_from_iter.rs @@ -63,7 +63,7 @@ where } } - let mut vec = Vec::<T, Global, COOP_PREFERRED>::new(); + let mut vec = Vec::<T, Global, COOP_PREFERRED>::new_co(); // must delegate to spec_extend() since extend() itself delegates // to spec_from for empty Vecs vec.spec_extend(iterator); diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index 3ccd2da20df49..ff774e4534bf7 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -29,7 +29,7 @@ where // vector being full in the few subsequent loop iterations. // So we get better branch prediction. let mut vector = match iterator.next() { - None => return Vec::new(), + None => return Vec::new_co(), Some(element) => { let (lower, _) = iterator.size_hint(); let initial_capacity = From 9e7e06a54a35a38593d6a249f368ee2dacf60c19 Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Fri, 20 Jan 2023 22:19:33 -0800 Subject: [PATCH 46/47] Minor fixes --- library/std/src/ffi/os_str.rs | 1 + library/std/src/io/impls.rs | 6 +++--- library/std/src/lib.rs | 4 ++++ library/std/src/path.rs | 3 ++- library/std/src/sys/unix/thread_local_dtor.rs | 5 +---- library/std/src/sys_common/thread_local_dtor.rs | 1 - 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index a0da25a75da4a..75693df5053af 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -1,6 +1,7 @@ #[cfg(test)] mod tests; +use crate::alloc::Global; use crate::borrow::{Borrow, Cow}; use crate::cmp; use crate::collections::TryReserveError; diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index 8671ebf5c8141..64efd793b8c20 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests; -use crate::alloc::Allocator; +use crate::alloc::{Allocator, self}; use crate::cmp; use crate::collections::VecDeque; use crate::fmt; @@ -419,7 +419,7 @@ where #[stable(feature = "vecdeque_read_write", since = "1.63.0")] impl<A: Allocator, const _COOP_PREFERRED: bool> Read for VecDeque<u8, A, _COOP_PREFERRED> where - [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]:, { /// Fill `buf` with the contents of the "front" slice as returned by /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are @@ -446,7 +446,7 @@ where #[stable(feature = "vecdeque_read_write", since = "1.63.0")] impl<A: Allocator, const _COOP_PREFERRED: bool> Write for VecDeque<u8, A, _COOP_PREFERRED> where - [(); co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(_COOP_PREFERRED)]:, { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result<usize> { diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index a7e13f5b866b5..4d6c5e0968cdc 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -214,6 +214,10 @@ #![needs_panic_runtime] // // Lints: +#![allow(incomplete_features)] +#![feature(generic_const_exprs)] +#![feature(global_co_alloc)] +#![feature(global_co_alloc_plvec)] #![warn(deprecated_in_future)] #![warn(missing_docs)] #![warn(missing_debug_implementations)] diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 90112d66ec1a4..f39d912970aca 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -70,6 +70,7 @@ #[cfg(test)] mod tests; +use crate::alloc::{Global, self}; use crate::borrow::{Borrow, Cow}; use crate::cmp; use crate::collections::TryReserveError; @@ -3143,7 +3144,7 @@ impl AsRef<Path> for str { #[stable(feature = "rust1", since = "1.0.0")] impl<const COOP_PREFERRED: bool> AsRef<Path> for String<COOP_PREFERRED> where - [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, + [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { #[inline] fn as_ref(&self) -> &Path { diff --git a/library/std/src/sys/unix/thread_local_dtor.rs b/library/std/src/sys/unix/thread_local_dtor.rs index 4549f5d77742c..157e09359bd61 100644 --- a/library/std/src/sys/unix/thread_local_dtor.rs +++ b/library/std/src/sys/unix/thread_local_dtor.rs @@ -1,12 +1,9 @@ #![cfg(target_thread_local)] #![unstable(feature = "thread_local_internals", issue = "none")] -#![feature(global_co_alloc_plvec)] //! Provides thread-local destructors without an associated "key", which //! can be more efficient. -use core::alloc::PlVec; - // Since what appears to be glibc 2.18 this symbol has been shipped which // GCC and clang both use to invoke destructors in thread_local globals, so // let's do the same! @@ -69,7 +66,7 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { REGISTERED.set(true); } - type List = PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>; + type List = alloc::vec::PlVec<(*mut u8, unsafe extern "C" fn(*mut u8))>; #[thread_local] static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut()); diff --git a/library/std/src/sys_common/thread_local_dtor.rs b/library/std/src/sys_common/thread_local_dtor.rs index 0914dc2639cc3..21cd817d2d8f6 100644 --- a/library/std/src/sys_common/thread_local_dtor.rs +++ b/library/std/src/sys_common/thread_local_dtor.rs @@ -12,7 +12,6 @@ #![unstable(feature = "thread_local_internals", issue = "none")] #![allow(dead_code)] -#![feature(global_co_alloc_plvec)] use crate::ptr; use crate::sys_common::thread_local_key::StaticKey; From 2089cb6447b1fccc74403e1c4fc134cb8e4bb16e Mon Sep 17 00:00:00 2001 From: Peter Kehl <peter.kehl@gmail.com> Date: Sat, 21 Jan 2023 11:09:11 -0800 Subject: [PATCH 47/47] Added ToString::RESULT - an associated type with a default. --- library/alloc/src/lib.rs | 1 + library/alloc/src/string.rs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 8e74717255203..b41f5045069bd 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -191,6 +191,7 @@ // - cannot specialize on predicate `[(); _] well-formed` // - cannot specialize on predicate `the constant `core::alloc::co_alloc_metadata_num_slots::<A>()` can be evaluated` //#![feature(min_specialization)] +#![feature(associated_type_defaults)] #![feature(specialization)] #![feature(negative_impls)] #![feature(never_type)] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b445845e110fc..ae95207c0fd0b 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2682,6 +2682,10 @@ pub trait ToString<const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}> where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, { + /// NOT for public use. Do not override. This exists to woraround (current) limitations of const generic defaults. + #[unstable(feature = "global_co_alloc", issue = "none")] + type RESULT = String<COOP_PREFERRED>; + /// Converts the given value to a `String`. /// /// # Examples @@ -2696,7 +2700,7 @@ where /// ``` #[rustc_conversion_suggestion] #[stable(feature = "rust1", since = "1.0.0")] - fn to_string(&self) -> String<COOP_PREFERRED>; + fn to_string(&self) -> Self::RESULT; } /// # Panics @@ -2728,18 +2732,24 @@ where #[cfg(not(no_global_oom_handling))] #[stable(feature = "char_to_string_specialization", since = "1.46.0")] -impl ToString for char { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for char +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[inline] - fn to_string(&self) -> String { + fn to_string(&self) -> String<COOP_PREFERRED> { String::from(self.encode_utf8(&mut [0; 4])) } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "bool_to_string_specialization", since = "CURRENT_RUSTC_VERSION")] -impl ToString for bool { +impl<const COOP_PREFERRED: bool> ToString<COOP_PREFERRED> for bool +where + [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:, +{ #[inline] - fn to_string(&self) -> String { + fn to_string(&self) -> String<COOP_PREFERRED> { String::from(if *self { "true" } else { "false" }) } } @@ -3291,7 +3301,7 @@ where /// assert_eq!("a", &s[..]); /// ``` #[inline] - fn from(c: char) -> Self { + fn from(c: char) -> String<COOP_PREFERRED> { c.to_string() } }