Skip to content
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 and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement write() method for Box<MaybeUninit<T>> #88906

Closed
wants to merge 1 commit into from

Conversation

Kixunil
Copy link
Contributor

@Kixunil Kixunil commented Sep 13, 2021

This adds method similar to MaybeUninit::write main difference being
it returns owned Box. This can be used to elide copy from stack
safely, however it's not currently tested that the optimization actually
occurs.

Analogous methods are not provided for Rc and Arc as those need to
handle the possibility of sharing. Some version of them may be added in
the future.

This was discussed in #63291 which this change extends.

@rust-highfive
Copy link
Collaborator

r? @dtolnay

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 13, 2021
@rust-log-analyzer

This comment has been minimized.

@Kixunil Kixunil force-pushed the box-maybe-uninit-write branch from 005cd4b to d0c2ccf Compare September 13, 2021 13:58
@rust-log-analyzer

This comment has been minimized.

@Kixunil Kixunil force-pushed the box-maybe-uninit-write branch from d0c2ccf to 322b67d Compare September 13, 2021 14:06
@rust-log-analyzer

This comment has been minimized.

@Kixunil Kixunil force-pushed the box-maybe-uninit-write branch from 322b67d to f747ae4 Compare September 13, 2021 14:42
@the8472
Copy link
Member

the8472 commented Sep 13, 2021

There's a pending RFC that proposes another approach which would probably make this one redundant.

rust-lang/rfcs#2884

@Kixunil
Copy link
Contributor Author

Kixunil commented Sep 14, 2021

@the8472 thanks for heads up! It could be argued it also makes #6321 redundant (although that one can be useful in FFI but then why not add this one for completeness?)

@the8472 the8472 added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Sep 21, 2021
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 9, 2021
@JohnCSimon
Copy link
Member

Ping from triage:
@Kixunil What is the status of this PR? Are you still interested in moving forward?

@Kixunil
Copy link
Contributor Author

Kixunil commented Oct 25, 2021

Sure! This is waiting on reviewer. I believe this change is simple and obvious.

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 16, 2021
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

I believe this change is simple and obvious.

To point out why this change isn't simple and obvious: this is a breaking change. It can make previously working code fail to compile, or keep compiling but do the wrong thing, or silently introduce UB into existing correct code.

Here is an example of an ordinary idiomatic use of MaybeUninit which would no longer work:

struct Thing {
    buf: Box<MaybeUninit<[u8; N]>>,
}

impl Thing {
    fn method(&mut self) {
        let _arr = self.buf.write([0; N]);
    }
}

@Kixunil
Copy link
Contributor Author

Kixunil commented Nov 26, 2021

Ah, I see, good point. I think changing it to fn write(this: Self) would fix it, right? Will try to do it when I have time (shouldn't be more than couple of days).

@dtolnay dtolnay 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 Nov 26, 2021
@bors
Copy link
Contributor

bors commented Nov 28, 2021

☔ The latest upstream changes (presumably #91230) made this pull request unmergeable. Please resolve the merge conflicts.

@Kixunil Kixunil force-pushed the box-maybe-uninit-write branch 2 times, most recently from 9bdca22 to 928a0b0 Compare December 2, 2021 16:13
@Kixunil
Copy link
Contributor Author

Kixunil commented Dec 2, 2021

Fixed and rebased. Also noticed that wording in the example looked like a promise of optimization which is not actually guaranteed, so I reworded it accordingly.

@rust-log-analyzer

This comment has been minimized.

This adds method similar to `MaybeUninit::write` main difference being
it returns owned `Box`. This can be used to elide copy from stack
safely, however it's not currently tested that the optimization actually
occurs.

Analogous methods are not provided for `Rc` and `Arc` as those need to
handle the possibility of sharing. Some version of them may be added in
the future.

This was discussed in rust-lang#63291 which this change extends.
@Kixunil Kixunil force-pushed the box-maybe-uninit-write branch from 928a0b0 to 41e21aa Compare December 2, 2021 16:18
@Kixunil Kixunil requested a review from dtolnay December 2, 2021 17:54
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

@dtolnay
Copy link
Member

dtolnay commented Dec 3, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Dec 3, 2021

📌 Commit 41e21aa has been approved by dtolnay

@bors bors 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 3, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 3, 2021
…tolnay

Implement write() method for Box<MaybeUninit<T>>

This adds method similar to `MaybeUninit::write` main difference being
it returns owned `Box`. This can be used to elide copy from stack
safely, however it's not currently tested that the optimization actually
occurs.

Analogous methods are not provided for `Rc` and `Arc` as those need to
handle the possibility of sharing. Some version of them may be added in
the future.

This was discussed in rust-lang#63291 which this change extends.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 3, 2021
…tolnay

Implement write() method for Box<MaybeUninit<T>>

This adds method similar to `MaybeUninit::write` main difference being
it returns owned `Box`. This can be used to elide copy from stack
safely, however it's not currently tested that the optimization actually
occurs.

Analogous methods are not provided for `Rc` and `Arc` as those need to
handle the possibility of sharing. Some version of them may be added in
the future.

This was discussed in rust-lang#63291 which this change extends.
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 3, 2021
…askrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#88906 (Implement write() method for Box<MaybeUninit<T>>)
 - rust-lang#90269 (Make `Option::expect` unstably const)
 - rust-lang#90854 (Type can be unsized and uninhabited)
 - rust-lang#91170 (rustdoc: preload fonts)
 - rust-lang#91273 (Fix ICE rust-lang#91268 by checking that the snippet ends with a `)`)
 - rust-lang#91381 (Android: -ldl must appear after -lgcc when linking)
 - rust-lang#91453 (Document Windows TLS drop behaviour)
 - rust-lang#91462 (Use try_normalize_erasing_regions in needs_drop)
 - rust-lang#91474 (suppress warning about set_errno being unused on DragonFly)
 - rust-lang#91483 (Sync rustfmt subtree)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
Copy link
Contributor

bors commented Dec 3, 2021

☔ The latest upstream changes (presumably #91486) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 3, 2021
@Kixunil
Copy link
Contributor Author

Kixunil commented Dec 3, 2021

I'm confused by this - it conflicted with an unmerged PR? Is this some fluke or am I actually supposed to do something (what?) about it?

@pietroalbini
Copy link
Member

This has been merged into master by bors (in #91486), but GitHub didn't detect it for whatever reason. Closing.

@Kixunil Kixunil deleted the box-maybe-uninit-write branch December 3, 2021 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants