Skip to content

Suggest {to,from}_ne_bytes for transmutations between arrays and integers, etc#136083

Merged
bors merged 2 commits intorust-lang:masterfrom
bend-n:⃤⃤
Apr 24, 2025

Hidden character warning

The head ref may contain hidden characters: "\u20e4\u20e4"
Merged

Suggest {to,from}_ne_bytes for transmutations between arrays and integers, etc#136083
bors merged 2 commits intorust-lang:masterfrom
bend-n:⃤⃤

Conversation

@bend-n
Copy link
Copy Markdown
Contributor

@bend-n bend-n commented Jan 26, 2025

implements #136067

Rust has helper methods for many kinds of safe transmutes, for example integer<->bytes. This is a lint against using transmute for these cases.

fn bytes_at_home(x: [u8; 4]) -> u32 {
   transmute(x)
}

// other examples
transmute::<[u8; 2], u16>();
transmute::<[u8; 8], f64>();
transmute::<u32, [u8; 4]>();
transmute::<char, u32>();
transmute::<u32, char>();

It would be handy to suggest u32::from_ne_bytes(x).
This is implemented for [u8; _] -> {float int}

This also implements the cases:
fXX <-> uXX = {from_bits, to_bits}
uXX -> iXX via cast_unsigned and cast_signed
{char -> u32, bool -> n8} via from
u32 -> char via from_u32_unchecked (note: notes from_u32().unwrap()) (contested)
u8 -> bool via == (debatable)


try-job: aarch64-gnu
try-job: test-various

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

Labels

L-unnecessary_transmutes Lint: unnecessary_transmutes S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-libs Relevant 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.