Skip to content

alloc: Unstably add Vec::from_elem and Vec::from_elem_in - #164043

Open
Gelbpunkt wants to merge 6 commits into
rust-lang:mainfrom
Gelbpunkt:from-elem-in
Open

Gelbpunkt wants to merge 6 commits into
rust-lang:mainfrom
Gelbpunkt:from-elem-in

Conversation

@Gelbpunkt

@Gelbpunkt Gelbpunkt commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Tracking issue: #164215

These methods are of particular interest for users because they perform better due to specialization, e.g. when elem is zero. In that case, it will result in a call to calloc or equivalent, which is better than a call to malloc followed by memset.

We also forward the existing std::vec::from_elem to Vec::from_elem. The old module-level method std::vec::from_elem_in was unstable and unused, so I've removed it as per reviewer's request.

Zulip thread

r? @clarfonthey

These methods are of particular interest for users because they perform
better due to specialization, e.g. when `elem` is zero. In that case, it
will result in a call to `calloc` or equivalent, which is better than a
call to `malloc` followed by `memset`.

We also forward the existing `std::vec::from_elem` and
`std::vec::from_elem_in` to these new associated methods on `Vec`. The
module-level methods could potentially be removed in a follow-up.
@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 Oct 9, 2026
Comment thread library/alloc/src/vec/mod.rs Outdated
/// assert_eq!(vec, [0; 3]);
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "vec_from_elem", issue = "none")]

@Gelbpunkt Gelbpunkt Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part of an existing feature or should we create a new tracking issue for this? Also, should from_elem_in use the same feature or does it belong in allocator_ext?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put both of these into their own from_elem issue since I have a feeling allocator_ext will get stabilised first. But yes, should have one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a tracking issue: #164215

@folkertdev folkertdev added the F-abi_custom `#![feature(abi_custom)]` label Oct 9, 2026
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev added A-allocators Area: Custom and system allocators and removed F-abi_custom `#![feature(abi_custom)]` labels Oct 9, 2026
Comment thread library/alloc/src/vec/mod.rs Outdated
@clarfonthey

Copy link
Copy Markdown
Contributor

r? ChrisDenton

Offloading a few PRs, Chris should have enough context to handle this.

@rustbot rustbot assigned ChrisDenton and unassigned clarfonthey Oct 11, 2026
@rust-log-analyzer

This comment has been minimized.

@ChrisDenton

Copy link
Copy Markdown
Member

I wouldn't be willing to support this without an accepted ACP that details the motivation. It is basically a new specialization wrapper (while it existed before, it was only for internal use). So I'll pass on review.

r? libs

@nia-e

nia-e commented Oct 13, 2026

Copy link
Copy Markdown
Member

Yup, this is major enough to need discussion as an ACP. @Gelbpunkt could you file one?

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