From e2094fc9068fa52672a2300e3f2ffd26069fe18d Mon Sep 17 00:00:00 2001 From: George Burton Date: Wed, 8 Feb 2017 19:08:39 +0000 Subject: [PATCH 1/3] Impl fmt::Write for AsciiString, and add tests --- src/ascii_string.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/ascii_string.rs b/src/ascii_string.rs index c9dd3fb..95c64e1 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -466,6 +466,20 @@ impl fmt::Debug for AsciiString { } } +impl fmt::Write for AsciiString { + fn write_str(&mut self, s: &str) -> fmt::Result { + let astr = try!(AsciiStr::from_ascii(s.as_bytes()).map_err(|_| fmt::Error)); + self.push_str(astr); + Ok(()) + } + + fn write_char(&mut self, c: char) -> fmt::Result { + let achar = try!(AsciiChar::from(c).map_err(|_| fmt::Error)); + self.push(achar); + Ok(()) + } +} + impl FromIterator for AsciiString { fn from_iter>(iter: I) -> AsciiString { let mut buf = AsciiString::new(); @@ -644,7 +658,6 @@ impl IntoAsciiString for String { } } - #[cfg(test)] mod tests { use std::str::FromStr; @@ -675,4 +688,23 @@ mod tests { assert_eq!(format!("{}", s), "abc".to_string()); assert_eq!(format!("{:?}", s), "\"abc\"".to_string()); } + + #[test] + fn write_fmt() { + use std::fmt; + use std::str; + + let mut s0 = AsciiString::new(); + fmt::write(&mut s0, format_args!("Hello World")).unwrap(); + assert_eq!(s0, "Hello World"); + + let mut s1 = AsciiString::new(); + fmt::write(&mut s1, format_args!("{}", 9)).unwrap(); + assert_eq!(s1, "9"); + + let mut s2 = AsciiString::new(); + let sparkle_heart_bytes = [240, 159, 146, 150]; + let sparkle_heart = str::from_utf8(&sparkle_heart_bytes).unwrap(); + assert!(fmt::write(&mut s2, format_args!("{}", sparkle_heart)).is_err()); + } } From acfb90b2cffbc8b71ac3ae94e4477346a3e01478 Mon Sep 17 00:00:00 2001 From: George Burton Date: Wed, 8 Feb 2017 19:14:30 +0000 Subject: [PATCH 2/3] Small simplifying refactor --- src/ascii_string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ascii_string.rs b/src/ascii_string.rs index 95c64e1..88934e8 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -468,7 +468,7 @@ impl fmt::Debug for AsciiString { impl fmt::Write for AsciiString { fn write_str(&mut self, s: &str) -> fmt::Result { - let astr = try!(AsciiStr::from_ascii(s.as_bytes()).map_err(|_| fmt::Error)); + let astr = try!(AsciiStr::from_ascii(s).map_err(|_| fmt::Error)); self.push_str(astr); Ok(()) } From f7220f4b862cbe061b192f5beff219a9348405bb Mon Sep 17 00:00:00 2001 From: George Burton Date: Thu, 9 Feb 2017 18:18:42 +0000 Subject: [PATCH 3/3] Simplify use statements in write_fmt test --- src/ascii_string.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ascii_string.rs b/src/ascii_string.rs index 88934e8..06f313b 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -691,8 +691,7 @@ mod tests { #[test] fn write_fmt() { - use std::fmt; - use std::str; + use std::{fmt, str}; let mut s0 = AsciiString::new(); fmt::write(&mut s0, format_args!("Hello World")).unwrap();