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
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.

6 participants