Update `ascii` example.

pull/13/head
Jim Blandy 3 years ago
parent 488030873c
commit 64472e49a7

@ -2,5 +2,6 @@
name = "ascii" name = "ascii"
version = "0.1.0" version = "0.1.0"
authors = ["You <you@example.com>"] authors = ["You <you@example.com>"]
edition = "2018"
[dependencies] [dependencies]

@ -29,16 +29,10 @@ mod my_ascii {
fn from(ascii: Ascii) -> String { fn from(ascii: Ascii) -> String {
// If this module has no bugs, this is safe, because // If this module has no bugs, this is safe, because
// well-formed ASCII text is also well-formed UTF-8. // well-formed ASCII text is also well-formed UTF-8.
unsafe { unsafe { String::from_utf8_unchecked(ascii.0) }
String::from_utf8_unchecked(ascii.0)
}
} }
} }
impl From<Ascii> for Vec<u8> {
fn from(ascii: Ascii) -> Vec<u8> { ascii.0 }
}
// This must be placed inside the `my_ascii` module. // This must be placed inside the `my_ascii` module.
impl Ascii { impl Ascii {
/// Construct an `Ascii` value from `bytes`, without checking /// Construct an `Ascii` value from `bytes`, without checking
@ -59,10 +53,9 @@ mod my_ascii {
} }
} }
#[test] #[test]
fn good_ascii() { fn good_ascii() {
use self::my_ascii::Ascii; use my_ascii::Ascii;
let bytes: Vec<u8> = b"ASCII and ye shall receive".to_vec(); let bytes: Vec<u8> = b"ASCII and ye shall receive".to_vec();
@ -79,7 +72,7 @@ fn good_ascii() {
#[test] #[test]
fn bad_ascii() { fn bad_ascii() {
use self::my_ascii::Ascii; use my_ascii::Ascii;
// Imagine that this vector is the result of some complicated process // Imagine that this vector is the result of some complicated process
// that we expected to produce ASCII. Something went wrong! // that we expected to produce ASCII. Something went wrong!
@ -96,6 +89,5 @@ fn bad_ascii() {
// `bogus` now holds ill-formed UTF-8. Parsing its first character produces // `bogus` now holds ill-formed UTF-8. Parsing its first character produces
// a `char` that is not a valid Unicode code point. That's undefined // a `char` that is not a valid Unicode code point. That's undefined
// behavior, so the language doesn't say how this assertion should behave. // behavior, so the language doesn't say how this assertion should behave.
// It could pass, fail, crash, do nothing at all, etc. assert_eq!(bogus.chars().next().unwrap() as u32, 0x1fffff);
assert_eq!(bogus.chars().next().unwrap() as u32, 0x1fffff_u32);
} }

Loading…
Cancel
Save