Skip to content

Commit 41d0a89

Browse files
committed
Implement From<char> for u32, and From<u8> for char
These fit with other From implementations between integer types. This helps the coding style of avoiding the 'as' operator that sometimes silently truncates, and signals that these specific conversions are lossless and infaillible.
1 parent 86dde9b commit 41d0a89

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/libcore/char.rs

+34
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,40 @@ pub unsafe fn from_u32_unchecked(i: u32) -> char {
175175
transmute(i)
176176
}
177177

178+
#[stable(feature = "char_convert", since = "1.13.0")]
179+
impl From<char> for u32 {
180+
#[inline]
181+
fn from(c: char) -> Self {
182+
c as u32
183+
}
184+
}
185+
186+
/// Maps a byte in 0x00...0xFF to a `char` whose code point has the same value, in U+0000 to U+00FF.
187+
///
188+
/// Unicode is designed such that this effectively decodes bytes
189+
/// with the character encoding that IANA calls ISO-8859-1.
190+
/// This encoding is compatible with ASCII.
191+
///
192+
/// Note that this is different from ISO/IEC 8859-1 a.k.a. ISO 8859-1 (with one less hypen),
193+
/// which leaves some "blanks", byte values that are not assigned to any character.
194+
/// ISO-8859-1 (the IANA one) assigns them to the C0 and C1 control codes.
195+
///
196+
/// Note that this is *also* different from Windows-1252 a.k.a. code page 1252,
197+
/// which is a superset ISO/IEC 8859-1 that assigns some (not all!) blanks
198+
/// to punctuation and various Latin characters.
199+
///
200+
/// To confuse things further, [on the Web](https://encoding.spec.whatwg.org/)
201+
/// `ascii`, `iso-8859-1`, and `windows-1252` are all aliases
202+
/// for a superset of Windows-1252 that fills the remaining blanks with corresponding
203+
/// C0 and C1 control codes.
204+
#[stable(feature = "char_convert", since = "1.13.0")]
205+
impl From<u8> for char {
206+
#[inline]
207+
fn from(i: u8) -> Self {
208+
i as char
209+
}
210+
}
211+
178212
/// Converts a digit in the given radix to a `char`.
179213
///
180214
/// A 'radix' here is sometimes also called a 'base'. A radix of two

src/libcoretest/char.rs

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
use std::char;
1212

13+
#[test]
14+
fn test_convert() {
15+
assert_eq!(u32::from('a'), 0x61);
16+
assert_eq!(char::from(b'\0'), '\0');
17+
assert_eq!(char::from(b'a'), 'a');
18+
assert_eq!(char::from(b'\xFF'), '\u{FF}');
19+
}
20+
1321
#[test]
1422
fn test_is_lowercase() {
1523
assert!('a'.is_lowercase());

0 commit comments

Comments
 (0)