-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add Write
implementation for [MaybeUninit<u8>]
#142849
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
base: master
Are you sure you want to change the base?
Conversation
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There’s `Write` implement for slice of bytes so it’s not unreasonable to also have one for slice of `MaybeUninit` bytes. This makes dealing with uninitialised memory slightly easier in some cases (though it sadly doesn’t eliminate use of `unsafe` once the data is written) and makes it possible to use existing APIs which `Write` data (e.g. `serde_json::to_writer`) to initialise memory.
The job Click to see the possible cause of the failure (guessed by this bot)
|
This will likely need an ACP before an implementation PR can be reviewed. r? libs-api |
Note that there is a subtle soundness requirement for doing either of those things: you have to trust that the writing code actually initialized as many bytes as the updated fn skip_writes(w: &mut &mut [MaybeUninit<u8>]) -> io::Result<()> {
*w = &mut w[w.len()..];
Ok(())
} It’s a little harder to do the same in a function that just |
Could you expand on the motivation for this change. Specifically, could you give a code example where this is used and you recover the number of bytes actually written to the buffer. We discussed this in the @rust-lang/libs-api meeting and it seems like this use case may perhaps be better serve with some sort of cursor API. |
The length is recovered the same way it’s recovered when writing to
Why doesn’t that argument apply to
|
Now try to write the code that calls Note that std already has a safe cursor API for writing to uninitialized buffers, |
Yes. Using MaybeUninit often involves unsafe blocks. That’s not a gotcha.
It’s also slower.
Yes, I’m keenly aware of it and there’s a reason I call it Rust’s worst feature. Using |
It’s not about unsafe being needed at all, it’s about how you can encapsulate the unsafe to make its soundness depends on as little trusted code as possible. |
There is some topical discussion going on at #t-libs > Future of read_buf about the design at https://docs.rs/buffer-trait/latest/buffer_trait/, which seems to resolve a few of these issues. |
There’s
Write
implement for slice of bytes so it’s not unreasonable to also have one for slice ofMaybeUninit
bytes.This makes dealing with uninitialised memory slightly easier in some cases (though it sadly doesn’t eliminate use of
unsafe
once the data is written) and makes it possible to use existing APIs whichWrite
data (e.g.serde_json::to_writer
) to initialise memory.