Skip to content

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

Merged
bors merged 1 commit into
rust-lang:masterfrom
Amanieu:maybeuninit_bytes
Jan 20, 2022
Merged

Add MaybeUninit::(slice_)as_bytes(_mut)#89747
bors merged 1 commit into
rust-lang:masterfrom
Amanieu:maybeuninit_bytes

Conversation

@Amanieu

Copy link
Copy Markdown
Member

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>{pubfnas_bytes(&self) -> &[MaybeUninit<u8>];pubfnas_bytes_mut(&mutself) -> &mut[MaybeUninit<u8>];pubfnslice_as_bytes(this:&[MaybeUninit<T>]) -> &[MaybeUninit<u8>];pubfnslice_as_bytes_mut(this:&mut[MaybeUninit<T>]) -> &mut[MaybeUninit<u8>];}

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @m-ou-se

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

@rust-highfiverust-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
Copy Markdown
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.

@the8472the8472 added the T-libs-api [DEPRECATED; DO NOT USE] label Oct 11, 2021
@Amanieu

Copy link
Copy Markdown
MemberAuthor

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.

@JohnCSimonJohnCSimon 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
@JohnCSimonJohnCSimon 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
Copy Markdown
MemberAuthor

ping @m-ou-se

@m-ou-se

Copy link
Copy Markdown
Member

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

r=me with the tracking issue number added.

@m-ou-sem-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
Copy Markdown
MemberAuthor

@bors r=m-ou-se

@bors

bors commented Jan 19, 2022

Copy link
Copy Markdown
Collaborator

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

@borsbors 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:masterJan 20, 2022
@rustbotrustbot added this to the 1.60.0 milestone Jan 20, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@Amanieu@rust-highfive@rust-log-analyzer@joshtriplett@m-ou-se@bors@the8472@JohnCSimon@rustbot