Skip to content

Commit 7438f43

Browse files
author
Julian Wollersberger
committed
Implement From<char> for u64 and u128.
1 parent 4ae328b commit 7438f43

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

library/core/src/char/convert.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,48 @@ impl From<char> for u32 {
112112
}
113113
}
114114

115+
#[stable(feature = "more_char_conversions", since = "1.50.0")]
116+
impl From<char> for u64 {
117+
/// Converts a [`char`] into a [`u64`].
118+
///
119+
/// # Examples
120+
///
121+
/// ```
122+
/// use std::mem;
123+
///
124+
/// let c = '👤';
125+
/// let u = u64::from(c);
126+
/// assert!(8 == mem::size_of_val(&u))
127+
/// ```
128+
#[inline]
129+
fn from(c: char) -> Self {
130+
// The char is casted to the value of the code point, then zero-extended to 64 bit.
131+
// See [https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics]
132+
c as u64
133+
}
134+
}
135+
136+
#[stable(feature = "more_char_conversions", since = "1.50.0")]
137+
impl From<char> for u128 {
138+
/// Converts a [`char`] into a [`u128`].
139+
///
140+
/// # Examples
141+
///
142+
/// ```
143+
/// use std::mem;
144+
///
145+
/// let c = '⚙';
146+
/// let u = u128::from(c);
147+
/// assert!(16 == mem::size_of_val(&u))
148+
/// ```
149+
#[inline]
150+
fn from(c: char) -> Self {
151+
// The char is casted to the value of the code point, then zero-extended to 128 bit.
152+
// See [https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics]
153+
c as u128
154+
}
155+
}
156+
115157
/// Maps a byte in 0x00..=0xFF to a `char` whose code point has the same value, in U+0000..=U+00FF.
116158
///
117159
/// Unicode is designed such that this effectively decodes bytes

0 commit comments

Comments
 (0)