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

Add MaybeUninit::(slice_)as_bytes(_mut) #89747

Merged
merged 1 commit into from
Jan 20, 2022

Conversation

Amanieu
Copy link
Member

@Amanieu Amanieu commented Oct 10, 2021

This adds methods to convert between MaybeUninit<T> and a slice of MaybeUninit<u8>. This is safe since MaybeUninit<u8> can correctly handle padding bytes in any T.

These methods are added:

impl<T> MaybeUninit<T> {
	pub fn as_bytes(&self) -> &[MaybeUninit<u8>];
	pub fn as_bytes_mut(&mut self) -> &mut [MaybeUninit<u8>];
	pub fn slice_as_bytes(this: &[MaybeUninit<T>]) -> &[MaybeUninit<u8>];
	pub fn slice_as_bytes_mut(this: &mut [MaybeUninit<T>]) -> &mut [MaybeUninit<u8>];
}

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(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 Oct 10, 2021
@rust-log-analyzer

This comment has been minimized.

@joshtriplett
Copy link
Member

This seems related to some of the questions that have come up in the safe transmute work.

For the mut versions, this allows writing arbitrary data to a value or slice. That data still won't become usable until a call to assume_init, which is unsafe, so this isn't unsound, but as far as I know, doing something like that would previously have required mem::transmute.

In either case, this would make it very easy to rely on the internal layout of a type, which a crate may not have intended to make public. So at the very least, I think we need a comment noting that the ability to access the bytes of private fields or the exact layout of a structure does not necessarily make those details part of a crate's stable ABI.

I don't have any objections to these methods existing; I just want to make sure we note (and where appropriate document) what new capabilities these methods expose, and the potential hazards associated with them.

@the8472 the8472 added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 11, 2021
@Amanieu
Copy link
Member Author

Amanieu commented Oct 27, 2021

The main use case I have for these methods is reading bytes from a file or memory and turning them into structured types (which I have previously checked to be valid for all bit patterns).

I don't think this exposes anything that wasn't already possible with or without unsafe code.

@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
@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 Dec 5, 2021
@Amanieu
Copy link
Member Author

Amanieu commented Jan 19, 2022

ping @m-ou-se

@m-ou-se
Copy link
Member

m-ou-se commented Jan 19, 2022

Looks good to me. Can you open a tracking issue?

r=me with the tracking issue number added.

@m-ou-se m-ou-se 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 Jan 19, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Amanieu
Copy link
Member Author

Amanieu commented Jan 19, 2022

@bors r=m-ou-se

@bors
Copy link
Contributor

bors commented Jan 19, 2022

📌 Commit 5c96dcf has been approved by m-ou-se

@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 Jan 19, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 20, 2022
…askrgr

Rollup of 13 pull requests

Successful merges:

 - rust-lang#89747 (Add MaybeUninit::(slice_)as_bytes(_mut))
 - rust-lang#89764 (Fix variant index / discriminant confusion in uninhabited enum branching)
 - rust-lang#91606 (Stabilize `-Z print-link-args` as `--print link-args`)
 - rust-lang#91694 (rustdoc: decouple stability and const-stability)
 - rust-lang#92183 (Point at correct argument when async fn output type lifetime disagrees with signature)
 - rust-lang#92582 (improve `_` constants in item signature handling)
 - rust-lang#92680 (intra-doc: Use the impl's assoc item where possible)
 - rust-lang#92704 (Change lint message to be stronger for &T -> &mut T transmute)
 - rust-lang#92861 (Rustdoc mobile: put out-of-band info on its own line)
 - rust-lang#92992 (Help optimize out backtraces when disabled)
 - rust-lang#93038 (Fix star handling in block doc comments)
 - rust-lang#93108 (:arrow_up: rust-analyzer)
 - rust-lang#93112 (Fix CVE-2022-21658)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 98cb338 into rust-lang:master Jan 20, 2022
@rustbot rustbot added this to the 1.60.0 milestone Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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