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 trait for MaybeUninit<u8> #94665

Closed
wants to merge 1 commit into from

Conversation

SUPERCILEX
Copy link
Contributor

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

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

This comment has been minimized.

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

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
Member

m-ou-se commented Mar 9, 2022

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-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 Mar 9, 2022
@SUPERCILEX
Copy link
Contributor Author

Would it make sense to implement Write for ReadBuf then?

@JohnCSimon
Copy link
Member

@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
Contributor Author

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

@JohnCSimon
Copy link
Member

@m-ou-se ☝️ ?

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

bors commented Jun 9, 2022

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

@JohnCSimon
Copy link
Member

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

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

@JohnCSimon JohnCSimon 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
@JohnCSimon JohnCSimon 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
Contributor Author

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

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

@SUPERCILEX SUPERCILEX closed this Sep 30, 2022
@SUPERCILEX SUPERCILEX deleted the maybeuninit-write branch September 30, 2022 04:17
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 Relevant 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