close
Skip to content

Add io::Read::read_le and io::Read::read_be#156983

Open
joshtriplett wants to merge 2 commits into
rust-lang:mainfrom
joshtriplett:read-le-be
Open

Add io::Read::read_le and io::Read::read_be#156983
joshtriplett wants to merge 2 commits into
rust-lang:mainfrom
joshtriplett:read-le-be

Conversation

@joshtriplett
Copy link
Copy Markdown
Member

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.


As discussed at RustWeek.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 26, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

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.
@Darksonn
Copy link
Copy Markdown
Member

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 Buf has at this point.

@joshtriplett
Copy link
Copy Markdown
Member Author

@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 try_ variants because these are on Read and already always return a result.

@clarfonthey
Copy link
Copy Markdown
Contributor

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.

@Darksonn
Copy link
Copy Markdown
Member

Yes, the trait does help somewhat.

@joshtriplett joshtriplett added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 27, 2026
Copy link
Copy Markdown
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do really like this, but I'm curious if BigEndian/LittleEndian wrapper types were considered, which would allow integration with the more typical From/TryFrom traits?

View changes since this review

Comment thread library/std/src/io/mod.rs
Comment on lines +3227 to +3237
/// Trait for types that can be converted from a fixed-size byte array with a specified endianness
#[unstable(feature = "read_le_be_internals", reason = "internals", issue = "none")]
// Once we can use associated consts in the types of method parameters, rewrite this to have
// `from_le_bytes` and `from_be_bytes` methods, move it to `core`, and make it public.
pub trait FromEndianBytes: crate::sealed::Sealed + Sized {
#[doc(hidden)]
fn read_le_from(r: &mut impl Read) -> Result<Self>;

#[doc(hidden)]
fn read_be_from(r: &mut impl Read) -> Result<Self>;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was discussed in-person at Rust week, but I think it would be nicer to introduce BigEndian and LittleEndian wrappers so we could omit this trait and instead use TryFrom directly. I have an example here. The biggest downsides are:

  1. It requires adding a From<Infallible> implementation for std::io::Error. But I think that's fine, and also means primitives can have infallible From<XEndian<[u8; _]>> implementations that naturally get exposed as TryFrom implementations for the sake of read_le/read_be.
  2. My example has an explicit N parameter fir the same reasons described in the PR description, and could be removed under the same conditions. I do think it's noteworthy that the compiler is able to successfully infer N even with the level of indirection seen here. Could a shorter-term fix for the usability of these methods just be the compiler allowing these parameters to be omitted, similar to how lifetimes that can be inferred are allowed to be omitted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api T-libs-api Relevant to the library API 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