From 64472e49a7c9238591221f8d4d3b6efd4d7f4bd3 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Sun, 6 Jun 2021 14:26:38 -0700 Subject: [PATCH] Update `ascii` example. --- ascii/Cargo.toml | 1 + ascii/src/lib.rs | 16 ++++------------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/ascii/Cargo.toml b/ascii/Cargo.toml index 9b936b5..af9dc53 100644 --- a/ascii/Cargo.toml +++ b/ascii/Cargo.toml @@ -2,5 +2,6 @@ name = "ascii" version = "0.1.0" authors = ["You "] +edition = "2018" [dependencies] diff --git a/ascii/src/lib.rs b/ascii/src/lib.rs index a441db1..400c90e 100644 --- a/ascii/src/lib.rs +++ b/ascii/src/lib.rs @@ -29,16 +29,10 @@ mod my_ascii { fn from(ascii: Ascii) -> String { // If this module has no bugs, this is safe, because // well-formed ASCII text is also well-formed UTF-8. - unsafe { - String::from_utf8_unchecked(ascii.0) - } + unsafe { String::from_utf8_unchecked(ascii.0) } } } - impl From for Vec { - fn from(ascii: Ascii) -> Vec { ascii.0 } - } - // This must be placed inside the `my_ascii` module. impl Ascii { /// Construct an `Ascii` value from `bytes`, without checking @@ -59,10 +53,9 @@ mod my_ascii { } } - #[test] fn good_ascii() { - use self::my_ascii::Ascii; + use my_ascii::Ascii; let bytes: Vec = b"ASCII and ye shall receive".to_vec(); @@ -79,7 +72,7 @@ fn good_ascii() { #[test] fn bad_ascii() { - use self::my_ascii::Ascii; + use my_ascii::Ascii; // Imagine that this vector is the result of some complicated process // 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 // 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. - // It could pass, fail, crash, do nothing at all, etc. - assert_eq!(bogus.chars().next().unwrap() as u32, 0x1fffff_u32); + assert_eq!(bogus.chars().next().unwrap() as u32, 0x1fffff); }