diff options
| author | bors <bors@rust-lang.org> | 2016-09-01 02:53:28 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-09-01 02:53:28 -0700 |
| commit | b2799a56a1631ee351e4687ee649808e28cccda1 (patch) | |
| tree | ee8fd765766cc38f38e8dd02782412c1d2ee189d /src/libcoretest | |
| parent | 3135b7877a66e72ac4833588eb8654aa155876e8 (diff) | |
| parent | f040208d533e1d6d9ee0e0408ee74e26e14d1284 (diff) | |
Auto merge of #35755 - SimonSapin:char_convert, r=alexcrichton
Implement std::convert traits for char This is motivated by avoiding the `as` operator, which sometimes silently truncates, and instead use conversions that are explicitly lossless and infallible. I’m less certain that `From<u8> for char` should be implemented: while it matches an existing behavior of `as`, it’s not necessarily the right thing to use for non-ASCII bytes. It effectively decodes bytes as ISO/IEC 8859-1 (since Unicode designed its first 256 code points to be compatible with that encoding), but that is not apparent in the API name.
Diffstat (limited to 'src/libcoretest')
| -rw-r--r-- | src/libcoretest/char.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/libcoretest/char.rs b/src/libcoretest/char.rs index 333503d7389..199437a431e 100644 --- a/src/libcoretest/char.rs +++ b/src/libcoretest/char.rs @@ -9,6 +9,24 @@ // except according to those terms. use std::char; +use std::convert::TryFrom; + +#[test] +fn test_convert() { + assert_eq!(u32::from('a'), 0x61); + assert_eq!(char::from(b'\0'), '\0'); + assert_eq!(char::from(b'a'), 'a'); + assert_eq!(char::from(b'\xFF'), '\u{FF}'); + assert_eq!(char::try_from(0_u32), Ok('\0')); + assert_eq!(char::try_from(0x61_u32), Ok('a')); + assert_eq!(char::try_from(0xD7FF_u32), Ok('\u{D7FF}')); + assert!(char::try_from(0xD800_u32).is_err()); + assert!(char::try_from(0xDFFF_u32).is_err()); + assert_eq!(char::try_from(0xE000_u32), Ok('\u{E000}')); + assert_eq!(char::try_from(0x10FFFF_u32), Ok('\u{10FFFF}')); + assert!(char::try_from(0x110000_u32).is_err()); + assert!(char::try_from(0xFFFF_FFFF_u32).is_err()); +} #[test] fn test_is_lowercase() { |
