smol-rs / futures-lite

Futures, streams, and async I/O combinators.
Apache License 2.0
449 stars 26 forks source link

Add `buffer_mut` method to `BufWriter` #61

Closed comblock closed 1 year ago

comblock commented 1 year ago

This can be useful sometimes when trying to reduce the amount of copies. My specific use case is to use the underlying buffer in a https://docs.rs/inout/0.1.2/inout/struct.InOutBuf.html for encryption. So the unencrypted data would get encrypted and immediately written to the internal buffer, which is one less copy than if I encrypted it in place or to another buffer and then wrote it to the buffer

comblock commented 1 year ago

This crate largely aims to mirror the libstd version of these interfaces. BufWriter (and BufReader for that matter) do not provide this interface. While this may be because they don't actually use Vec<u8> to store data, there may be some soundness or correctness reason for not being able to mutate the data. Maybe it'd be better as an &mut [u8]?

If I'm not mistaken, you cannot write more data to a &mut [u8], only replace what is already there, so I would need it to be &mut Vec<u8>. I also looked into the code of BufWriter a bit more and I'm not seeing any reason why it would be unsound for the user to mutate the data in the buffer.

notgull commented 1 year ago

Closing as per discussion here: https://discordapp.com/channels/701824908866617385/701824908866617388/1035597283988873277