From 1c7eaf95a0b78cecf843cb1adb8069699705190e Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Fri, 17 Jul 2026 14:42:20 -0700 Subject: [PATCH] Make `Global` and `System` allocators implement `const Clone + const Default`. This is useful for generically constructing empty collections: struct Foo(Vec); impl Foo { pub const EMPTY: Self = Self(Vec::new_in(A::default())); } --- library/alloc/src/alloc.rs | 3 ++- library/alloc/src/lib.rs | 1 + library/std/src/alloc.rs | 3 ++- library/std/src/lib.rs | 2 ++ 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 8729e98278a82..8560a644f207b 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -52,7 +52,8 @@ unsafe extern "Rust" { /// Note: while this type is unstable, the functionality it provides can be /// accessed through the [free functions in `alloc`](self#functions). #[unstable(feature = "allocator_api", issue = "32838")] -#[derive(Copy, Clone, Default, Debug)] +#[derive(Copy, Debug)] +#[derive_const(Clone, Default)] // the compiler needs to know when a Box uses the global allocator vs a custom one #[lang = "global_alloc_ty"] pub struct Global; diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 9f82f5be82a68..f355cefadeec5 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -122,6 +122,7 @@ #![feature(core_io_internals)] #![feature(deprecated_suggestion)] #![feature(deref_pure_trait)] +#![feature(derive_const)] #![feature(diagnostic_on_move)] #![feature(dispatch_from_dyn)] #![feature(ergonomic_clones)] diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 753ca5aba561c..84447c06aaecf 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -136,7 +136,8 @@ use crate::{hint, mem, ptr}; /// program opts in to using jemalloc as the global allocator, `System` will /// still allocate memory using `malloc` and `HeapAlloc`. #[stable(feature = "alloc_system_type", since = "1.28.0")] -#[derive(Debug, Default, Copy, Clone)] +#[derive(Copy, Debug)] +#[derive_const(Clone, Default)] pub struct System; impl System { diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 66d9c9da27180..514033a4aa7b0 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -327,6 +327,7 @@ #![feature(cast_maybe_uninit)] #![feature(char_internals)] #![feature(clone_to_uninit)] +#![feature(const_clone)] #![feature(const_convert)] #![feature(const_default)] #![feature(core_float_math)] @@ -336,6 +337,7 @@ #![feature(core_io_internals)] #![feature(cstr_display)] #![feature(cursor_split)] +#![feature(derive_const)] #![feature(drop_guard)] #![feature(duration_constants)] #![feature(error_generic_member_access)]