Skip to content

stabilize Box::new_uninit_in and Box::new_zeroed_in - #166148

Open
folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:stabilize-box-uninit-zeroed-in
Open

folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:stabilize-box-uninit-zeroed-in

Conversation

@folkertdev

@folkertdev folkertdev commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

tracking issue: #163177

Splits out Box::new_uninit_in and Box::new_zeroed_in from allocator_ext and stabilizes them.

Public API

impl<T, A: Allocator> Box<T, A> {
    pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> { /* ... */ }
    pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> { /* ... */ }
}

These are the custom allocator equivalents of the stable Box::new_uninit and Box::new_zeroed functions.

There are equivalent functions for Rc and Arc, but there even new_in is still unstable.

There are also the Box::new_{uninit, zeroed}_slice_in methods, but I believe those are a bit more controversial because they add another axis of constructor function. We may soon have the equivalent

Vec::repeat_in(0u8, 32 * 1024 * 1024, allo).into_boxed_slice()
FallibleVec::repeat_in(0u8, 32 * 1024 * 1024, alloc).into_boxed_slice()

Implementation history

Some relevant PRs

@folkertdev folkertdev added the A-allocators Area: Custom and system allocators label Nov 18, 2026
@rustbot rustbot added A-box Area: Our favorite opsem complication S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 18, 2026
@folkertdev folkertdev added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. I-libs-nominated Nominated for discussion during a libs team meeting. labels Nov 18, 2026
@folkertdev
folkertdev marked this pull request as ready for review November 18, 2026 13:05
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 18, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 18, 2026
@rustbot

rustbot commented Nov 18, 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: libs
  • libs expanded to 38 candidates
  • Random selection from ChrisDenton, JohnTitor, Mark-Simulacrum, clarfonthey, maxdexh

@clarfonthey

Copy link
Copy Markdown
Contributor

r=me pending FCP:

@rfcbot merge libs

@rust-rfcbot

rust-rfcbot commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

@clarfonthey has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. and removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Nov 18, 2026
@clarfonthey clarfonthey removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Nov 18, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

(Removing nomination since this is just more allocator API stabilisations. I think these are fine to merge since we've decided how we want to do this, and while the Box constructor problem is still an issue, we shouldn't delay stabilising these because of that.)

@folkertdev

Copy link
Copy Markdown
Contributor Author

Box constructor problem

By this you mean the exponential explosion of method names for each flavor of allocating a Box (zeroed, uninit, slice, custom allocator, etc.)?

@clarfonthey

Copy link
Copy Markdown
Contributor

Correct. It's not ideal, but it is what we currently have, and it would be ideal if we could at least make these use cases possible even if the end result is a lot of methods.

@the8472

the8472 commented Nov 18, 2026

Copy link
Copy Markdown
Member

Hrrm... and then someone comes and asks for the try_ variants? In my mind fallible, zeroed and uninit are all fairly fundamental properties, none really "lesser" than the others.
And that's the problem with the method proliferation.

So can you make an argument why these and not those?

@clarfonthey

Copy link
Copy Markdown
Contributor

To this question, I point you toward integers and their combinatorical explosion of methods.

@the8472

the8472 commented Nov 18, 2026

Copy link
Copy Markdown
Member

But then you're not really arguing "let's add only these", you're arguing for stabilizing these with an argument that generalizes to more methods... which in turn is basically saying we're not going to solve that problem and stabilize most of the methods instead.
Which is a choice, but I think it should be made explicitly and not by salami-slicing it.

@clarfonthey

Copy link
Copy Markdown
Contributor

Salami-slicing is probably the most German phrase you could have chosen here, but yes, you are right.

I think that the Box constructor problem is an issue and I think that, at least for the case of the allocator APIs, we should not solve it. Which is to say, we should be okay adding in extra _in methods for existing constructors even if that doesn't mean that we ultimately add, for example, try_ variants.

@folkertdev

Copy link
Copy Markdown
Contributor Author

Well for the fallible versions there is the current idea of FallibleVec. We could have FallibleBox, but there are way fewer Box methods that allocate. Given that new_in is already stable, and new_uninit and new_zeroed are, I think the only logical option here is to also add these two methods.

For try_ and (to a lesser extend) _slice we can make a different choice.

@hanna-kruppe

hanna-kruppe commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

The other idea that sometimes floats around for reducing the combinatorial explosion is to have types like BoxBuilder etc. that has O(n) methods for the n different concerns instead of O(c^n) methods up to try_uninit_slice_in_inplace_please_with_spare_capacity. Did that idea ever go anywhere?

@workingjubilee

Copy link
Copy Markdown
Member

German? I thought salami was an Italian word?

If we want to be Deutsch about it we'd say we don't want to chop up this problem any wurst.

@tgross35

Copy link
Copy Markdown
Member

The other idea that sometimes floats around for reducing the combinatorial explosion is to have types like BoxBuilder etc. that has O(n) methods for the n different concerns instead of O(c^n) methods up to try_uninit_slice_in_inplace_please_with_spare_capacity. Did that idea ever go anywhere?

There's rust-lang/wg-allocators#90 which has a rough shape sketched at the end. I think there has been openness to an experiment, just needs somebody to actually do it.

This branch has not been deployed

No deployments
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 A-box Area: Our favorite opsem complication disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. 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.

8 participants