Skip to content

Suggest _bytes versions of endian-converting methods - #149295

Merged
bors merged 1 commit into
rust-lang:mainfrom
kornelski:endian_bytes
Nov 27, 2025
Merged

Suggest _bytes versions of endian-converting methods#149295
bors merged 1 commit into
rust-lang:mainfrom
kornelski:endian_bytes

Conversation

@kornelski

@kornelskikornelski commented Nov 25, 2025

Copy link
Copy Markdown
Contributor

As pointed out in this article, the int.to_be()/int.to_le() functions are technically returning a wrong type, because endian-specific representations of integers and Rust's always-native-endian integers are different things.

I wanted to make the docs state more directly that byte swapping will result in the type having a "wrong" value, but I wasn't sure whether to delve into the special case of palindrome-like values (0x12343412) not changing.

I've updated the docs to suggest {to,from}_[lb]e_bytes() instead. These methods use [u8; _] for endian-specific representations, which is a different type than the native-endian u16/u32/u64, and this is a type-safety improvement.

@rustbotrustbot 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 Nov 25, 2025
@rustbot

Copy link
Copy Markdown
Collaborator

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

@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Nov 26, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit 3faad7c has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 26, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 26, 2025
…ulacrum
Suggest _bytes versions of endian-converting methods
As pointed out [in this article](https://purplesyringa.moe/blog/ntoh-hton-is-a-bad-api/), the `int.to_be()`/`int.to_le()` functions are technically returning a wrong type, because endian-specific representations of integers and Rust's always-native-endian integers are different things.
I wanted to make the docs state more directly that byte swapping will result in the type having a "wrong" value, but I wasn't sure whether to delve into the special case of palindrome-like values (`0x12343412`) not changing.
I've updated the docs to suggest `{to,from}_[lb]e_bytes()` instead. These methods use `[u8; _]` for endian-specific representations, which is a different type than the native-endian `u16`/`u32`/`u64`, and this is a type-safety improvement.
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 26, 2025
…ulacrum
Suggest _bytes versions of endian-converting methods
As pointed out [in this article](https://purplesyringa.moe/blog/ntoh-hton-is-a-bad-api/), the `int.to_be()`/`int.to_le()` functions are technically returning a wrong type, because endian-specific representations of integers and Rust's always-native-endian integers are different things.
I wanted to make the docs state more directly that byte swapping will result in the type having a "wrong" value, but I wasn't sure whether to delve into the special case of palindrome-like values (`0x12343412`) not changing.
I've updated the docs to suggest `{to,from}_[lb]e_bytes()` instead. These methods use `[u8; _]` for endian-specific representations, which is a different type than the native-endian `u16`/`u32`/`u64`, and this is a type-safety improvement.
@ZalatharZalathar mentioned this pull request Nov 26, 2025
bors added a commit that referenced this pull request Nov 26, 2025
Rollup of 19 pull requests
Successful merges:
- #148048 (Stabilize `maybe_uninit_write_slice`)
- #148641 (Add a diagnostic attribute for special casing const bound errors for non-const impls)
- #148765 (std: split up the `thread` module)
- #149074 (Add Command::get_env_clear)
- #149097 (num: Implement `uint_gather_scatter_bits` feature for unsigned integers)
- #149131 (optimize `slice::Iter::next_chunk`)
- #149190 (Forbid `CHECK: br` and `CHECK-NOT: br` in codegen tests (suggest `br {{.*}}` instead))
- #149239 (clarify float min/max behavios for NaNs and signed zeros)
- #149243 (Fix typo and clarify bootstrap change tracker entry)
- #149270 (implement `Iterator::{exactly_one, collect_array}`)
- #149295 (Suggest _bytes versions of endian-converting methods)
- #149301 (Motor OS: make decode_error_kind more comprehensive)
- #149306 (bootstrap: Miri now handles jemalloc like everything else)
- #149325 (rustdoc: add regression test for #140968)
- #149332 (fix rustdoc search says “Consider searching for "null" instead.” #149324)
- #149349 (Fix typo in comment.)
- #149353 (Tidying up UI tests [3/N])
- #149355 (Document that `build.description` affects symbol mangling and crate IDs)
- #149360 (Enable CI download for windows-gnullvm)
r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 27, 2025
…ulacrum
Suggest _bytes versions of endian-converting methods
As pointed out [in this article](https://purplesyringa.moe/blog/ntoh-hton-is-a-bad-api/), the `int.to_be()`/`int.to_le()` functions are technically returning a wrong type, because endian-specific representations of integers and Rust's always-native-endian integers are different things.
I wanted to make the docs state more directly that byte swapping will result in the type having a "wrong" value, but I wasn't sure whether to delve into the special case of palindrome-like values (`0x12343412`) not changing.
I've updated the docs to suggest `{to,from}_[lb]e_bytes()` instead. These methods use `[u8; _]` for endian-specific representations, which is a different type than the native-endian `u16`/`u32`/`u64`, and this is a type-safety improvement.
This was referenced Nov 27, 2025
bors added a commit that referenced this pull request Nov 27, 2025
Rollup of 8 pull requests
Successful merges:
- #149238 (float::clamp: make treatment of signed zeros unspecified)
- #149270 (implement `Iterator::{exactly_one, collect_array}`)
- #149295 (Suggest _bytes versions of endian-converting methods)
- #149332 (fix rustdoc search says “Consider searching for "null" instead.” #149324)
- #149349 (Fix typo in comment.)
- #149353 (Tidying up UI tests [3/N])
- #149355 (Document that `build.description` affects symbol mangling and crate IDs)
- #149360 (Enable CI download for windows-gnullvm)
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 463eeba into rust-lang:mainNov 27, 2025
11 checks passed
@rustbotrustbot added this to the 1.93.0 milestone Nov 27, 2025
rust-timer added a commit that referenced this pull request Nov 27, 2025
Rollup merge of #149295 - kornelski:endian_bytes, r=Mark-Simulacrum
Suggest _bytes versions of endian-converting methods
As pointed out [in this article](https://purplesyringa.moe/blog/ntoh-hton-is-a-bad-api/), the `int.to_be()`/`int.to_le()` functions are technically returning a wrong type, because endian-specific representations of integers and Rust's always-native-endian integers are different things.
I wanted to make the docs state more directly that byte swapping will result in the type having a "wrong" value, but I wasn't sure whether to delve into the special case of palindrome-like values (`0x12343412`) not changing.
I've updated the docs to suggest `{to,from}_[lb]e_bytes()` instead. These methods use `[u8; _]` for endian-specific representations, which is a different type than the native-endian `u16`/`u32`/`u64`, and this is a type-safety improvement.
@kornelski
kornelski deleted the endian_bytes branch November 28, 2025 16:37
github-actionsBot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Dec 1, 2025
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#149238 (float::clamp: make treatment of signed zeros unspecified)
- rust-lang/rust#149270 (implement `Iterator::{exactly_one, collect_array}`)
- rust-lang/rust#149295 (Suggest _bytes versions of endian-converting methods)
- rust-lang/rust#149332 (fix rustdoc search says “Consider searching for "null" instead.” rust-lang/rust#149324)
- rust-lang/rust#149349 (Fix typo in comment.)
- rust-lang/rust#149353 (Tidying up UI tests [3/N])
- rust-lang/rust#149355 (Document that `build.description` affects symbol mangling and crate IDs)
- rust-lang/rust#149360 (Enable CI download for windows-gnullvm)
r? `@ghost`
`@rustbot` modify labels: rollup
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jan 20, 2026
…ulacrum
Suggest _bytes versions of endian-converting methods
As pointed out [in this article](https://purplesyringa.moe/blog/ntoh-hton-is-a-bad-api/), the `int.to_be()`/`int.to_le()` functions are technically returning a wrong type, because endian-specific representations of integers and Rust's always-native-endian integers are different things.
I wanted to make the docs state more directly that byte swapping will result in the type having a "wrong" value, but I wasn't sure whether to delve into the special case of palindrome-like values (`0x12343412`) not changing.
I've updated the docs to suggest `{to,from}_[lb]e_bytes()` instead. These methods use `[u8; _]` for endian-specific representations, which is a different type than the native-endian `u16`/`u32`/`u64`, and this is a type-safety improvement.
github-actionsBot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jan 20, 2026
Rollup of 8 pull requests
Successful merges:
- rust-lang#149238 (float::clamp: make treatment of signed zeros unspecified)
- rust-lang#149270 (implement `Iterator::{exactly_one, collect_array}`)
- rust-lang#149295 (Suggest _bytes versions of endian-converting methods)
- rust-lang#149332 (fix rustdoc search says “Consider searching for "null" instead.” rust-lang#149324)
- rust-lang#149349 (Fix typo in comment.)
- rust-lang#149353 (Tidying up UI tests [3/N])
- rust-lang#149355 (Document that `build.description` affects symbol mangling and crate IDs)
- rust-lang#149360 (Enable CI download for windows-gnullvm)
r? `@ghost`
`@rustbot` modify labels: rollup
ghaaj pushed a commit to ghaaj/polygrammar that referenced this pull request Aug 7, 2026
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#149238 (float::clamp: make treatment of signed zeros unspecified)
- rust-lang/rust#149270 (implement `Iterator::{exactly_one, collect_array}`)
- rust-lang/rust#149295 (Suggest _bytes versions of endian-converting methods)
- rust-lang/rust#149332 (fix rustdoc search says “Consider searching for "null" instead.” rust-lang/rust#149324)
- rust-lang/rust#149349 (Fix typo in comment.)
- rust-lang/rust#149353 (Tidying up UI tests [3/N])
- rust-lang/rust#149355 (Document that `build.description` affects symbol mangling and crate IDs)
- rust-lang/rust#149360 (Enable CI download for windows-gnullvm)
r? `@ghost`
`@rustbot` modify labels: rollup
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Aug 17, 2026
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#149238 (float::clamp: make treatment of signed zeros unspecified)
- rust-lang/rust#149270 (implement `Iterator::{exactly_one, collect_array}`)
- rust-lang/rust#149295 (Suggest _bytes versions of endian-converting methods)
- rust-lang/rust#149332 (fix rustdoc search says “Consider searching for "null" instead.” rust-lang/rust#149324)
- rust-lang/rust#149349 (Fix typo in comment.)
- rust-lang/rust#149353 (Tidying up UI tests [3/N])
- rust-lang/rust#149355 (Document that `build.description` affects symbol mangling and crate IDs)
- rust-lang/rust#149360 (Enable CI download for windows-gnullvm)
r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@kornelski@rustbot@Mark-Simulacrum@bors