about summary refs log tree commit diff
path: root/src/libstd/vec.rs
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2014-02-13 06:41:34 +1100
committerAlex Crichton <alex@alexcrichton.com>2014-02-13 12:54:01 -0800
commit957fcb3f54d4a3ee2d9ed67a532bb48e34753094 (patch)
tree2fa9ee5589b0c98416e0765fcbc81e1aa4b0a8a2 /src/libstd/vec.rs
parent8a5b938b3bb408c24f0f1d6fbd6bd0ba011f60a1 (diff)
downloadrust-957fcb3f54d4a3ee2d9ed67a532bb48e34753094.tar.gz
rust-957fcb3f54d4a3ee2d9ed67a532bb48e34753094.zip
Add some missing Show implementations in libstd
Diffstat (limited to 'src/libstd/vec.rs')
-rw-r--r--src/libstd/vec.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 2acafecf957..75993cdada2 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -108,6 +108,7 @@ use container::{Container, Mutable};
 use cmp::{Eq, TotalOrd, Ordering, Less, Equal, Greater};
 use cmp;
 use default::Default;
+#[cfg(not(stage0))] use fmt;
 use iter::*;
 use num::{Integer, CheckedAdd, Saturating, checked_next_power_of_two};
 use option::{None, Option, Some};
@@ -115,6 +116,7 @@ use ptr::to_unsafe_ptr;
 use ptr;
 use ptr::RawPtr;
 use rt::global_heap::{malloc_raw, realloc_raw, exchange_free};
+#[cfg(not(stage0))] use result::{Ok, Err};
 use mem;
 use mem::size_of;
 use kinds::marker;
@@ -2640,6 +2642,30 @@ impl<A: DeepClone> DeepClone for ~[A] {
     }
 }
 
+#[cfg(not(stage0))]
+impl<'a, T: fmt::Show> fmt::Show for &'a [T] {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        if_ok!(write!(f.buf, "["));
+        let mut is_first = true;
+        for x in self.iter() {
+            if is_first {
+                is_first = false;
+            } else {
+                if_ok!(write!(f.buf, ", "));
+            }
+            if_ok!(write!(f.buf, "{}", *x))
+        }
+        write!(f.buf, "]")
+    }
+}
+
+#[cfg(not(stage0))]
+impl<T: fmt::Show> fmt::Show for ~[T] {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        self.as_slice().fmt(f)
+    }
+}
+
 // This works because every lifetime is a sub-lifetime of 'static
 impl<'a, A> Default for &'a [A] {
     fn default() -> &'a [A] { &'a [] }
@@ -4050,6 +4076,22 @@ mod tests {
     }
 
     #[test]
+    fn test_show() {
+        macro_rules! test_show_vec(
+            ($x:expr, $x_str:expr) => ({
+                let (x, x_str) = ($x, $x_str);
+                assert_eq!(format!("{}", x), x_str);
+                assert_eq!(format!("{}", x.as_slice()), x_str);
+            })
+        )
+        let empty: ~[int] = ~[];
+        test_show_vec!(empty, ~"[]");
+        test_show_vec!(~[1], ~"[1]");
+        test_show_vec!(~[1, 2, 3], ~"[1, 2, 3]");
+        test_show_vec!(~[~[], ~[1u], ~[1u, 1u]], ~"[[], [1], [1, 1]]");
+    }
+
+    #[test]
     fn test_vec_default() {
         use default::Default;
         macro_rules! t (