]> git.lizzy.rs Git - rust.git/blob - src/libcore/fmt/mod.rs
61f0d09453f2b1b44f0d177b13aa944cdf891093
[rust.git] / src / libcore / fmt / mod.rs
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Utilities for formatting and printing strings
12
13 #![allow(unused_variable)]
14
15 use any;
16 use cell::{Cell, Ref, RefMut};
17 use char::Char;
18 use collections::Collection;
19 use iter::{Iterator, range};
20 use kinds::Copy;
21 use mem;
22 use num::Float;
23 use option::{Option, Some, None};
24 use ops::Deref;
25 use result::{Ok, Err};
26 use result;
27 use slice::{Vector, ImmutableSlice};
28 use slice;
29 use str::StrSlice;
30 use str;
31
32 pub use self::num::radix;
33 pub use self::num::Radix;
34 pub use self::num::RadixFmt;
35
36 mod num;
37 mod float;
38 pub mod rt;
39
40 pub type Result = result::Result<(), FormatError>;
41
42 /// The error type which is returned from formatting a message into a stream.
43 ///
44 /// This type does not support transmission of an error other than that an error
45 /// occurred. Any extra information must be arranged to be transmitted through
46 /// some other means.
47 pub enum FormatError {
48     /// A generic write error occurred during formatting, no other information
49     /// is transmitted via this variant.
50     WriteError,
51 }
52
53 /// A collection of methods that are required to format a message into a stream.
54 ///
55 /// This trait is the type which this modules requires when formatting
56 /// information. This is similar to the standard library's `io::Writer` trait,
57 /// but it is only intended for use in libcore.
58 ///
59 /// This trait should generally not be implemented by consumers of the standard
60 /// library. The `write!` macro accepts an instance of `io::Writer`, and the
61 /// `io::Writer` trait is favored over implementing this trait.
62 pub trait FormatWriter {
63     /// Writes a slice of bytes into this writer, returning whether the write
64     /// succeeded.
65     ///
66     /// This method can only succeed if the entire byte slice was successfully
67     /// written, and this method will not return until all data has been
68     /// written or an error occurs.
69     ///
70     /// # Errors
71     ///
72     /// This function will return an instance of `FormatError` on error.
73     fn write(&mut self, bytes: &[u8]) -> Result;
74
75     /// Glue for usage of the `write!` macro with implementers of this trait.
76     ///
77     /// This method should generally not be invoked manually, but rather through
78     /// the `write!` macro itself.
79     fn write_fmt(&mut self, args: &Arguments) -> Result { write(self, args) }
80 }
81
82 /// A struct to represent both where to emit formatting strings to and how they
83 /// should be formatted. A mutable version of this is passed to all formatting
84 /// traits.
85 pub struct Formatter<'a> {
86     /// Flags for formatting (packed version of rt::Flag)
87     pub flags: uint,
88     /// Character used as 'fill' whenever there is alignment
89     pub fill: char,
90     /// Boolean indication of whether the output should be left-aligned
91     pub align: rt::Alignment,
92     /// Optionally specified integer width that the output should be
93     pub width: Option<uint>,
94     /// Optionally specified precision for numeric types
95     pub precision: Option<uint>,
96
97     buf: &'a mut FormatWriter,
98     curarg: slice::Items<'a, Argument<'a>>,
99     args: &'a [Argument<'a>],
100 }
101
102 enum Void {}
103
104 /// This struct represents the generic "argument" which is taken by the Xprintf
105 /// family of functions. It contains a function to format the given value. At
106 /// compile time it is ensured that the function and the value have the correct
107 /// types, and then this struct is used to canonicalize arguments to one type.
108 pub struct Argument<'a> {
109     formatter: extern "Rust" fn(&Void, &mut Formatter) -> Result,
110     value: &'a Void,
111 }
112
113 impl<'a> Arguments<'a> {
114     /// When using the format_args!() macro, this function is used to generate the
115     /// Arguments structure. The compiler inserts an `unsafe` block to call this,
116     /// which is valid because the compiler performs all necessary validation to
117     /// ensure that the resulting call to format/write would be safe.
118     #[doc(hidden)] #[inline]
119     pub unsafe fn new<'a>(fmt: &'static [rt::Piece<'static>],
120                           args: &'a [Argument<'a>]) -> Arguments<'a> {
121         Arguments{ fmt: mem::transmute(fmt), args: args }
122     }
123 }
124
125 /// This structure represents a safely precompiled version of a format string
126 /// and its arguments. This cannot be generated at runtime because it cannot
127 /// safely be done so, so no constructors are given and the fields are private
128 /// to prevent modification.
129 ///
130 /// The `format_args!` macro will safely create an instance of this structure
131 /// and pass it to a function or closure, passed as the first argument. The
132 /// macro validates the format string at compile-time so usage of the `write`
133 /// and `format` functions can be safely performed.
134 pub struct Arguments<'a> {
135     fmt: &'a [rt::Piece<'a>],
136     args: &'a [Argument<'a>],
137 }
138
139 impl<'a> Show for Arguments<'a> {
140     fn fmt(&self, fmt: &mut Formatter) -> Result {
141         write(fmt.buf, self)
142     }
143 }
144
145 /// When a format is not otherwise specified, types are formatted by ascribing
146 /// to this trait. There is not an explicit way of selecting this trait to be
147 /// used for formatting, it is only if no other format is specified.
148 pub trait Show {
149     /// Formats the value using the given formatter.
150     fn fmt(&self, &mut Formatter) -> Result;
151 }
152
153 /// Format trait for the `b` character
154 pub trait Bool {
155     /// Formats the value using the given formatter.
156     fn fmt(&self, &mut Formatter) -> Result;
157 }
158
159 /// Format trait for the `c` character
160 pub trait Char {
161     /// Formats the value using the given formatter.
162     fn fmt(&self, &mut Formatter) -> Result;
163 }
164
165 /// Format trait for the `i` and `d` characters
166 pub trait Signed {
167     /// Formats the value using the given formatter.
168     fn fmt(&self, &mut Formatter) -> Result;
169 }
170
171 /// Format trait for the `u` character
172 pub trait Unsigned {
173     /// Formats the value using the given formatter.
174     fn fmt(&self, &mut Formatter) -> Result;
175 }
176
177 /// Format trait for the `o` character
178 pub trait Octal {
179     /// Formats the value using the given formatter.
180     fn fmt(&self, &mut Formatter) -> Result;
181 }
182
183 /// Format trait for the `t` character
184 pub trait Binary {
185     /// Formats the value using the given formatter.
186     fn fmt(&self, &mut Formatter) -> Result;
187 }
188
189 /// Format trait for the `x` character
190 pub trait LowerHex {
191     /// Formats the value using the given formatter.
192     fn fmt(&self, &mut Formatter) -> Result;
193 }
194
195 /// Format trait for the `X` character
196 pub trait UpperHex {
197     /// Formats the value using the given formatter.
198     fn fmt(&self, &mut Formatter) -> Result;
199 }
200
201 /// Format trait for the `s` character
202 pub trait String {
203     /// Formats the value using the given formatter.
204     fn fmt(&self, &mut Formatter) -> Result;
205 }
206
207 /// Format trait for the `p` character
208 pub trait Pointer {
209     /// Formats the value using the given formatter.
210     fn fmt(&self, &mut Formatter) -> Result;
211 }
212
213 /// Format trait for the `f` character
214 pub trait Float {
215     /// Formats the value using the given formatter.
216     fn fmt(&self, &mut Formatter) -> Result;
217 }
218
219 /// Format trait for the `e` character
220 pub trait LowerExp {
221     /// Formats the value using the given formatter.
222     fn fmt(&self, &mut Formatter) -> Result;
223 }
224
225 /// Format trait for the `E` character
226 pub trait UpperExp {
227     /// Formats the value using the given formatter.
228     fn fmt(&self, &mut Formatter) -> Result;
229 }
230
231 // FIXME #11938 - UFCS would make us able call the above methods
232 // directly Show::show(x, fmt).
233 macro_rules! uniform_fn_call_workaround {
234     ($( $name: ident, $trait_: ident; )*) => {
235         $(
236             #[doc(hidden)]
237             pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) -> Result {
238                 x.fmt(fmt)
239             }
240             )*
241     }
242 }
243 uniform_fn_call_workaround! {
244     secret_show, Show;
245     secret_bool, Bool;
246     secret_char, Char;
247     secret_signed, Signed;
248     secret_unsigned, Unsigned;
249     secret_octal, Octal;
250     secret_binary, Binary;
251     secret_lower_hex, LowerHex;
252     secret_upper_hex, UpperHex;
253     secret_string, String;
254     secret_pointer, Pointer;
255     secret_float, Float;
256     secret_lower_exp, LowerExp;
257     secret_upper_exp, UpperExp;
258 }
259
260 /// The `write` function takes an output stream, a precompiled format string,
261 /// and a list of arguments. The arguments will be formatted according to the
262 /// specified format string into the output stream provided.
263 ///
264 /// # Arguments
265 ///
266 ///   * output - the buffer to write output to
267 ///   * args - the precompiled arguments generated by `format_args!`
268 pub fn write(output: &mut FormatWriter, args: &Arguments) -> Result {
269     let mut formatter = Formatter {
270         flags: 0,
271         width: None,
272         precision: None,
273         buf: output,
274         align: rt::AlignUnknown,
275         fill: ' ',
276         args: args.args,
277         curarg: args.args.iter(),
278     };
279     for piece in args.fmt.iter() {
280         try!(formatter.run(piece));
281     }
282     Ok(())
283 }
284
285 impl<'a> Formatter<'a> {
286
287     // First up is the collection of functions used to execute a format string
288     // at runtime. This consumes all of the compile-time statics generated by
289     // the format! syntax extension.
290
291     fn run(&mut self, piece: &rt::Piece) -> Result {
292         match *piece {
293             rt::String(s) => self.buf.write(s.as_bytes()),
294             rt::Argument(ref arg) => {
295                 // Fill in the format parameters into the formatter
296                 self.fill = arg.format.fill;
297                 self.align = arg.format.align;
298                 self.flags = arg.format.flags;
299                 self.width = self.getcount(&arg.format.width);
300                 self.precision = self.getcount(&arg.format.precision);
301
302                 // Extract the correct argument
303                 let value = match arg.position {
304                     rt::ArgumentNext => { *self.curarg.next().unwrap() }
305                     rt::ArgumentIs(i) => self.args[i],
306                 };
307
308                 // Then actually do some printing
309                 (value.formatter)(value.value, self)
310             }
311         }
312     }
313
314     fn getcount(&mut self, cnt: &rt::Count) -> Option<uint> {
315         match *cnt {
316             rt::CountIs(n) => { Some(n) }
317             rt::CountImplied => { None }
318             rt::CountIsParam(i) => {
319                 let v = self.args[i].value;
320                 unsafe { Some(*(v as *const _ as *const uint)) }
321             }
322             rt::CountIsNextParam => {
323                 let v = self.curarg.next().unwrap().value;
324                 unsafe { Some(*(v as *const _ as *const uint)) }
325             }
326         }
327     }
328
329     // Helper methods used for padding and processing formatting arguments that
330     // all formatting traits can use.
331
332     /// Performs the correct padding for an integer which has already been
333     /// emitted into a byte-array. The byte-array should *not* contain the sign
334     /// for the integer, that will be added by this method.
335     ///
336     /// # Arguments
337     ///
338     /// * is_positive - whether the original integer was positive or not.
339     /// * prefix - if the '#' character (FlagAlternate) is provided, this
340     ///   is the prefix to put in front of the number.
341     /// * buf - the byte array that the number has been formatted into
342     ///
343     /// This function will correctly account for the flags provided as well as
344     /// the minimum width. It will not take precision into account.
345     pub fn pad_integral(&mut self, is_positive: bool, prefix: &str,
346                         buf: &[u8]) -> Result {
347         use fmt::rt::{FlagAlternate, FlagSignPlus, FlagSignAwareZeroPad};
348
349         let mut width = buf.len();
350
351         let mut sign = None;
352         if !is_positive {
353             sign = Some('-'); width += 1;
354         } else if self.flags & (1 << (FlagSignPlus as uint)) != 0 {
355             sign = Some('+'); width += 1;
356         }
357
358         let mut prefixed = false;
359         if self.flags & (1 << (FlagAlternate as uint)) != 0 {
360             prefixed = true; width += prefix.len();
361         }
362
363         // Writes the sign if it exists, and then the prefix if it was requested
364         let write_prefix = |f: &mut Formatter| {
365             for c in sign.move_iter() {
366                 let mut b = [0, ..4];
367                 let n = c.encode_utf8(b);
368                 try!(f.buf.write(b.slice_to(n)));
369             }
370             if prefixed { f.buf.write(prefix.as_bytes()) }
371             else { Ok(()) }
372         };
373
374         // The `width` field is more of a `min-width` parameter at this point.
375         match self.width {
376             // If there's no minimum length requirements then we can just
377             // write the bytes.
378             None => {
379                 try!(write_prefix(self)); self.buf.write(buf)
380             }
381             // Check if we're over the minimum width, if so then we can also
382             // just write the bytes.
383             Some(min) if width >= min => {
384                 try!(write_prefix(self)); self.buf.write(buf)
385             }
386             // The sign and prefix goes before the padding if the fill character
387             // is zero
388             Some(min) if self.flags & (1 << (FlagSignAwareZeroPad as uint)) != 0 => {
389                 self.fill = '0';
390                 try!(write_prefix(self));
391                 self.with_padding(min - width, rt::AlignRight, |f| f.buf.write(buf))
392             }
393             // Otherwise, the sign and prefix goes after the padding
394             Some(min) => {
395                 self.with_padding(min - width, rt::AlignRight, |f| {
396                     try!(write_prefix(f)); f.buf.write(buf)
397                 })
398             }
399         }
400     }
401
402     /// This function takes a string slice and emits it to the internal buffer
403     /// after applying the relevant formatting flags specified. The flags
404     /// recognized for generic strings are:
405     ///
406     /// * width - the minimum width of what to emit
407     /// * fill/align - what to emit and where to emit it if the string
408     ///                provided needs to be padded
409     /// * precision - the maximum length to emit, the string is truncated if it
410     ///               is longer than this length
411     ///
412     /// Notably this function ignored the `flag` parameters
413     pub fn pad(&mut self, s: &str) -> Result {
414         // Make sure there's a fast path up front
415         if self.width.is_none() && self.precision.is_none() {
416             return self.buf.write(s.as_bytes());
417         }
418         // The `precision` field can be interpreted as a `max-width` for the
419         // string being formatted
420         match self.precision {
421             Some(max) => {
422                 // If there's a maximum width and our string is longer than
423                 // that, then we must always have truncation. This is the only
424                 // case where the maximum length will matter.
425                 let char_len = s.char_len();
426                 if char_len >= max {
427                     let nchars = ::cmp::min(max, char_len);
428                     return self.buf.write(s.slice_chars(0, nchars).as_bytes());
429                 }
430             }
431             None => {}
432         }
433         // The `width` field is more of a `min-width` parameter at this point.
434         match self.width {
435             // If we're under the maximum length, and there's no minimum length
436             // requirements, then we can just emit the string
437             None => self.buf.write(s.as_bytes()),
438             // If we're under the maximum width, check if we're over the minimum
439             // width, if so it's as easy as just emitting the string.
440             Some(width) if s.char_len() >= width => {
441                 self.buf.write(s.as_bytes())
442             }
443             // If we're under both the maximum and the minimum width, then fill
444             // up the minimum width with the specified string + some alignment.
445             Some(width) => {
446                 self.with_padding(width - s.len(), rt::AlignLeft, |me| {
447                     me.buf.write(s.as_bytes())
448                 })
449             }
450         }
451     }
452
453     /// Runs a callback, emitting the correct padding either before or
454     /// afterwards depending on whether right or left alignment is requested.
455     fn with_padding(&mut self,
456                     padding: uint,
457                     default: rt::Alignment,
458                     f: |&mut Formatter| -> Result) -> Result {
459         let align = match self.align {
460             rt::AlignUnknown => default,
461             rt::AlignLeft | rt::AlignRight => self.align
462         };
463         if align == rt::AlignLeft {
464             try!(f(self));
465         }
466         let mut fill = [0u8, ..4];
467         let len = self.fill.encode_utf8(fill);
468         for _ in range(0, padding) {
469             try!(self.buf.write(fill.slice_to(len)));
470         }
471         if align == rt::AlignRight {
472             try!(f(self));
473         }
474         Ok(())
475     }
476
477     /// Writes some data to the underlying buffer contained within this
478     /// formatter.
479     pub fn write(&mut self, data: &[u8]) -> Result {
480         self.buf.write(data)
481     }
482
483     /// Writes some formatted information into this instance
484     pub fn write_fmt(&mut self, fmt: &Arguments) -> Result {
485         write(self.buf, fmt)
486     }
487 }
488
489 /// This is a function which calls are emitted to by the compiler itself to
490 /// create the Argument structures that are passed into the `format` function.
491 #[doc(hidden)] #[inline]
492 pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
493                        t: &'a T) -> Argument<'a> {
494     unsafe {
495         Argument {
496             formatter: mem::transmute(f),
497             value: mem::transmute(t)
498         }
499     }
500 }
501
502 /// When the compiler determines that the type of an argument *must* be a string
503 /// (such as for select), then it invokes this method.
504 #[doc(hidden)] #[inline]
505 pub fn argumentstr<'a>(s: &'a &str) -> Argument<'a> {
506     argument(secret_string, s)
507 }
508
509 /// When the compiler determines that the type of an argument *must* be a uint
510 /// (such as for plural), then it invokes this method.
511 #[doc(hidden)] #[inline]
512 pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
513     argument(secret_unsigned, s)
514 }
515
516 // Implementations of the core formatting traits
517
518 impl<'a, T: Show> Show for &'a T {
519     fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
520 }
521 impl<'a, T: Show> Show for &'a mut T {
522     fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
523 }
524 impl<'a> Show for &'a Show {
525     fn fmt(&self, f: &mut Formatter) -> Result { (*self).fmt(f) }
526 }
527
528 impl Bool for bool {
529     fn fmt(&self, f: &mut Formatter) -> Result {
530         secret_string(&(if *self {"true"} else {"false"}), f)
531     }
532 }
533
534 impl<'a, T: str::Str> String for T {
535     fn fmt(&self, f: &mut Formatter) -> Result {
536         f.pad(self.as_slice())
537     }
538 }
539
540 impl Char for char {
541     fn fmt(&self, f: &mut Formatter) -> Result {
542         let mut utf8 = [0u8, ..4];
543         let amt = self.encode_utf8(utf8);
544         let s: &str = unsafe { mem::transmute(utf8.slice_to(amt)) };
545         secret_string(&s, f)
546     }
547 }
548
549 impl<T> Pointer for *const T {
550     fn fmt(&self, f: &mut Formatter) -> Result {
551         f.flags |= 1 << (rt::FlagAlternate as uint);
552         secret_lower_hex::<uint>(&(*self as uint), f)
553     }
554 }
555 impl<T> Pointer for *mut T {
556     fn fmt(&self, f: &mut Formatter) -> Result {
557         secret_pointer::<*const T>(&(*self as *const T), f)
558     }
559 }
560 impl<'a, T> Pointer for &'a T {
561     fn fmt(&self, f: &mut Formatter) -> Result {
562         secret_pointer::<*const T>(&(&**self as *const T), f)
563     }
564 }
565 impl<'a, T> Pointer for &'a mut T {
566     fn fmt(&self, f: &mut Formatter) -> Result {
567         secret_pointer::<*const T>(&(&**self as *const T), f)
568     }
569 }
570
571 macro_rules! floating(($ty:ident) => {
572     impl Float for $ty {
573         fn fmt(&self, fmt: &mut Formatter) -> Result {
574             use num::Signed;
575
576             let digits = match fmt.precision {
577                 Some(i) => float::DigExact(i),
578                 None => float::DigMax(6),
579             };
580             float::float_to_str_bytes_common(self.abs(),
581                                              10,
582                                              true,
583                                              float::SignNeg,
584                                              digits,
585                                              float::ExpNone,
586                                              false,
587                                              |bytes| {
588                 fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
589             })
590         }
591     }
592
593     impl LowerExp for $ty {
594         fn fmt(&self, fmt: &mut Formatter) -> Result {
595             use num::Signed;
596
597             let digits = match fmt.precision {
598                 Some(i) => float::DigExact(i),
599                 None => float::DigMax(6),
600             };
601             float::float_to_str_bytes_common(self.abs(),
602                                              10,
603                                              true,
604                                              float::SignNeg,
605                                              digits,
606                                              float::ExpDec,
607                                              false,
608                                              |bytes| {
609                 fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
610             })
611         }
612     }
613
614     impl UpperExp for $ty {
615         fn fmt(&self, fmt: &mut Formatter) -> Result {
616             use num::Signed;
617
618             let digits = match fmt.precision {
619                 Some(i) => float::DigExact(i),
620                 None => float::DigMax(6),
621             };
622             float::float_to_str_bytes_common(self.abs(),
623                                              10,
624                                              true,
625                                              float::SignNeg,
626                                              digits,
627                                              float::ExpDec,
628                                              true,
629                                              |bytes| {
630                 fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
631             })
632         }
633     }
634 })
635 floating!(f32)
636 floating!(f64)
637
638 // Implementation of Show for various core types
639
640 macro_rules! delegate(($ty:ty to $other:ident) => {
641     impl<'a> Show for $ty {
642         fn fmt(&self, f: &mut Formatter) -> Result {
643             (concat_idents!(secret_, $other)(self, f))
644         }
645     }
646 })
647 delegate!(&'a str to string)
648 delegate!(bool to bool)
649 delegate!(char to char)
650 delegate!(f32 to float)
651 delegate!(f64 to float)
652
653 impl<T> Show for *const T {
654     fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
655 }
656 impl<T> Show for *mut T {
657     fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
658 }
659
660 macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
661
662 macro_rules! tuple (
663     () => ();
664     ( $($name:ident,)+ ) => (
665         impl<$($name:Show),*> Show for ($($name,)*) {
666             #[allow(uppercase_variables, dead_assignment)]
667             fn fmt(&self, f: &mut Formatter) -> Result {
668                 try!(write!(f, "("));
669                 let ($(ref $name,)*) = *self;
670                 let mut n = 0i;
671                 $(
672                     if n > 0 {
673                         try!(write!(f, ", "));
674                     }
675                     try!(write!(f, "{}", *$name));
676                     n += 1;
677                 )*
678                 if n == 1 {
679                     try!(write!(f, ","));
680                 }
681                 write!(f, ")")
682             }
683         }
684         peel!($($name,)*)
685     )
686 )
687
688 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
689
690 impl<'a> Show for &'a any::Any {
691     fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") }
692 }
693
694 impl<'a, T: Show> Show for &'a [T] {
695     fn fmt(&self, f: &mut Formatter) -> Result {
696         if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
697             try!(write!(f, "["));
698         }
699         let mut is_first = true;
700         for x in self.iter() {
701             if is_first {
702                 is_first = false;
703             } else {
704                 try!(write!(f, ", "));
705             }
706             try!(write!(f, "{}", *x))
707         }
708         if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
709             try!(write!(f, "]"));
710         }
711         Ok(())
712     }
713 }
714
715 impl<'a, T: Show> Show for &'a mut [T] {
716     fn fmt(&self, f: &mut Formatter) -> Result {
717         secret_show(&self.as_slice(), f)
718     }
719 }
720
721 impl Show for () {
722     fn fmt(&self, f: &mut Formatter) -> Result {
723         f.pad("()")
724     }
725 }
726
727 impl<T: Copy + Show> Show for Cell<T> {
728     fn fmt(&self, f: &mut Formatter) -> Result {
729         write!(f, "Cell {{ value: {} }}", self.get())
730     }
731 }
732
733 impl<'b, T: Show> Show for Ref<'b, T> {
734     fn fmt(&self, f: &mut Formatter) -> Result {
735         (**self).fmt(f)
736     }
737 }
738
739 impl<'b, T: Show> Show for RefMut<'b, T> {
740     fn fmt(&self, f: &mut Formatter) -> Result {
741         (*(self.deref())).fmt(f)
742     }
743 }
744
745 // If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
746 // it's a lot easier than creating all of the rt::Piece structures here.