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

Implement From<char> for u64 and u128. #79502

Merged
merged 2 commits into from
Jan 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions library/core/src/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,48 @@ impl From<char> for u32 {
}
}

#[stable(feature = "more_char_conversions", since = "1.50.0")]
impl From<char> for u64 {
/// Converts a [`char`] into a [`u64`].
///
/// # Examples
///
/// ```
/// use std::mem;
///
/// let c = '👤';
/// let u = u64::from(c);
/// assert!(8 == mem::size_of_val(&u))
/// ```
#[inline]
fn from(c: char) -> Self {
// The char is casted to the value of the code point, then zero-extended to 64 bit.
// See [https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics]
c as u64
}
}

#[stable(feature = "more_char_conversions", since = "1.50.0")]
impl From<char> for u128 {
/// Converts a [`char`] into a [`u128`].
///
/// # Examples
///
/// ```
/// use std::mem;
///
/// let c = '⚙';
/// let u = u128::from(c);
/// assert!(16 == mem::size_of_val(&u))
/// ```
#[inline]
fn from(c: char) -> Self {
// The char is casted to the value of the code point, then zero-extended to 128 bit.
// See [https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics]
c as u128
}
}

/// Maps a byte in 0x00..=0xFF to a `char` whose code point has the same value, in U+0000..=U+00FF.
///
/// Unicode is designed such that this effectively decodes bytes
Expand Down