Implement MaybeUninit::fill{,_cloned,_mut,_with,_from} by jmillikin · Pull Request #117426 · rust-lang/rust (original) (raw)

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation9 Commits1 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

jmillikin

@jmillikin

@rustbot

r? @joshtriplett

(rustbot has picked a reviewer for you, use r? to override)

@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 31, 2023

@jmillikin

@rustbot rustbot added T-libs-api

Relevant to the library API team, which will review and decide on the PR/issue.

and removed T-libs

Relevant to the library team, which will review and decide on the PR/issue.

labels

Oct 31, 2023

@jmillikin

The original ACP proposed adding fill<T: Clone>(&mut self, value: T) -> &mut [T] -- I made the following adjustments to this API:

The ACP's proposed fill_with<F: FnMut() -> T>(f: F) and fill_from<I: Iterator<Item = T>>(iter: I) were implemented as originally described.

I verified that MaybeUninit<u8>::fill() compiles to memset().

@jmillikin

@Amanieu

* Changed to return a non-mutable slice to avoid the accidental leaking discussed in [MaybeUninit::write_slice_cloned() makes it very easy to accidentally leak #80376](https://github.com/rust-lang/rust/issues/80376)

I disagree. I think this should always return &mut. I think introducing more API surface will only increase user confusion, and by working with MaybeUninit you already have to be aware of potentially leaking data. All the fill methods should return mutable slices.

* To accommodate the common case of filling with zero or a known bit pattern, I renamed the `Clone` fill method to `fill_cloned()` and added `fill()` / `fill_mut()` for `T: Copy` -- the mut slice return isn't dangerous since there's no dropping.

I'm less concerned about this: there is already fill_with for people who want precise control over how many clone calls are made. The discussion about T vs &T has already happened in #70758, and I would prefer just keeping the T version for consistency with slice::fill.

@bors

@Amanieu

@rustbot rustbot added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Feb 29, 2024

@Amanieu

ping @jmillikin, could you address the review feedback?

@jmillikin

Labels

S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

T-libs-api

Relevant to the library API team, which will review and decide on the PR/issue.