Add io::Read::read_le and io::Read::read_be#156983
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
These functions make it easy to read a fixed-size type as little-endian or big-endian. They're trivial wrappers around the combination of `io::Read::read_array` and `T::from_le_bytes`/`T::from_be_bytes`. The implementation uses a sealed trait `FromEndianBytes`. That trait is currently in `std` and accepts a `&mut io::Read`. Once we can use associated consts in the types of method parameters, we can change this trait to have `from_le_bytes` and `from_be_bytes` methods, move it to `core`, and make it public.
|
Just a warning: My experience from having methods similar to these in the bytes crate is that you will receive endless requests to add more and more variations of them. Just look at how many methods |
|
@Darksonn I'm hoping that having generic methods (such that we don't need a separate method per return type) will avoid the combinatorial explosion. We also don't need the |
|
Is there any other discussions that might be relevant to link about this? I figure there isn't an ACP, but I saw that a bunch of RustWeek meetings seemed to have involved various documents people made, and wonder if there's an associated one for this. |
|
Yes, the trait does help somewhat. |
These functions make it easy to read a fixed-size type as little-endian
or big-endian. They're trivial wrappers around the combination of
io::Read::read_arrayandT::from_le_bytes/T::from_be_bytes.The implementation uses a sealed trait
FromEndianBytes. That trait iscurrently in
stdand accepts a&mut io::Read. Once we can useassociated consts in the types of method parameters, we can change this
trait to have
from_le_bytesandfrom_be_bytesmethods, move it tocore, and make it public.As discussed at RustWeek.