@@ -135,12 +135,43 @@ impl Ascii {
135
135
}
136
136
}
137
137
138
- impl < ' a > fmt:: Show for Ascii {
138
+ impl fmt:: String for Ascii {
139
139
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)
141
141
}
142
142
}
143
143
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
+
144
175
/// Trait for converting into an ascii type.
145
176
#[ experimental = "may be replaced by generic conversion traits" ]
146
177
pub trait AsciiCast < T , U = Self > : AsciiExt < U > {
@@ -393,14 +424,41 @@ mod tests {
393
424
}
394
425
395
426
#[ 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( ) ) ;
399
430
}
400
431
401
432
#[ 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( ) ) ;
405
442
}
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
+ // }
406
464
}
0 commit comments