Skip to content

Commit ecd3c0f

Browse files
author
Thomas Bahn
committed
Add fmt::String implementations
The main types `Ascii`, `Vec<Ascii>` and `[Ascii]` can now be formatted with `fmt::String`.
1 parent 0690ad9 commit ecd3c0f

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

src/lib.rs

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,43 @@ impl Ascii {
135135
}
136136
}
137137

138-
impl<'a> fmt::Show for Ascii {
138+
impl fmt::String for Ascii {
139139
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
140-
(self.chr as char).fmt(f)
140+
fmt::String::fmt(&(self.chr as char), f)
141141
}
142142
}
143143

144+
impl fmt::String for Vec<Ascii> {
145+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
146+
fmt::String::fmt(&self[], f)
147+
}
148+
}
149+
150+
impl fmt::String for [Ascii] {
151+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
152+
fmt::String::fmt(self.as_str(), f)
153+
}
154+
}
155+
156+
impl fmt::Show for Ascii {
157+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158+
fmt::Show::fmt(&(self.chr as char), f)
159+
}
160+
}
161+
162+
// TODO: The following impls conflict with the generic impls in std.
163+
// impl fmt::Show for Vec<Ascii> {
164+
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165+
// fmt::Show::fmt(&self[], f)
166+
// }
167+
// }
168+
169+
// impl fmt::Show for [Ascii] {
170+
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
171+
// fmt::Show::fmt(self.as_str(), f)
172+
// }
173+
// }
174+
144175
/// Trait for converting into an ascii type.
145176
#[experimental = "may be replaced by generic conversion traits"]
146177
pub trait AsciiCast<T, U = Self> : AsciiExt<U> {
@@ -393,14 +424,41 @@ mod tests {
393424
}
394425

395426
#[test]
396-
fn test_to_string() {
397-
let s = Ascii{ chr: b't' }.to_string();
398-
assert_eq!(s, "t".to_string());
427+
fn fmt_string_ascii() {
428+
let s = Ascii{ chr: b't' };
429+
assert_eq!(format!("{}", s), "t".to_string());
399430
}
400431

401432
#[test]
402-
fn test_show() {
403-
let c = Ascii { chr: b't' };
404-
assert_eq!(format!("{}", c), "t".to_string());
433+
fn fmt_string_ascii_slice() {
434+
let s = "abc".to_ascii().unwrap();
435+
assert_eq!(format!("{}", s), "abc".to_string());
436+
}
437+
438+
#[test]
439+
fn fmt_string_ascii_vec() {
440+
let s = "abc".to_string().into_ascii().unwrap();
441+
assert_eq!(format!("{}", s), "abc".to_string());
405442
}
443+
444+
#[test]
445+
fn fmt_show_ascii() {
446+
let c = Ascii { chr: b't' };
447+
assert_eq!(format!("{:?}", c), "'t'".to_string());
448+
}
449+
450+
// NOTE: The following tests fail intentionally until custom `fmt::Show`
451+
// implementations for `Vec<Ascii>` and `&[Ascii]` can be provided.
452+
// (Or the current types are newtyped.)
453+
// #[test]
454+
// fn fmt_show_ascii_slice() {
455+
// let s = "abc".to_ascii().unwrap();
456+
// assert_eq!(format!("{}", s), "\"abc\"".to_string());
457+
// }
458+
459+
// #[test]
460+
// fn fmt_show_ascii_vec() {
461+
// let s = "abc".to_string().into_ascii().unwrap();
462+
// assert_eq!(format!("{}", s), "\"abc\"".to_string());
463+
// }
406464
}

0 commit comments

Comments
 (0)