diff options
| author | Julian Wollersberger <julian.wollersberger@gmx.at> | 2020-11-28 13:35:09 +0100 |
|---|---|---|
| committer | Julian Wollersberger <julian.wollersberger@gmx.at> | 2020-11-28 13:35:09 +0100 |
| commit | 7438f43b7cef5105dea5f00cefe6f1d160ed7eb7 (patch) | |
| tree | 562d779f58ee9a3137d7a38852811c96b5519b38 | |
| parent | 4ae328bef47dffcbf363e5ae873f419c06a5511d (diff) | |
| download | rust-7438f43b7cef5105dea5f00cefe6f1d160ed7eb7.tar.gz rust-7438f43b7cef5105dea5f00cefe6f1d160ed7eb7.zip | |
Implement From<char> for u64 and u128.
| -rw-r--r-- | library/core/src/char/convert.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/library/core/src/char/convert.rs b/library/core/src/char/convert.rs index 394db5b5917..ad193c082e4 100644 --- a/library/core/src/char/convert.rs +++ b/library/core/src/char/convert.rs @@ -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 |
