Skip to content

ACP: Add char::to_u32 to access underlying value #778

@okaneco

Description

@okaneco

Proposal

Problem statement

There's no explicit char method to access the underlying 4-bytes of data which backs the type.

An as cast, TryFrom/TryInto, or From<u32> must be used to convert to another type. into() has its own issues of usually requiring more type annotations because it cannot be inferred.

edit: I originally wrote that the From<char> for u32 impl was not visible on the char docs, but it does exist. As pointed out, it's here.

Motivating examples or use cases

When casting from char, it's common to blindly as u32 or as u8 since it's the most ergonomic/lazy way to currently write it. When reading or writing code, as casts are generally to be treated with suspicion so having an explicit to_u32() method would lessen the cognitive load of reviewing code with these conversions.

The currently unstable core::ascii::Char type has a to_u8 method.

As of this writing, there are 11 code uses (2 doc comments) of <char> as u32 and 4 uses of <char> as u8 in library\core\src\char\methods.rs. With the unstable truncation methods, we can explicitly write the u8 conversion as <char>.to_u32().truncate::<u8>().

The current documentation of char::from_u32 teaches readers to use an as cast for char to u32 conversion:

Converts a u32 to a char.

Note that all chars are valid u32s, and can be cast to one with as.

Solution sketch

impl char {
    /// Returns the value of `self` as a `u32`.
    pub const fn to_u32(self) -> u32 {
        self as u32
    }
}

Alternatives

  1. Do nothing. There are ways to do this today but they're not as explicit except for u32::from.
  2. Use the new, unstable truncate/extend API to allow for conversion from char to integers. I was prompted to write this ACP because I realized I mainly still used as casts for floats and from char to u8 for ASCII character processing. I would like to be able to use more explicit casting but it didn't seem appropriate to implement truncate and extend as a one-way conversion from char since it's really not like the other integers. This proposal would be a more minimal library addition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions