-
Notifications
You must be signed in to change notification settings - Fork 105
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
Add FromBytes
/IntoBytes
methods which read from/write to an io::Read
/io::Write
#158
Comments
The obvious name, IMO, for this is Since it's marked If this is the course we take, we should update its deprecation message to something like this:
|
Not sure what to call the |
A few considerations:
|
There's an oddity about |
IMO it's fine to provide the minimal API and let users manually construct fancier use cases. All of this can be built safely using our existing primitives anyway. |
Throwing out the option of |
It would be helpful to consider For example, I have this enum that represents compression: #[repr(u8)]
pub enum Compression {
Stored,
Zlib,
BZip2,
LZMA1,
LZMA2,
} Currently, I use let compression = reader.read_u8()?;
header.compression = Compression::from_repr(compression)
.ok_or_else(|| eyre!("Unexpected compression value: {compression}"))?; Now that we have let mut buf = [0; size_of::<Compression>()];
reader.read_exact(&mut buf)?;
Compression::try_read_from_bytes(&buf) This is now several lines every time I want to read an enum. It's difficult to make this generic as the below function fails to compile with fn enum_value<T: KnownLayout + TryFromBytes + Sized>(reader: &mut impl Read) -> Result<T> {
let mut buf = [0; size_of::<T>()];
reader.read_exact(&mut buf)?;
T::try_read_from_bytes(&buf)
} I've also considered a macro, which does work, but it's not syntactically as nice as it could be. macro_rules! enum_value {
($reader:expr, $ty:ty) => {{
let mut buf = [0; size_of::<$ty>()];
$reader.read_exact(&mut buf)?;
<$ty>::try_read_from_bytes(&buf)
}};
} I've considered using |
More prior art: The |
FromBytes
constructor which reads from an io::Read
FromBytes
/IntoBytes
methods which read from/write to an io::Read
/io::Write
Makes progress on google#158.
Makes progress on #158. Co-authored-by: Joshua Liebow-Feeser <[email protected]>
Makes progress on #158. Co-authored-by: Joshua Liebow-Feeser <[email protected]>
Makes progress on #158. Co-authored-by: Joshua Liebow-Feeser <[email protected]> gherrit-pr-id: I9253d6be7407d8d8679ee355e4e71cd9b15b9ff7
…2046) Makes progress on #158. gherrit-pr-id: I9253d6be7407d8d8679ee355e4e71cd9b15b9ff7 Co-authored-by: Jack Wrenn <[email protected]>
Progress
FromBytes::read_from_io
andIntoBytes::write_to_io
in 0.8IntoBytes::write_to
->write_to_bytes
(to be consistent withFromBytes::read_from_bytes
)FromBytes::read_from_io
->read_from
andIntoBytes::write_to_io
->write_to
Original text
Crosvm has a utility function called zerocopy_from_reader:
Maybe we should add something similar to
FromBytes
(and potentially a write analogue toAsBytes
)?The text was updated successfully, but these errors were encountered: