Skip to content

as_ne_bytes for primitive types #64464

Closed
@hch12907

Description

@hch12907

Recently I've encountered a problem, when I wanted to return a slice of bytes in an integer:

fn to_bytes(num: &u32) -> &[u8] {
    &u32::to_le_bytes(*num)
}

(This is an oversimplification of the original code, which is generic over every integer type along with other stuff)

Obviously, since to_le_bytes(*num) is dropped when the function returns, this piece of code failed the borrow check. Hence my need for as_le_bytes(num), as num is actually alive all this time!

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions