Skip to content

Make Global and System allocators unstably implement const Clone + const Default.#159471

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
kpreid:constalloc
Jul 21, 2026
Merged

Make Global and System allocators unstably implement const Clone + const Default.#159471
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
kpreid:constalloc

Conversation

@kpreid

@kpreid kpreid commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Allocators implementing const Default is useful for generically constructing empty collections inside of constants:

struct Foo<A: Allocator>(Vec<i32, A>);
impl<A: Allocator + const Default> Foo<A> {
    pub const EMPTY: Self = Self(Vec::new_in(A::default()));
}

const Clone is not strictly necessary to permit such constants to exist, but may make more complex code easier to write (e.g. so a const constructor can accept one allocator and clone it).

@rustbot label +A-allocators

…Default`.

This is useful for generically constructing empty collections:

    struct Foo<A: Allocator>(Vec<i32, A>);
    impl<A: Allocator + const Default> Foo<A> {
        pub const EMPTY: Self = Self(Vec::new_in(A::default()));
    }
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 17, 2026
@rustbot

rustbot commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

r? @clarfonthey

rustbot has assigned @clarfonthey.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 6 candidates

@rustbot rustbot added the A-allocators Area: Custom and system allocators label Jul 17, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

Based upon the ruling in #155816 I'm tempted to just close this. You can already construct both of these in const code because they're just ZSTs with public constructors, and even though adding const traits is easy, similarly, this means we can do that later.

@kpreid

kpreid commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

You can already construct both of these in const code because they're just ZSTs with public constructors

You can’t construct them in generic const code.

Well, can’t without defining your own ConstAllocatorDefault trait to substitute for Default entirely. I suppose that would work. I do think, though, that it would be particularly useful to have this impl for people experimenting with Allocator before its stabilization, because even if it’s not strictly necessary it’s a speedbump.

@clarfonthey

Copy link
Copy Markdown
Contributor

Right, so, I had missed the discussion on Zulip, and you're right these should be reasonable to add. Sorry for the confusion.

@bors r+ rollup

@clarfonthey

Copy link
Copy Markdown
Contributor

@bors r+ rollup

…?

@rust-bors

rust-bors Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 1c7eaf9 has been approved by clarfonthey

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 21, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 21, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - #155617 (Replace `jemalloc` bootstrap options with `override-allocator`)
 - #156225 (feat(num): improve error messages for `TryFromIntError`)
 - #159058 (implement #![feature(macroless_generic_const_args)])
 - #159435 (core: implement `Rng` for references)
 - #159471 (Make `Global` and `System` allocators unstably implement `const Clone + const Default`.)
 - #159590 (Remove some dead code)
 - #159636 (Improve some comments (and an error message))
@rust-bors
rust-bors Bot merged commit 66dc1b3 into rust-lang:main Jul 21, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 21, 2026
rust-timer added a commit that referenced this pull request Jul 21, 2026
Rollup merge of #159471 - kpreid:constalloc, r=clarfonthey

Make `Global` and `System` allocators unstably implement `const Clone + const Default`.

Allocators implementing `const Default` is useful for generically constructing empty collections inside of constants:

```rust
struct Foo<A: Allocator>(Vec<i32, A>);
impl<A: Allocator + const Default> Foo<A> {
    pub const EMPTY: Self = Self(Vec::new_in(A::default()));
}
```

`const Clone` is not strictly necessary to permit such constants to exist, but may make more complex code easier to write (e.g. so a const constructor can accept one allocator and clone it).

@rustbot label +A-allocators
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jul 23, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#155617 (Replace `jemalloc` bootstrap options with `override-allocator`)
 - rust-lang/rust#156225 (feat(num): improve error messages for `TryFromIntError`)
 - rust-lang/rust#159058 (implement #![feature(macroless_generic_const_args)])
 - rust-lang/rust#159435 (core: implement `Rng` for references)
 - rust-lang/rust#159471 (Make `Global` and `System` allocators unstably implement `const Clone + const Default`.)
 - rust-lang/rust#159590 (Remove some dead code)
 - rust-lang/rust#159636 (Improve some comments (and an error message))
@kpreid
kpreid deleted the constalloc branch July 23, 2026 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-allocators Area: Custom and system allocators S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants