From 7438f43b7cef5105dea5f00cefe6f1d160ed7eb7 Mon Sep 17 00:00:00 2001 From: Julian Wollersberger Date: Sat, 28 Nov 2020 13:35:09 +0100 Subject: [PATCH 1/2] Implement From for u64 and u128. --- library/core/src/char/convert.rs | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) 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 for u32 { } } +#[stable(feature = "more_char_conversions", since = "1.50.0")] +impl From 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 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 From e8cb72c503ff02144876578e38f4782414750776 Mon Sep 17 00:00:00 2001 From: Julian Wollersberger Date: Sat, 9 Jan 2021 12:31:30 +0100 Subject: [PATCH 2/2] Update the stabilisation version. --- library/core/src/char/convert.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/char/convert.rs b/library/core/src/char/convert.rs index ad193c082e4..6dc8a90a67c 100644 --- a/library/core/src/char/convert.rs +++ b/library/core/src/char/convert.rs @@ -112,7 +112,7 @@ impl From for u32 { } } -#[stable(feature = "more_char_conversions", since = "1.50.0")] +#[stable(feature = "more_char_conversions", since = "1.51.0")] impl From for u64 { /// Converts a [`char`] into a [`u64`]. /// @@ -133,7 +133,7 @@ impl From for u64 { } } -#[stable(feature = "more_char_conversions", since = "1.50.0")] +#[stable(feature = "more_char_conversions", since = "1.51.0")] impl From for u128 { /// Converts a [`char`] into a [`u128`]. ///