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
- Do nothing. There are ways to do this today but they're not as explicit except for
u32::from.
- 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.
Proposal
Problem statement
There's no explicit
charmethod to access the underlying 4-bytes of data which backs the type.An
ascast,TryFrom/TryInto, orFrom<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 u32impl was not visible on thechardocs, but it does exist. As pointed out, it's here.Motivating examples or use cases
When casting from
char, it's common to blindlyas u32oras u8since it's the most ergonomic/lazy way to currently write it. When reading or writing code,ascasts are generally to be treated with suspicion so having an explicitto_u32()method would lessen the cognitive load of reviewing code with these conversions.The currently unstable
core::ascii::Chartype has ato_u8method.As of this writing, there are 11 code uses (2 doc comments) of
<char> as u32and 4 uses of<char> as u8inlibrary\core\src\char\methods.rs. With the unstable truncation methods, we can explicitly write theu8conversion as<char>.to_u32().truncate::<u8>().The current documentation of
char::from_u32teaches readers to use anascast forchartou32conversion:Solution sketch
Alternatives
u32::from.truncate/extendAPI to allow for conversion fromcharto integers. I was prompted to write this ACP because I realized I mainly still usedascasts for floats and fromchartou8for 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 fromcharsince it's really not like the other integers. This proposal would be a more minimal library addition.