Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List from_be_bytes as Alternative in Transmute Docs #71187

Closed
domenukk opened this issue Apr 16, 2020 · 5 comments
Closed

List from_be_bytes as Alternative in Transmute Docs #71187

domenukk opened this issue Apr 16, 2020 · 5 comments
Assignees
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@domenukk
Copy link

Many code bases I see use transmute to convert raw bytes (&[u8]) to u32, f64, etc. - while they could, and probably should, use from_le_bytes or from_be_bytes.
The transmute docs include a lengthy section about Alternatives, but this obvious one seems to be missing.

@jonas-schievink jonas-schievink added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Apr 16, 2020
@huangjiahua
Copy link

@domenukk I've used from_le_bytes and from_be_bytes several times in my little project. I want to have a try. Will this code snippet work?

    let raw_data = &[0x78, 0x56, 0x34, 0x12];
    
    let num = unsafe {
        std::mem::transmute::<[u8; 4], u32>(*raw_data)
    };
    // This result depends on your machine
    assert_eq!(num, 0x12345678);


    // use from_be_bytes and from_le_bytes instead to produce predictable result
    let num = u32::from_le_bytes(*raw_data);
    assert_eq!(num, 0x12345678);
    
    let num = u32::from_be_bytes(*raw_data);
    assert_eq!(num, 0x78563412);

@Duddino
Copy link

Duddino commented Apr 17, 2020

@huangjiahua maybe you can also add u32::from_ne_bytes() as an alternative to transmute but without unsafe
also you can show that u32::from_ne_bytes() is equal to mem::trasmute with an assertion

@rustbot
Copy link
Collaborator

rustbot commented Apr 17, 2020

Error: Parsing assign command in comment failed: ...uangjiahua|error: user should start with @ at >|...

Please let @rust-lang/release know if you're having trouble with this bot.

1 similar comment
@rustbot
Copy link
Collaborator

rustbot commented Apr 17, 2020

Error: Parsing assign command in comment failed: ...uangjiahua|error: user should start with @ at >|...

Please let @rust-lang/release know if you're having trouble with this bot.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 19, 2020
…=Dylan-DPC

Add example in the alternative in std::mem::transmute docs

It is safer to use `from_ne_bytes` to convert raw bytes to type like u32.  rust-lang#71187
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 19, 2020
…=Dylan-DPC

Add example in the alternative in std::mem::transmute docs

It is safer to use `from_ne_bytes` to convert raw bytes to type like u32.  rust-lang#71187
@Alexendoo
Copy link
Member

Closed by #71315

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants