Skip to content

Implement Write trait for MaybeUninit<u8> - #94665

Closed
SUPERCILEX wants to merge 1 commit into
rust-lang:masterfrom
SUPERCILEX:maybeuninit-write
Closed

Implement Write trait for MaybeUninit<u8>#94665
SUPERCILEX wants to merge 1 commit into
rust-lang:masterfrom
SUPERCILEX:maybeuninit-write

Conversation

@SUPERCILEX

Copy link
Copy Markdown
Contributor

This PR makes it possible to write something like the following:

letmut buf = [MaybeUninit::<u8>::uninit();20];let written = {let slice = buf.as_mut_slice();write!(slice,"{}", u64_int).unwrap();
buf.len() - slice.len()};let buf:[u8] = unsafe{ mem::transmute(buf[..written])};

@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 Mar 6, 2022
@rust-log-analyzer

This comment has been minimized.

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
@SUPERCILEX

Copy link
Copy Markdown
ContributorAuthor

Fixed the benchmark and added one for MaybeUninit:

test io::impls::tests::bench_write_maybeuninit ... bench: 24 ns/iter (+/- 0)
test io::impls::tests::bench_write_slice ... bench: 25 ns/iter (+/- 6)
test io::impls::tests::bench_write_vec ... bench: 28 ns/iter (+/- 0)

@m-ou-se

Copy link
Copy Markdown
Member

I'm not sure if this is the right kind of API we want to provide for this. The guarantee that the written part is properly initialized and usable as &[u8] is not expressed through this API, and still requires the user to use unsafe code. An abstraction/type for partially initialized buffers is probably a lot nicer to use, and could be entirely safe to use.

@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 Mar 9, 2022
@SUPERCILEX

Copy link
Copy Markdown
ContributorAuthor

Would it make sense to implement Write for ReadBuf then?

@JohnCSimon

Copy link
Copy Markdown

@SUPERCILEX
Ping from triage: Can you please post the status of this PR? Maybe ping any commenters for additional feedback?
Thank you.

@SUPERCILEX

Copy link
Copy Markdown
ContributorAuthor

I'm waiting on a reply to my above comment.

@JohnCSimon

Copy link
Copy Markdown

@m-ou-se ☝️ ?

@apirainoapiraino added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 19, 2022
@bors

bors commented Jun 9, 2022

Copy link
Copy Markdown
Collaborator

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

@JohnCSimon

Copy link
Copy Markdown

I'm waiting on a reply to my above comment.

@m-ou-se - can you comment on this?

@JohnCSimonJohnCSimon 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 24, 2022
@JohnCSimonJohnCSimon 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 11, 2022
@SUPERCILEX

Copy link
Copy Markdown
ContributorAuthor

This has been implemented in #97015. You can now do this:

letmut buf = [MaybeUninit::<u8>::uninit();20];letmut buf:BorrowedBuf = buf.as_mut_slice().into();write!(buf.unfilled(),"{}",42).unwrap();let buf:&[u8] = buf.filled();

@SUPERCILEX
SUPERCILEX deleted the maybeuninit-write branch September 30, 2022 04:17
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-authorStatus: This is awaiting some action (such as code changes or more information) from the author.T-libsRelevant 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.

7 participants

@SUPERCILEX@rust-highfive@rust-log-analyzer@m-ou-se@JohnCSimon@bors@apiraino