Skip to content

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

Open
kpreid wants to merge 1 commit into
rust-lang:mainfrom
kpreid:constalloc
Open

Make Global and System allocators unstably implement const Clone + const Default.#159471
kpreid wants to merge 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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants