]> git.lizzy.rs Git - rust.git/commitdiff
auto merge of #14115 : alexcrichton/rust/core-fmt, r=brson
authorbors <bors@rust-lang.org>
Fri, 16 May 2014 06:26:26 +0000 (23:26 -0700)
committerbors <bors@rust-lang.org>
Fri, 16 May 2014 06:26:26 +0000 (23:26 -0700)
This was a more difficult change than I thought it would be, and it is unfortunately a breaking change rather than a drop-in replacement. Most of the rationale can be found in the third commit.

cc #13851

119 files changed:
src/compiletest/common.rs
src/libcollections/btree.rs
src/libcollections/hashmap.rs
src/libcollections/lru_cache.rs
src/libcore/any.rs
src/libcore/cell.rs
src/libcore/char.rs
src/libcore/clone.rs
src/libcore/cmp.rs
src/libcore/failure.rs
src/libcore/fmt/float.rs [new file with mode: 0644]
src/libcore/fmt/mod.rs [new file with mode: 0644]
src/libcore/fmt/num.rs [new file with mode: 0644]
src/libcore/fmt/rt.rs [new file with mode: 0644]
src/libcore/intrinsics.rs
src/libcore/iter.rs
src/libcore/lib.rs
src/libcore/macros.rs
src/libcore/num/f32.rs
src/libcore/num/f64.rs
src/libcore/num/mod.rs
src/libcore/option.rs
src/libcore/prelude.rs
src/libcore/ptr.rs
src/libcore/result.rs
src/libcore/should_not_exist.rs
src/libcore/str.rs
src/libgreen/macros.rs
src/liblog/lib.rs
src/libnum/bigint.rs
src/libnum/complex.rs
src/libnum/rational.rs
src/libregex/parse.rs
src/libregex/re.rs
src/librustc/metadata/tyencode.rs
src/librustc/middle/liveness.rs
src/librustc/middle/ty.rs
src/librustc/middle/typeck/coherence.rs
src/librustc/middle/typeck/variance.rs
src/librustdoc/html/escape.rs
src/librustdoc/html/format.rs
src/librustdoc/html/layout.rs
src/librustdoc/html/markdown.rs
src/librustdoc/html/render.rs
src/librustdoc/html/toc.rs
src/librustuv/lib.rs
src/librustuv/macros.rs
src/libsemver/lib.rs
src/libserialize/base64.rs
src/libserialize/hex.rs
src/libserialize/json.rs
src/libstd/bitflags.rs
src/libstd/fmt.rs [new file with mode: 0644]
src/libstd/fmt/mod.rs [deleted file]
src/libstd/fmt/num.rs [deleted file]
src/libstd/fmt/rt.rs [deleted file]
src/libstd/io/buffered.rs
src/libstd/io/mod.rs
src/libstd/io/net/ip.rs
src/libstd/io/net/udp.rs
src/libstd/io/process.rs
src/libstd/io/stdio.rs
src/libstd/lib.rs
src/libstd/macros.rs
src/libstd/num/f32.rs
src/libstd/num/f64.rs
src/libstd/num/i16.rs
src/libstd/num/i32.rs
src/libstd/num/i64.rs
src/libstd/num/i8.rs
src/libstd/num/int.rs
src/libstd/num/int_macros.rs
src/libstd/num/mod.rs
src/libstd/num/strconv.rs
src/libstd/num/u16.rs
src/libstd/num/u32.rs
src/libstd/num/u64.rs
src/libstd/num/u8.rs
src/libstd/num/uint.rs
src/libstd/num/uint_macros.rs
src/libstd/os.rs
src/libstd/owned.rs
src/libstd/prelude.rs
src/libstd/repr.rs
src/libstd/result.rs [deleted file]
src/libstd/rt/unwind.rs
src/libstd/rt/util.rs
src/libstd/task.rs
src/libsyntax/abi.rs
src/libsyntax/ast.rs
src/libsyntax/ast_map.rs
src/libsyntax/crateid.rs
src/libsyntax/ext/base.rs
src/libsyntax/ext/deriving/show.rs
src/libsyntax/ext/format.rs
src/libsyntax/parse/token.rs
src/libtest/stats.rs
src/liburl/lib.rs
src/libuuid/lib.rs
src/test/auxiliary/cci_class_cast.rs
src/test/bench/shootout-chameneos-redux.rs
src/test/compile-fail-fulldeps/gated-phase.rs [new file with mode: 0644]
src/test/compile-fail-fulldeps/macro-crate-unexported-macro.rs [new file with mode: 0644]
src/test/compile-fail-fulldeps/macro-crate-unknown-crate.rs [new file with mode: 0644]
src/test/compile-fail-fulldeps/phase-syntax-doesnt-resolve.rs [new file with mode: 0644]
src/test/compile-fail/gated-phase.rs [deleted file]
src/test/compile-fail/ifmt-unimpl.rs
src/test/compile-fail/macro-crate-unexported-macro.rs [deleted file]
src/test/compile-fail/macro-crate-unknown-crate.rs [deleted file]
src/test/compile-fail/phase-syntax-doesnt-resolve.rs [deleted file]
src/test/compile-fail/use-after-move-implicity-coerced-object.rs
src/test/run-pass/capturing-logging.rs
src/test/run-pass/class-separate-impl.rs
src/test/run-pass/colorful-write-macros.rs
src/test/run-pass/deriving-show-2.rs
src/test/run-pass/ifmt.rs
src/test/run-pass/issue-2904.rs
src/test/run-pass/issue-3563-3.rs
src/test/run-pass/new-impl-syntax.rs

index b1f1e69c5a1874da7e6e877bfef2b31892c34d7a..695c0420e2b109ad0e2e0c1cbd6110da7ac3e59f 100644 (file)
@@ -41,15 +41,15 @@ fn from_str(s: &str) -> Option<Mode> {
 impl fmt::Show for Mode {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let msg = match *self {
-          CompileFail => "compile-fail",
-          RunFail => "run-fail",
-          RunPass => "run-pass",
-          Pretty => "pretty",
-          DebugInfoGdb => "debuginfo-gdb",
-          DebugInfoLldb => "debuginfo-lldb",
-          Codegen => "codegen",
+            CompileFail => "compile-fail",
+            RunFail => "run-fail",
+            RunPass => "run-pass",
+            Pretty => "pretty",
+            DebugInfoGdb => "debuginfo-gdb",
+            DebugInfoLldb => "debuginfo-lldb",
+            Codegen => "codegen",
         };
-        write!(f.buf, "{}", msg)
+        msg.fmt(f)
     }
 }
 
index 245040d791cff1cd3cee12c9c448a460588c018e..ba83ad8d37c9a77dfd901f4eae13afbc97b210c8 100644 (file)
@@ -425,8 +425,8 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Leaf<K, V> {
     ///Returns a string representation of a Leaf.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         for (i, s) in self.elts.iter().enumerate() {
-            if i != 0 { try!(write!(f.buf, " // ")) }
-            try!(write!(f.buf, "{}", *s))
+            if i != 0 { try!(write!(f, " // ")) }
+            try!(write!(f, "{}", *s))
         }
         Ok(())
     }
@@ -654,10 +654,10 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Branch<K, V> {
     ///Returns a string representation of a Branch.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         for (i, s) in self.elts.iter().enumerate() {
-            if i != 0 { try!(write!(f.buf, " // ")) }
-            try!(write!(f.buf, "{}", *s))
+            if i != 0 { try!(write!(f, " // ")) }
+            try!(write!(f, "{}", *s))
         }
-        write!(f.buf, " // rightmost child: ({}) ", *self.rightmost_child)
+        write!(f, " // rightmost child: ({}) ", *self.rightmost_child)
     }
 }
 
@@ -715,7 +715,7 @@ fn cmp(&self, other: &LeafElt<K, V>) -> Ordering {
 impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for LeafElt<K, V> {
     ///Returns a string representation of a LeafElt.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "Key: {}, value: {};", self.key, self.value)
+        write!(f, "Key: {}, value: {};", self.key, self.value)
     }
 }
 
@@ -765,7 +765,7 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for BranchElt<K, V> {
     /// Returns string containing key, value, and child (which should recur to a
     /// leaf) Consider changing in future to be more readable.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "Key: {}, value: {}, (child: {})",
+        write!(f, "Key: {}, value: {}, (child: {})",
                self.key, self.value, *self.left)
     }
 }
index 4b9c8ccadd2475629911767321643bc82a63a4b7..4259f458e00643baacdd6ac9fb2435d4390a025c 100644 (file)
@@ -1418,14 +1418,14 @@ fn eq(&self, other: &HashMap<K, V, H>) -> bool {
 
 impl<K: TotalEq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, r"\{"));
+        try!(write!(f, r"\{"));
 
         for (i, (k, v)) in self.iter().enumerate() {
-            if i != 0 { try!(write!(f.buf, ", ")); }
-            try!(write!(f.buf, "{}: {}", *k, *v));
+            if i != 0 { try!(write!(f, ", ")); }
+            try!(write!(f, "{}: {}", *k, *v));
         }
 
-        write!(f.buf, r"\}")
+        write!(f, r"\}")
     }
 }
 
@@ -1605,14 +1605,14 @@ pub fn union<'a>(&'a self, other: &'a HashSet<T, H>)
 
 impl<T: TotalEq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, r"\{"));
+        try!(write!(f, r"\{"));
 
         for (i, x) in self.iter().enumerate() {
-            if i != 0 { try!(write!(f.buf, ", ")); }
-            try!(write!(f.buf, "{}", *x));
+            if i != 0 { try!(write!(f, ", ")); }
+            try!(write!(f, "{}", *x));
         }
 
-        write!(f.buf, r"\}")
+        write!(f, r"\}")
     }
 }
 
index 72eefe4f44d66d1a28ca72dd9f2e086c728debc7..8fdc0e095bf411096cb4caa6bc162fb4182d2e65 100644 (file)
@@ -205,20 +205,20 @@ impl<A: fmt::Show + Hash + TotalEq, B: fmt::Show> fmt::Show for LruCache<A, B> {
     /// Return a string that lists the key-value pairs from most-recently
     /// used to least-recently used.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, r"\{"));
+        try!(write!(f, r"\{"));
         let mut cur = self.head;
         for i in range(0, self.len()) {
-            if i > 0 { try!(write!(f.buf, ", ")) }
+            if i > 0 { try!(write!(f, ", ")) }
             unsafe {
                 cur = (*cur).next;
-                try!(write!(f.buf, "{}", (*cur).key));
+                try!(write!(f, "{}", (*cur).key));
             }
-            try!(write!(f.buf, ": "));
+            try!(write!(f, ": "));
             unsafe {
-                try!(write!(f.buf, "{}", (*cur).value));
+                try!(write!(f, "{}", (*cur).value));
             }
         }
-        write!(f.buf, r"\}")
+        write!(f, r"\}")
     }
 }
 
index 70cd46dcfa2b45ce8b9fc03685ba3c7b8226fe35..61c1193e515973f939e9daf8ede42269079de50b 100644 (file)
@@ -166,12 +166,12 @@ fn any_as_ref() {
 
         match a.as_ref::<uint>() {
             Some(&5) => {}
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
 
         match a.as_ref::<Test>() {
             None => {}
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
     }
 
@@ -189,7 +189,7 @@ fn any_as_mut() {
                 assert_eq!(*x, 5u);
                 *x = 612;
             }
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
 
         match b_r.as_mut::<uint>() {
@@ -197,27 +197,27 @@ fn any_as_mut() {
                 assert_eq!(*x, 7u);
                 *x = 413;
             }
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
 
         match a_r.as_mut::<Test>() {
             None => (),
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
 
         match b_r.as_mut::<Test>() {
             None => (),
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
 
         match a_r.as_mut::<uint>() {
             Some(&612) => {}
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
 
         match b_r.as_mut::<uint>() {
             Some(&413) => {}
-            x => fail!("Unexpected value {:?}", x)
+            x => fail!("Unexpected value {}", x)
         }
     }
 
@@ -229,11 +229,11 @@ fn any_move() {
         let b = box Test as Box<Any>;
 
         match a.move::<uint>() {
-            Ok(a) => { assert_eq!(a, box 8u); }
+            Ok(a) => { assert!(a == box 8u); }
             Err(..) => fail!()
         }
         match b.move::<Test>() {
-            Ok(a) => { assert_eq!(a, box Test); }
+            Ok(a) => { assert!(a == box Test); }
             Err(..) => fail!()
         }
 
@@ -246,13 +246,14 @@ fn any_move() {
 
     #[test]
     fn test_show() {
-        let a = box 8u as Box<::realcore::any::Any>;
-        let b = box Test as Box<::realcore::any::Any>;
-        assert_eq!(format!("{}", a), "Box<Any>".to_owned());
-        assert_eq!(format!("{}", b), "Box<Any>".to_owned());
-
-        let a = &8u as &::realcore::any::Any;
-        let b = &Test as &::realcore::any::Any;
+        use realstd::to_str::ToStr;
+        let a = box 8u as Box<::realstd::any::Any>;
+        let b = box Test as Box<::realstd::any::Any>;
+        assert_eq!(a.to_str(), "Box<Any>".to_owned());
+        assert_eq!(b.to_str(), "Box<Any>".to_owned());
+
+        let a = &8u as &Any;
+        let b = &Test as &Any;
         assert_eq!(format!("{}", a), "&Any".to_owned());
         assert_eq!(format!("{}", b), "&Any".to_owned());
     }
index 8b3494f3127339cc83beaa7f592393a4364c5732..d42ad49485f6dae9927a33e8df4c68afc394fe68 100644 (file)
@@ -255,7 +255,7 @@ fn smoketest_cell() {
     fn cell_has_sensible_show() {
         use str::StrSlice;
 
-        let x = ::realcore::cell::Cell::new("foo bar");
+        let x = Cell::new("foo bar");
         assert!(format!("{}", x).contains(x.get()));
 
         x.set("baz qux");
index 934483dbed423d1337d4544fdde200820d7aa3fa..6e9d4c9bafbf616be4ae623ac8c9fad8ea641f87 100644 (file)
@@ -633,9 +633,9 @@ fn default() -> char { '\x00' }
 mod test {
     use super::{escape_unicode, escape_default};
 
-    use realcore::char::Char;
+    use char::Char;
     use slice::ImmutableVector;
-    use realstd::option::{Some, None};
+    use option::{Some, None};
     use realstd::strbuf::StrBuf;
     use realstd::str::StrAllocating;
 
index cd66beabc12de3fce964966012ded57abb79a347..c7befe2f4b1e0a64d23bd7c162577b79f067ef7e 100644 (file)
@@ -131,7 +131,7 @@ fn realclone_from<T: ::realstd::clone::Clone>(t1: &mut T, t2: &T) {
     fn test_owned_clone() {
         let a = box 5i;
         let b: Box<int> = realclone(&a);
-        assert_eq!(a, b);
+        assert!(a == b);
     }
 
     #[test]
index bf02f053336bb9822b6189d4e565e6630a9a94f5..a50108607ce71cf46d1af2a618714e83317ebac9 100644 (file)
@@ -82,7 +82,7 @@ fn assert_receiver_is_total_eq(&self) {}
 }
 
 /// An ordering is, e.g, a result of a comparison between two values.
-#[deriving(Clone, Eq)]
+#[deriving(Clone, Eq, Show)]
 pub enum Ordering {
    /// An ordering where a compared value is less [than another].
    Less = -1,
index 8a28f7b13928fc1d2287414a749eb68587fd4413..c4a2c9a609925fd15b9a565801c8399ad04d1bba 100644 (file)
@@ -9,21 +9,28 @@
 // except according to those terms.
 
 //! Failure support for libcore
+//!
+//! The core library cannot define failure, but it does *declare* failure. This
+//! means that the functions inside of libcore are allowed to fail, but to be
+//! useful an upstream crate must define failure for libcore to use. The current
+//! interface for failure is:
+//!
+//!     fn begin_unwind(fmt: &fmt::Arguments, file: &str, line: uint) -> !;
+//!
+//! This definition allows for failing with any general message, but it does not
+//! allow for failing with a `~Any` value. The reason for this is that libcore
+//! is not allowed to allocate.
+//!
+//! This module contains a few other failure functions, but these are just the
+//! necessary lang items for the compiler. All failure is funneled through this
+//! one function. Currently, the actual symbol is declared in the standard
+//! library, but the location of this may change over time.
 
 #![allow(dead_code, missing_doc)]
 
 #[cfg(not(test))]
 use str::raw::c_str_to_static_slice;
-
-// FIXME: Once std::fmt is in libcore, all of these functions should delegate
-//        to a common failure function with this signature:
-//
-//          extern {
-//              fn rust_unwind(f: &fmt::Arguments, file: &str, line: uint) -> !;
-//          }
-//
-//        Each of these functions can create a temporary fmt::Arguments
-//        structure to pass to this function.
+use fmt;
 
 #[cold] #[inline(never)] // this is the slow path, always
 #[lang="fail_"]
@@ -32,7 +39,11 @@ fn fail_(expr: *u8, file: *u8, line: uint) -> ! {
     unsafe {
         let expr = c_str_to_static_slice(expr as *i8);
         let file = c_str_to_static_slice(file as *i8);
-        begin_unwind(expr, file, line)
+        format_args!(|args| -> () {
+            begin_unwind(args, file, line);
+        }, "{}", expr);
+
+        loop {}
     }
 }
 
@@ -40,16 +51,19 @@ fn fail_(expr: *u8, file: *u8, line: uint) -> ! {
 #[lang="fail_bounds_check"]
 #[cfg(not(test))]
 fn fail_bounds_check(file: *u8, line: uint, index: uint, len: uint) -> ! {
-    #[allow(ctypes)]
-    extern { fn rust_fail_bounds_check(file: *u8, line: uint,
-                                       index: uint, len: uint,) -> !; }
-    unsafe { rust_fail_bounds_check(file, line, index, len) }
+    let file = unsafe { c_str_to_static_slice(file as *i8) };
+    format_args!(|args| -> () {
+        begin_unwind(args, file, line);
+    }, "index out of bounds: the len is {} but the index is {}", len, index);
+    loop {}
 }
 
 #[cold]
-pub fn begin_unwind(msg: &str, file: &'static str, line: uint) -> ! {
+pub fn begin_unwind(fmt: &fmt::Arguments, file: &'static str, line: uint) -> ! {
+    // FIXME: this should be a proper lang item, it should not just be some
+    //        undefined symbol sitting in the middle of nowhere.
     #[allow(ctypes)]
-    extern { fn rust_begin_unwind(msg: &str, file: &'static str,
+    extern { fn rust_begin_unwind(fmt: &fmt::Arguments, file: &'static str,
                                   line: uint) -> !; }
-    unsafe { rust_begin_unwind(msg, file, line) }
+    unsafe { rust_begin_unwind(fmt, file, line) }
 }
diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs
new file mode 100644 (file)
index 0000000..e5fb148
--- /dev/null
@@ -0,0 +1,370 @@
+// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(missing_doc)]
+
+use char;
+use container::Container;
+use fmt;
+use iter::{Iterator, range, DoubleEndedIterator};
+use num::{Float, FPNaN, FPInfinite, ToPrimitive, Primitive};
+use num::{Zero, One, cast};
+use option::{None, Some};
+use result::Ok;
+use slice::{ImmutableVector, MutableVector};
+use slice;
+use str::StrSlice;
+
+/// A flag that specifies whether to use exponential (scientific) notation.
+pub enum ExponentFormat {
+    /// Do not use exponential notation.
+    ExpNone,
+    /// Use exponential notation with the exponent having a base of 10 and the
+    /// exponent sign being `e` or `E`. For example, 1000 would be printed
+    /// 1e3.
+    ExpDec,
+    /// Use exponential notation with the exponent having a base of 2 and the
+    /// exponent sign being `p` or `P`. For example, 8 would be printed 1p3.
+    ExpBin,
+}
+
+/// The number of digits used for emitting the fractional part of a number, if
+/// any.
+pub enum SignificantDigits {
+    /// All calculable digits will be printed.
+    ///
+    /// Note that bignums or fractions may cause a surprisingly large number
+    /// of digits to be printed.
+    DigAll,
+
+    /// At most the given number of digits will be printed, truncating any
+    /// trailing zeroes.
+    DigMax(uint),
+
+    /// Precisely the given number of digits will be printed.
+    DigExact(uint)
+}
+
+/// How to emit the sign of a number.
+pub enum SignFormat {
+    /// No sign will be printed. The exponent sign will also be emitted.
+    SignNone,
+    /// `-` will be printed for negative values, but no sign will be emitted
+    /// for positive numbers.
+    SignNeg,
+    /// `+` will be printed for positive values, and `-` will be printed for
+    /// negative values.
+    SignAll,
+}
+
+static DIGIT_P_RADIX: uint = ('p' as uint) - ('a' as uint) + 11u;
+static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
+
+/**
+ * Converts a number to its string representation as a byte vector.
+ * This is meant to be a common base implementation for all numeric string
+ * conversion functions like `to_str()` or `to_str_radix()`.
+ *
+ * # Arguments
+ * - `num`           - The number to convert. Accepts any number that
+ *                     implements the numeric traits.
+ * - `radix`         - Base to use. Accepts only the values 2-36. If the exponential notation
+ *                     is used, then this base is only used for the significand. The exponent
+ *                     itself always printed using a base of 10.
+ * - `negative_zero` - Whether to treat the special value `-0` as
+ *                     `-0` or as `+0`.
+ * - `sign`          - How to emit the sign. See `SignFormat`.
+ * - `digits`        - The amount of digits to use for emitting the fractional
+ *                     part, if any. See `SignificantDigits`.
+ * - `exp_format`   - Whether or not to use the exponential (scientific) notation.
+ *                    See `ExponentFormat`.
+ * - `exp_capital`   - Whether or not to use a capital letter for the exponent sign, if
+ *                     exponential notation is desired.
+ * - `f`             - A closure to invoke with the bytes representing the
+ *                     float.
+ *
+ * # Failure
+ * - Fails if `radix` < 2 or `radix` > 36.
+ * - Fails if `radix` > 14 and `exp_format` is `ExpDec` due to conflict
+ *   between digit and exponent sign `'e'`.
+ * - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict
+ *   between digit and exponent sign `'p'`.
+ */
+pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
+    num: T,
+    radix: uint,
+    negative_zero: bool,
+    sign: SignFormat,
+    digits: SignificantDigits,
+    exp_format: ExponentFormat,
+    exp_upper: bool,
+    f: |&[u8]| -> U
+) -> U {
+    assert!(2 <= radix && radix <= 36);
+    match exp_format {
+        ExpDec if radix >= DIGIT_E_RADIX       // decimal exponent 'e'
+          => fail!("float_to_str_bytes_common: radix {} incompatible with \
+                    use of 'e' as decimal exponent", radix),
+        ExpBin if radix >= DIGIT_P_RADIX       // binary exponent 'p'
+          => fail!("float_to_str_bytes_common: radix {} incompatible with \
+                    use of 'p' as binary exponent", radix),
+        _ => ()
+    }
+
+    let _0: T = Zero::zero();
+    let _1: T = One::one();
+
+    match num.classify() {
+        FPNaN => return f("NaN".as_bytes()),
+        FPInfinite if num > _0 => {
+            return match sign {
+                SignAll => return f("+inf".as_bytes()),
+                _       => return f("inf".as_bytes()),
+            };
+        }
+        FPInfinite if num < _0 => {
+            return match sign {
+                SignNone => return f("inf".as_bytes()),
+                _        => return f("-inf".as_bytes()),
+            };
+        }
+        _ => {}
+    }
+
+    let neg = num < _0 || (negative_zero && _1 / num == Float::neg_infinity());
+    // For an f64 the exponent is in the range of [-1022, 1023] for base 2, so
+    // we may have up to that many digits. Give ourselves some extra wiggle room
+    // otherwise as well.
+    let mut buf = [0u8, ..1536];
+    let mut end = 0;
+    let radix_gen: T = cast(radix as int).unwrap();
+
+    let (num, exp) = match exp_format {
+        ExpNone => (num, 0i32),
+        ExpDec | ExpBin if num == _0 => (num, 0i32),
+        ExpDec | ExpBin => {
+            let (exp, exp_base) = match exp_format {
+                ExpDec => (num.abs().log10().floor(), cast::<f64, T>(10.0f64).unwrap()),
+                ExpBin => (num.abs().log2().floor(), cast::<f64, T>(2.0f64).unwrap()),
+                ExpNone => fail!("unreachable"),
+            };
+
+            (num / exp_base.powf(exp), cast::<T, i32>(exp).unwrap())
+        }
+    };
+
+    // First emit the non-fractional part, looping at least once to make
+    // sure at least a `0` gets emitted.
+    let mut deccum = num.trunc();
+    loop {
+        // Calculate the absolute value of each digit instead of only
+        // doing it once for the whole number because a
+        // representable negative number doesn't necessary have an
+        // representable additive inverse of the same type
+        // (See twos complement). But we assume that for the
+        // numbers [-35 .. 0] we always have [0 .. 35].
+        let current_digit = (deccum % radix_gen).abs();
+
+        // Decrease the deccumulator one digit at a time
+        deccum = deccum / radix_gen;
+        deccum = deccum.trunc();
+
+        let c = char::from_digit(current_digit.to_int().unwrap() as uint, radix);
+        buf[end] = c.unwrap() as u8;
+        end += 1;
+
+        // No more digits to calculate for the non-fractional part -> break
+        if deccum == _0 { break; }
+    }
+
+    // If limited digits, calculate one digit more for rounding.
+    let (limit_digits, digit_count, exact) = match digits {
+        DigAll          => (false, 0u,      false),
+        DigMax(count)   => (true,  count+1, false),
+        DigExact(count) => (true,  count+1, true)
+    };
+
+    // Decide what sign to put in front
+    match sign {
+        SignNeg | SignAll if neg => {
+            buf[end] = '-' as u8;
+            end += 1;
+        }
+        SignAll => {
+            buf[end] = '+' as u8;
+            end += 1;
+        }
+        _ => ()
+    }
+
+    buf.mut_slice_to(end).reverse();
+
+    // Remember start of the fractional digits.
+    // Points one beyond end of buf if none get generated,
+    // or at the '.' otherwise.
+    let start_fractional_digits = end;
+
+    // Now emit the fractional part, if any
+    deccum = num.fract();
+    if deccum != _0 || (limit_digits && exact && digit_count > 0) {
+        buf[end] = '.' as u8;
+        end += 1;
+        let mut dig = 0u;
+
+        // calculate new digits while
+        // - there is no limit and there are digits left
+        // - or there is a limit, it's not reached yet and
+        //   - it's exact
+        //   - or it's a maximum, and there are still digits left
+        while (!limit_digits && deccum != _0)
+           || (limit_digits && dig < digit_count && (
+                   exact
+                || (!exact && deccum != _0)
+              )
+        ) {
+            // Shift first fractional digit into the integer part
+            deccum = deccum * radix_gen;
+
+            // Calculate the absolute value of each digit.
+            // See note in first loop.
+            let current_digit = deccum.trunc().abs();
+
+            let c = char::from_digit(current_digit.to_int().unwrap() as uint,
+                                     radix);
+            buf[end] = c.unwrap() as u8;
+            end += 1;
+
+            // Decrease the deccumulator one fractional digit at a time
+            deccum = deccum.fract();
+            dig += 1u;
+        }
+
+        // If digits are limited, and that limit has been reached,
+        // cut off the one extra digit, and depending on its value
+        // round the remaining ones.
+        if limit_digits && dig == digit_count {
+            let ascii2value = |chr: u8| {
+                char::to_digit(chr as char, radix).unwrap()
+            };
+            let value2ascii = |val: uint| {
+                char::from_digit(val, radix).unwrap() as u8
+            };
+
+            let extra_digit = ascii2value(buf[end - 1]);
+            end -= 1;
+            if extra_digit >= radix / 2 { // -> need to round
+                let mut i: int = end as int - 1;
+                loop {
+                    // If reached left end of number, have to
+                    // insert additional digit:
+                    if i < 0
+                    || buf[i as uint] == '-' as u8
+                    || buf[i as uint] == '+' as u8 {
+                        for j in range(i as uint + 1, end).rev() {
+                            buf[j + 1] = buf[j];
+                        }
+                        buf[(i + 1) as uint] = value2ascii(1);
+                        end += 1;
+                        break;
+                    }
+
+                    // Skip the '.'
+                    if buf[i as uint] == '.' as u8 { i -= 1; continue; }
+
+                    // Either increment the digit,
+                    // or set to 0 if max and carry the 1.
+                    let current_digit = ascii2value(buf[i as uint]);
+                    if current_digit < (radix - 1) {
+                        buf[i as uint] = value2ascii(current_digit+1);
+                        break;
+                    } else {
+                        buf[i as uint] = value2ascii(0);
+                        i -= 1;
+                    }
+                }
+            }
+        }
+    }
+
+    // if number of digits is not exact, remove all trailing '0's up to
+    // and including the '.'
+    if !exact {
+        let buf_max_i = end - 1;
+
+        // index to truncate from
+        let mut i = buf_max_i;
+
+        // discover trailing zeros of fractional part
+        while i > start_fractional_digits && buf[i] == '0' as u8 {
+            i -= 1;
+        }
+
+        // Only attempt to truncate digits if buf has fractional digits
+        if i >= start_fractional_digits {
+            // If buf ends with '.', cut that too.
+            if buf[i] == '.' as u8 { i -= 1 }
+
+            // only resize buf if we actually remove digits
+            if i < buf_max_i {
+                end = i + 1;
+            }
+        }
+    } // If exact and trailing '.', just cut that
+    else {
+        let max_i = end - 1;
+        if buf[max_i] == '.' as u8 {
+            end = max_i;
+        }
+    }
+
+    match exp_format {
+        ExpNone => {},
+        _ => {
+            buf[end] = match exp_format {
+                ExpDec if exp_upper => 'E',
+                ExpDec if !exp_upper => 'e',
+                ExpBin if exp_upper => 'P',
+                ExpBin if !exp_upper => 'p',
+                _ => fail!("unreachable"),
+            } as u8;
+            end += 1;
+
+            struct Filler<'a> {
+                buf: &'a mut [u8],
+                end: &'a mut uint,
+            }
+
+            impl<'a> fmt::FormatWriter for Filler<'a> {
+                fn write(&mut self, bytes: &[u8]) -> fmt::Result {
+                    slice::bytes::copy_memory(self.buf.mut_slice_from(*self.end),
+                                              bytes);
+                    *self.end += bytes.len();
+                    Ok(())
+                }
+            }
+
+            let mut filler = Filler { buf: buf, end: &mut end };
+            match sign {
+                SignNeg => {
+                    let _ = format_args!(|args| {
+                        fmt::write(&mut filler, args)
+                    }, "{:-}", exp);
+                }
+                SignNone | SignAll => {
+                    let _ = format_args!(|args| {
+                        fmt::write(&mut filler, args)
+                    }, "{}", exp);
+                }
+            }
+        }
+    }
+
+    f(buf.slice_to(end))
+}
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
new file mode 100644 (file)
index 0000000..979928c
--- /dev/null
@@ -0,0 +1,834 @@
+// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Utilities for formatting and printing strings
+
+#![allow(unused_variable)]
+
+use any;
+use cell::Cell;
+use char::Char;
+use container::Container;
+use iter::{Iterator, range};
+use kinds::Copy;
+use mem;
+use option::{Option, Some, None};
+use result::{Ok, Err};
+use result;
+use slice::{Vector, ImmutableVector};
+use slice;
+use str::StrSlice;
+use str;
+
+pub use self::num::radix;
+pub use self::num::Radix;
+pub use self::num::RadixFmt;
+
+macro_rules! write(
+    ($dst:expr, $($arg:tt)*) => ({
+        format_args!(|args| { $dst.write_fmt(args) }, $($arg)*)
+    })
+)
+
+mod num;
+mod float;
+pub mod rt;
+
+pub type Result = result::Result<(), FormatError>;
+
+/// dox
+pub enum FormatError {
+    /// dox
+    WriteError,
+}
+
+/// dox
+pub trait FormatWriter {
+    /// dox
+    fn write(&mut self, bytes: &[u8]) -> Result;
+}
+
+/// A struct to represent both where to emit formatting strings to and how they
+/// should be formatted. A mutable version of this is passed to all formatting
+/// traits.
+pub struct Formatter<'a> {
+    /// Flags for formatting (packed version of rt::Flag)
+    pub flags: uint,
+    /// Character used as 'fill' whenever there is alignment
+    pub fill: char,
+    /// Boolean indication of whether the output should be left-aligned
+    pub align: rt::Alignment,
+    /// Optionally specified integer width that the output should be
+    pub width: Option<uint>,
+    /// Optionally specified precision for numeric types
+    pub precision: Option<uint>,
+
+    #[allow(missing_doc)]
+    #[cfg(stage0)]
+    pub buf: &'a mut FormatWriter,
+    #[cfg(not(stage0))]
+    buf: &'a mut FormatWriter,
+    curarg: slice::Items<'a, Argument<'a>>,
+    args: &'a [Argument<'a>],
+}
+
+enum CurrentlyFormatting<'a> {
+    Nothing,
+    RawString(&'a str),
+    Number(uint),
+}
+
+/// This struct represents the generic "argument" which is taken by the Xprintf
+/// family of functions. It contains a function to format the given value. At
+/// compile time it is ensured that the function and the value have the correct
+/// types, and then this struct is used to canonicalize arguments to one type.
+pub struct Argument<'a> {
+    formatter: extern "Rust" fn(&any::Void, &mut Formatter) -> Result,
+    value: &'a any::Void,
+}
+
+impl<'a> Arguments<'a> {
+    /// When using the format_args!() macro, this function is used to generate the
+    /// Arguments structure. The compiler inserts an `unsafe` block to call this,
+    /// which is valid because the compiler performs all necessary validation to
+    /// ensure that the resulting call to format/write would be safe.
+    #[doc(hidden)] #[inline]
+    pub unsafe fn new<'a>(fmt: &'static [rt::Piece<'static>],
+                          args: &'a [Argument<'a>]) -> Arguments<'a> {
+        Arguments{ fmt: mem::transmute(fmt), args: args }
+    }
+}
+
+/// This structure represents a safely precompiled version of a format string
+/// and its arguments. This cannot be generated at runtime because it cannot
+/// safely be done so, so no constructors are given and the fields are private
+/// to prevent modification.
+///
+/// The `format_args!` macro will safely create an instance of this structure
+/// and pass it to a user-supplied function. The macro validates the format
+/// string at compile-time so usage of the `write` and `format` functions can
+/// be safely performed.
+pub struct Arguments<'a> {
+    fmt: &'a [rt::Piece<'a>],
+    args: &'a [Argument<'a>],
+}
+
+impl<'a> Show for Arguments<'a> {
+    fn fmt(&self, fmt: &mut Formatter) -> Result {
+        write(fmt.buf, self)
+    }
+}
+
+/// When a format is not otherwise specified, types are formatted by ascribing
+/// to this trait. There is not an explicit way of selecting this trait to be
+/// used for formatting, it is only if no other format is specified.
+pub trait Show {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `b` character
+pub trait Bool {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `c` character
+pub trait Char {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `i` and `d` characters
+pub trait Signed {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `u` character
+pub trait Unsigned {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `o` character
+pub trait Octal {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `t` character
+pub trait Binary {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `x` character
+pub trait LowerHex {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `X` character
+pub trait UpperHex {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `s` character
+pub trait String {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `p` character
+pub trait Pointer {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `f` character
+pub trait Float {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `e` character
+pub trait LowerExp {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `E` character
+pub trait UpperExp {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+// FIXME #11938 - UFCS would make us able call the above methods
+// directly Show::show(x, fmt).
+macro_rules! uniform_fn_call_workaround {
+    ($( $name: ident, $trait_: ident; )*) => {
+        $(
+            #[doc(hidden)]
+            pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) -> Result {
+                x.fmt(fmt)
+            }
+            )*
+    }
+}
+uniform_fn_call_workaround! {
+    secret_show, Show;
+    secret_bool, Bool;
+    secret_char, Char;
+    secret_signed, Signed;
+    secret_unsigned, Unsigned;
+    secret_octal, Octal;
+    secret_binary, Binary;
+    secret_lower_hex, LowerHex;
+    secret_upper_hex, UpperHex;
+    secret_string, String;
+    secret_pointer, Pointer;
+    secret_float, Float;
+    secret_lower_exp, LowerExp;
+    secret_upper_exp, UpperExp;
+}
+
+/// The `write` function takes an output stream, a precompiled format string,
+/// and a list of arguments. The arguments will be formatted according to the
+/// specified format string into the output stream provided.
+///
+/// # Arguments
+///
+///   * output - the buffer to write output to
+///   * args - the precompiled arguments generated by `format_args!`
+pub fn write(output: &mut FormatWriter, args: &Arguments) -> Result {
+    let mut formatter = Formatter {
+        flags: 0,
+        width: None,
+        precision: None,
+        buf: output,
+        align: rt::AlignUnknown,
+        fill: ' ',
+        args: args.args,
+        curarg: args.args.iter(),
+    };
+    for piece in args.fmt.iter() {
+        try!(formatter.run(piece, Nothing));
+    }
+    Ok(())
+}
+
+impl<'a> Formatter<'a> {
+
+    // First up is the collection of functions used to execute a format string
+    // at runtime. This consumes all of the compile-time statics generated by
+    // the format! syntax extension.
+
+    fn run(&mut self, piece: &rt::Piece, cur: CurrentlyFormatting) -> Result {
+        match *piece {
+            rt::String(s) => self.buf.write(s.as_bytes()),
+            rt::CurrentArgument(()) => {
+                match cur {
+                    Nothing => Ok(()),
+                    Number(n) => secret_show(&radix(n, 10), self),
+                    RawString(s) => self.buf.write(s.as_bytes()),
+                }
+            }
+            rt::Argument(ref arg) => {
+                // Fill in the format parameters into the formatter
+                self.fill = arg.format.fill;
+                self.align = arg.format.align;
+                self.flags = arg.format.flags;
+                self.width = self.getcount(&arg.format.width);
+                self.precision = self.getcount(&arg.format.precision);
+
+                // Extract the correct argument
+                let value = match arg.position {
+                    rt::ArgumentNext => { *self.curarg.next().unwrap() }
+                    rt::ArgumentIs(i) => self.args[i],
+                };
+
+                // Then actually do some printing
+                match arg.method {
+                    None => (value.formatter)(value.value, self),
+                    Some(ref method) => self.execute(*method, value)
+                }
+            }
+        }
+    }
+
+    fn getcount(&mut self, cnt: &rt::Count) -> Option<uint> {
+        match *cnt {
+            rt::CountIs(n) => { Some(n) }
+            rt::CountImplied => { None }
+            rt::CountIsParam(i) => {
+                let v = self.args[i].value;
+                unsafe { Some(*(v as *any::Void as *uint)) }
+            }
+            rt::CountIsNextParam => {
+                let v = self.curarg.next().unwrap().value;
+                unsafe { Some(*(v as *any::Void as *uint)) }
+            }
+        }
+    }
+
+    fn execute(&mut self, method: &rt::Method, arg: Argument) -> Result {
+        match *method {
+            // Pluralization is selection upon a numeric value specified as the
+            // parameter.
+            rt::Plural(offset, ref selectors, ref default) => {
+                // This is validated at compile-time to be a pointer to a
+                // '&uint' value.
+                let value: &uint = unsafe { mem::transmute(arg.value) };
+                let value = *value;
+
+                // First, attempt to match against explicit values without the
+                // offsetted value
+                for s in selectors.iter() {
+                    match s.selector {
+                        rt::Literal(val) if value == val => {
+                            return self.runplural(value, s.result);
+                        }
+                        _ => {}
+                    }
+                }
+
+                // Next, offset the value and attempt to match against the
+                // keyword selectors.
+                let value = value - match offset { Some(i) => i, None => 0 };
+                for s in selectors.iter() {
+                    let run = match s.selector {
+                        rt::Keyword(rt::Zero) => value == 0,
+                        rt::Keyword(rt::One) => value == 1,
+                        rt::Keyword(rt::Two) => value == 2,
+
+                        // FIXME: Few/Many should have a user-specified boundary
+                        //      One possible option would be in the function
+                        //      pointer of the 'arg: Argument' struct.
+                        rt::Keyword(rt::Few) => value < 8,
+                        rt::Keyword(rt::Many) => value >= 8,
+
+                        rt::Literal(..) => false
+                    };
+                    if run {
+                        return self.runplural(value, s.result);
+                    }
+                }
+
+                self.runplural(value, *default)
+            }
+
+            // Select is just a matching against the string specified.
+            rt::Select(ref selectors, ref default) => {
+                // This is validated at compile-time to be a pointer to a
+                // string slice,
+                let value: & &str = unsafe { mem::transmute(arg.value) };
+                let value = *value;
+
+                for s in selectors.iter() {
+                    if s.selector == value {
+                        for piece in s.result.iter() {
+                            try!(self.run(piece, RawString(value)));
+                        }
+                        return Ok(());
+                    }
+                }
+                for piece in default.iter() {
+                    try!(self.run(piece, RawString(value)));
+                }
+                Ok(())
+            }
+        }
+    }
+
+    fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) -> Result {
+        for piece in pieces.iter() {
+            try!(self.run(piece, Number(value)));
+        }
+        Ok(())
+    }
+
+    // Helper methods used for padding and processing formatting arguments that
+    // all formatting traits can use.
+
+    /// Performs the correct padding for an integer which has already been
+    /// emitted into a byte-array. The byte-array should *not* contain the sign
+    /// for the integer, that will be added by this method.
+    ///
+    /// # Arguments
+    ///
+    /// * is_positive - whether the original integer was positive or not.
+    /// * prefix - if the '#' character (FlagAlternate) is provided, this
+    ///   is the prefix to put in front of the number.
+    /// * buf - the byte array that the number has been formatted into
+    ///
+    /// This function will correctly account for the flags provided as well as
+    /// the minimum width. It will not take precision into account.
+    pub fn pad_integral(&mut self, is_positive: bool, prefix: &str,
+                        buf: &[u8]) -> Result {
+        use fmt::rt::{FlagAlternate, FlagSignPlus, FlagSignAwareZeroPad};
+
+        let mut width = buf.len();
+
+        let mut sign = None;
+        if !is_positive {
+            sign = Some('-'); width += 1;
+        } else if self.flags & (1 << (FlagSignPlus as uint)) != 0 {
+            sign = Some('+'); width += 1;
+        }
+
+        let mut prefixed = false;
+        if self.flags & (1 << (FlagAlternate as uint)) != 0 {
+            prefixed = true; width += prefix.len();
+        }
+
+        // Writes the sign if it exists, and then the prefix if it was requested
+        let write_prefix = |f: &mut Formatter| {
+            for c in sign.move_iter() {
+                let mut b = [0, ..4];
+                let n = c.encode_utf8(b);
+                try!(f.buf.write(b.slice_to(n)));
+            }
+            if prefixed { f.buf.write(prefix.as_bytes()) }
+            else { Ok(()) }
+        };
+
+        // The `width` field is more of a `min-width` parameter at this point.
+        match self.width {
+            // If there's no minimum length requirements then we can just
+            // write the bytes.
+            None => {
+                try!(write_prefix(self)); self.buf.write(buf)
+            }
+            // Check if we're over the minimum width, if so then we can also
+            // just write the bytes.
+            Some(min) if width >= min => {
+                try!(write_prefix(self)); self.buf.write(buf)
+            }
+            // The sign and prefix goes before the padding if the fill character
+            // is zero
+            Some(min) if self.flags & (1 << (FlagSignAwareZeroPad as uint)) != 0 => {
+                self.fill = '0';
+                try!(write_prefix(self));
+                self.with_padding(min - width, rt::AlignRight, |f| f.buf.write(buf))
+            }
+            // Otherwise, the sign and prefix goes after the padding
+            Some(min) => {
+                self.with_padding(min - width, rt::AlignRight, |f| {
+                    try!(write_prefix(f)); f.buf.write(buf)
+                })
+            }
+        }
+    }
+
+    /// This function takes a string slice and emits it to the internal buffer
+    /// after applying the relevant formatting flags specified. The flags
+    /// recognized for generic strings are:
+    ///
+    /// * width - the minimum width of what to emit
+    /// * fill/align - what to emit and where to emit it if the string
+    ///                provided needs to be padded
+    /// * precision - the maximum length to emit, the string is truncated if it
+    ///               is longer than this length
+    ///
+    /// Notably this function ignored the `flag` parameters
+    pub fn pad(&mut self, s: &str) -> Result {
+        // Make sure there's a fast path up front
+        if self.width.is_none() && self.precision.is_none() {
+            return self.buf.write(s.as_bytes());
+        }
+        // The `precision` field can be interpreted as a `max-width` for the
+        // string being formatted
+        match self.precision {
+            Some(max) => {
+                // If there's a maximum width and our string is longer than
+                // that, then we must always have truncation. This is the only
+                // case where the maximum length will matter.
+                let char_len = s.char_len();
+                if char_len >= max {
+                    let nchars = ::cmp::min(max, char_len);
+                    return self.buf.write(s.slice_chars(0, nchars).as_bytes());
+                }
+            }
+            None => {}
+        }
+        // The `width` field is more of a `min-width` parameter at this point.
+        match self.width {
+            // If we're under the maximum length, and there's no minimum length
+            // requirements, then we can just emit the string
+            None => self.buf.write(s.as_bytes()),
+            // If we're under the maximum width, check if we're over the minimum
+            // width, if so it's as easy as just emitting the string.
+            Some(width) if s.char_len() >= width => {
+                self.buf.write(s.as_bytes())
+            }
+            // If we're under both the maximum and the minimum width, then fill
+            // up the minimum width with the specified string + some alignment.
+            Some(width) => {
+                self.with_padding(width - s.len(), rt::AlignLeft, |me| {
+                    me.buf.write(s.as_bytes())
+                })
+            }
+        }
+    }
+
+    /// Runs a callback, emitting the correct padding either before or
+    /// afterwards depending on whether right or left alingment is requested.
+    fn with_padding(&mut self,
+                    padding: uint,
+                    default: rt::Alignment,
+                    f: |&mut Formatter| -> Result) -> Result {
+        let align = match self.align {
+            rt::AlignUnknown => default,
+            rt::AlignLeft | rt::AlignRight => self.align
+        };
+        if align == rt::AlignLeft {
+            try!(f(self));
+        }
+        let mut fill = [0u8, ..4];
+        let len = self.fill.encode_utf8(fill);
+        for _ in range(0, padding) {
+            try!(self.buf.write(fill.slice_to(len)));
+        }
+        if align == rt::AlignRight {
+            try!(f(self));
+        }
+        Ok(())
+    }
+
+    /// Writes some data to the underlying buffer contained within this
+    /// formatter.
+    pub fn write(&mut self, data: &[u8]) -> Result {
+        self.buf.write(data)
+    }
+
+    /// Writes some formatted information into this instance
+    pub fn write_fmt(&mut self, fmt: &Arguments) -> Result {
+        write(self.buf, fmt)
+    }
+}
+
+/// This is a function which calls are emitted to by the compiler itself to
+/// create the Argument structures that are passed into the `format` function.
+#[doc(hidden)] #[inline]
+pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
+                       t: &'a T) -> Argument<'a> {
+    unsafe {
+        Argument {
+            formatter: mem::transmute(f),
+            value: mem::transmute(t)
+        }
+    }
+}
+
+#[cfg(test)]
+pub fn format(args: &Arguments) -> ~str {
+    use str;
+    use realstd::str::StrAllocating;
+    use realstd::io::MemWriter;
+
+    fn mywrite<T: ::realstd::io::Writer>(t: &mut T, b: &[u8]) {
+        use realstd::io::Writer;
+        let _ = t.write(b);
+    }
+
+    impl FormatWriter for MemWriter {
+        fn write(&mut self, bytes: &[u8]) -> Result {
+            mywrite(self, bytes);
+            Ok(())
+        }
+    }
+
+    let mut i = MemWriter::new();
+    let _ = write(&mut i, args);
+    str::from_utf8(i.get_ref()).unwrap().to_owned()
+}
+
+/// When the compiler determines that the type of an argument *must* be a string
+/// (such as for select), then it invokes this method.
+#[doc(hidden)] #[inline]
+pub fn argumentstr<'a>(s: &'a &str) -> Argument<'a> {
+    argument(secret_string, s)
+}
+
+/// When the compiler determines that the type of an argument *must* be a uint
+/// (such as for plural), then it invokes this method.
+#[doc(hidden)] #[inline]
+pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
+    argument(secret_unsigned, s)
+}
+
+// Implementations of the core formatting traits
+
+impl<T: Show> Show for @T {
+    fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
+}
+impl<'a, T: Show> Show for &'a T {
+    fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
+}
+impl<'a, T: Show> Show for &'a mut T {
+    fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
+}
+
+impl Bool for bool {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        secret_string(&(if *self {"true"} else {"false"}), f)
+    }
+}
+
+impl<'a, T: str::Str> String for T {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        f.pad(self.as_slice())
+    }
+}
+
+impl Char for char {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        let mut utf8 = [0u8, ..4];
+        let amt = self.encode_utf8(utf8);
+        let s: &str = unsafe { mem::transmute(utf8.slice_to(amt)) };
+        secret_string(&s, f)
+    }
+}
+
+impl<T> Pointer for *T {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        f.flags |= 1 << (rt::FlagAlternate as uint);
+        secret_lower_hex::<uint>(&(*self as uint), f)
+    }
+}
+impl<T> Pointer for *mut T {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        secret_pointer::<*T>(&(*self as *T), f)
+    }
+}
+impl<'a, T> Pointer for &'a T {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        secret_pointer::<*T>(&(&**self as *T), f)
+    }
+}
+impl<'a, T> Pointer for &'a mut T {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        secret_pointer::<*T>(&(&**self as *T), f)
+    }
+}
+
+macro_rules! floating(($ty:ident) => {
+    impl Float for $ty {
+        fn fmt(&self, fmt: &mut Formatter) -> Result {
+            use num::Signed;
+
+            let digits = match fmt.precision {
+                Some(i) => float::DigExact(i),
+                None => float::DigMax(6),
+            };
+            float::float_to_str_bytes_common(self.abs(),
+                                             10,
+                                             true,
+                                             float::SignNeg,
+                                             digits,
+                                             float::ExpNone,
+                                             false,
+                                             |bytes| {
+                fmt.pad_integral(*self >= 0.0, "", bytes)
+            })
+        }
+    }
+
+    impl LowerExp for $ty {
+        fn fmt(&self, fmt: &mut Formatter) -> Result {
+            use num::Signed;
+
+            let digits = match fmt.precision {
+                Some(i) => float::DigExact(i),
+                None => float::DigMax(6),
+            };
+            float::float_to_str_bytes_common(self.abs(),
+                                             10,
+                                             true,
+                                             float::SignNeg,
+                                             digits,
+                                             float::ExpDec,
+                                             false,
+                                             |bytes| {
+                fmt.pad_integral(*self >= 0.0, "", bytes)
+            })
+        }
+    }
+
+    impl UpperExp for $ty {
+        fn fmt(&self, fmt: &mut Formatter) -> Result {
+            use num::Signed;
+
+            let digits = match fmt.precision {
+                Some(i) => float::DigExact(i),
+                None => float::DigMax(6),
+            };
+            float::float_to_str_bytes_common(self.abs(),
+                                             10,
+                                             true,
+                                             float::SignNeg,
+                                             digits,
+                                             float::ExpDec,
+                                             true,
+                                             |bytes| {
+                fmt.pad_integral(*self >= 0.0, "", bytes)
+            })
+        }
+    }
+})
+floating!(f32)
+floating!(f64)
+
+// Implementation of Show for various core types
+
+macro_rules! delegate(($ty:ty to $other:ident) => {
+    impl<'a> Show for $ty {
+        fn fmt(&self, f: &mut Formatter) -> Result {
+            (concat_idents!(secret_, $other)(self, f))
+        }
+    }
+})
+delegate!(~str to string)
+delegate!(&'a str to string)
+delegate!(bool to bool)
+delegate!(char to char)
+delegate!(f32 to float)
+delegate!(f64 to float)
+
+impl<T> Show for *T {
+    fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
+}
+impl<T> Show for *mut T {
+    fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
+}
+
+macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
+
+macro_rules! tuple (
+    () => ();
+    ( $($name:ident,)+ ) => (
+        impl<$($name:Show),*> Show for ($($name,)*) {
+            #[allow(uppercase_variables, dead_assignment)]
+            fn fmt(&self, f: &mut Formatter) -> Result {
+                try!(write!(f, "("));
+                let ($(ref $name,)*) = *self;
+                let mut n = 0;
+                $(
+                    if n > 0 {
+                        try!(write!(f, ", "));
+                    }
+                    try!(write!(f, "{}", *$name));
+                    n += 1;
+                )*
+                if n == 1 {
+                    try!(write!(f, ","));
+                }
+                write!(f, ")")
+            }
+        }
+        peel!($($name,)*)
+    )
+)
+
+tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
+
+impl<'a> Show for &'a any::Any {
+    fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") }
+}
+
+impl<'a, T: Show> Show for &'a [T] {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
+            try!(write!(f, "["));
+        }
+        let mut is_first = true;
+        for x in self.iter() {
+            if is_first {
+                is_first = false;
+            } else {
+                try!(write!(f, ", "));
+            }
+            try!(write!(f, "{}", *x))
+        }
+        if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
+            try!(write!(f, "]"));
+        }
+        Ok(())
+    }
+}
+
+impl<'a, T: Show> Show for &'a mut [T] {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        secret_show(&self.as_slice(), f)
+    }
+}
+
+impl<T: Show> Show for ~[T] {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        secret_show(&self.as_slice(), f)
+    }
+}
+
+impl Show for () {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        f.pad("()")
+    }
+}
+
+impl<T: Copy + Show> Show for Cell<T> {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        write!(f, r"Cell \{ value: {} \}", self.get())
+    }
+}
+
+// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
+// it's a lot easier than creating all of the rt::Piece structures here.
diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs
new file mode 100644 (file)
index 0000000..d9a3271
--- /dev/null
@@ -0,0 +1,470 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Integer and floating-point number formatting
+
+// FIXME: #6220 Implement floating point formatting
+
+#![allow(unsigned_negate)]
+
+use container::Container;
+use fmt;
+use iter::{Iterator, DoubleEndedIterator};
+use num::{Int, cast, zero};
+use option::{Some, None};
+use slice::{ImmutableVector, MutableVector};
+
+/// A type that represents a specific radix
+trait GenericRadix {
+    /// The number of digits.
+    fn base(&self) -> u8;
+
+    /// A radix-specific prefix string.
+    fn prefix(&self) -> &'static str { "" }
+
+    /// Converts an integer to corresponding radix digit.
+    fn digit(&self, x: u8) -> u8;
+
+    /// Format an integer using the radix using a formatter.
+    fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
+        // The radix can be as low as 2, so we need a buffer of at least 64
+        // characters for a base 2 number.
+        let mut buf = [0u8, ..64];
+        let base = cast(self.base()).unwrap();
+        let mut curr = buf.len();
+        let is_positive = x >= zero();
+        if is_positive {
+            // Accumulate each digit of the number from the least significant
+            // to the most significant figure.
+            for byte in buf.mut_iter().rev() {
+                let n = x % base;                         // Get the current place value.
+                x = x / base;                             // Deaccumulate the number.
+                *byte = self.digit(cast(n).unwrap());     // Store the digit in the buffer.
+                curr -= 1;
+                if x == zero() { break; }                 // No more digits left to accumulate.
+            }
+        } else {
+            // Do the same as above, but accounting for two's complement.
+            for byte in buf.mut_iter().rev() {
+                let n = -(x % base);                      // Get the current place value.
+                x = x / base;                             // Deaccumulate the number.
+                *byte = self.digit(cast(n).unwrap());     // Store the digit in the buffer.
+                curr -= 1;
+                if x == zero() { break; }                 // No more digits left to accumulate.
+            }
+        }
+        f.pad_integral(is_positive, self.prefix(), buf.slice_from(curr))
+    }
+}
+
+/// A binary (base 2) radix
+#[deriving(Clone, Eq)]
+struct Binary;
+
+/// An octal (base 8) radix
+#[deriving(Clone, Eq)]
+struct Octal;
+
+/// A decimal (base 10) radix
+#[deriving(Clone, Eq)]
+struct Decimal;
+
+/// A hexadecimal (base 16) radix, formatted with lower-case characters
+#[deriving(Clone, Eq)]
+struct LowerHex;
+
+/// A hexadecimal (base 16) radix, formatted with upper-case characters
+#[deriving(Clone, Eq)]
+pub struct UpperHex;
+
+macro_rules! radix {
+    ($T:ident, $base:expr, $prefix:expr, $($x:pat => $conv:expr),+) => {
+        impl GenericRadix for $T {
+            fn base(&self) -> u8 { $base }
+            fn prefix(&self) -> &'static str { $prefix }
+            fn digit(&self, x: u8) -> u8 {
+                match x {
+                    $($x => $conv,)+
+                    x => fail!("number not in the range 0..{}: {}", self.base() - 1, x),
+                }
+            }
+        }
+    }
+}
+
+radix!(Binary,    2, "0b", x @  0 .. 2 => '0' as u8 + x)
+radix!(Octal,     8, "0o", x @  0 .. 7 => '0' as u8 + x)
+radix!(Decimal,  10, "",   x @  0 .. 9 => '0' as u8 + x)
+radix!(LowerHex, 16, "0x", x @  0 .. 9 => '0' as u8 + x,
+                           x @ 10 ..15 => 'a' as u8 + (x - 10))
+radix!(UpperHex, 16, "0x", x @  0 .. 9 => '0' as u8 + x,
+                           x @ 10 ..15 => 'A' as u8 + (x - 10))
+
+/// A radix with in the range of `2..36`.
+#[deriving(Clone, Eq)]
+pub struct Radix {
+    base: u8,
+}
+
+impl Radix {
+    fn new(base: u8) -> Radix {
+        assert!(2 <= base && base <= 36, "the base must be in the range of 0..36: {}", base);
+        Radix { base: base }
+    }
+}
+
+impl GenericRadix for Radix {
+    fn base(&self) -> u8 { self.base }
+    fn digit(&self, x: u8) -> u8 {
+        match x {
+            x @  0 ..9 => '0' as u8 + x,
+            x if x < self.base() => 'a' as u8 + (x - 10),
+            x => fail!("number not in the range 0..{}: {}", self.base() - 1, x),
+        }
+    }
+}
+
+/// A helper type for formatting radixes.
+pub struct RadixFmt<T, R>(T, R);
+
+/// Constructs a radix formatter in the range of `2..36`.
+///
+/// # Example
+///
+/// ~~~
+/// use std::fmt::radix;
+/// assert_eq!(format!("{}", radix(55, 36)), "1j".to_owned());
+/// ~~~
+pub fn radix<T>(x: T, base: u8) -> RadixFmt<T, Radix> {
+    RadixFmt(x, Radix::new(base))
+}
+
+macro_rules! radix_fmt {
+    ($T:ty as $U:ty, $fmt:ident) => {
+        impl fmt::Show for RadixFmt<$T, Radix> {
+            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+                match *self { RadixFmt(ref x, radix) => radix.$fmt(*x as $U, f) }
+            }
+        }
+    }
+}
+macro_rules! int_base {
+    ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {
+        impl fmt::$Trait for $T {
+            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+                $Radix.fmt_int(*self as $U, f)
+            }
+        }
+    }
+}
+macro_rules! integer {
+    ($Int:ident, $Uint:ident) => {
+        int_base!(Show     for $Int as $Int   -> Decimal)
+        int_base!(Signed   for $Int as $Int   -> Decimal)
+        int_base!(Binary   for $Int as $Uint  -> Binary)
+        int_base!(Octal    for $Int as $Uint  -> Octal)
+        int_base!(LowerHex for $Int as $Uint  -> LowerHex)
+        int_base!(UpperHex for $Int as $Uint  -> UpperHex)
+        radix_fmt!($Int as $Int, fmt_int)
+
+        int_base!(Show     for $Uint as $Uint -> Decimal)
+        int_base!(Unsigned for $Uint as $Uint -> Decimal)
+        int_base!(Binary   for $Uint as $Uint -> Binary)
+        int_base!(Octal    for $Uint as $Uint -> Octal)
+        int_base!(LowerHex for $Uint as $Uint -> LowerHex)
+        int_base!(UpperHex for $Uint as $Uint -> UpperHex)
+        radix_fmt!($Uint as $Uint, fmt_int)
+    }
+}
+integer!(int, uint)
+integer!(i8, u8)
+integer!(i16, u16)
+integer!(i32, u32)
+integer!(i64, u64)
+
+#[cfg(test)]
+mod tests {
+    use fmt::radix;
+    use super::{Binary, Octal, Decimal, LowerHex, UpperHex};
+    use super::{GenericRadix, Radix};
+    use realstd::str::StrAllocating;
+
+    #[test]
+    fn test_radix_base() {
+        assert_eq!(Binary.base(), 2);
+        assert_eq!(Octal.base(), 8);
+        assert_eq!(Decimal.base(), 10);
+        assert_eq!(LowerHex.base(), 16);
+        assert_eq!(UpperHex.base(), 16);
+        assert_eq!(Radix { base: 36 }.base(), 36);
+    }
+
+    #[test]
+    fn test_radix_prefix() {
+        assert_eq!(Binary.prefix(), "0b");
+        assert_eq!(Octal.prefix(), "0o");
+        assert_eq!(Decimal.prefix(), "");
+        assert_eq!(LowerHex.prefix(), "0x");
+        assert_eq!(UpperHex.prefix(), "0x");
+        assert_eq!(Radix { base: 36 }.prefix(), "");
+    }
+
+    #[test]
+    fn test_radix_digit() {
+        assert_eq!(Binary.digit(0), '0' as u8);
+        assert_eq!(Binary.digit(2), '2' as u8);
+        assert_eq!(Octal.digit(0), '0' as u8);
+        assert_eq!(Octal.digit(7), '7' as u8);
+        assert_eq!(Decimal.digit(0), '0' as u8);
+        assert_eq!(Decimal.digit(9), '9' as u8);
+        assert_eq!(LowerHex.digit(0), '0' as u8);
+        assert_eq!(LowerHex.digit(10), 'a' as u8);
+        assert_eq!(LowerHex.digit(15), 'f' as u8);
+        assert_eq!(UpperHex.digit(0), '0' as u8);
+        assert_eq!(UpperHex.digit(10), 'A' as u8);
+        assert_eq!(UpperHex.digit(15), 'F' as u8);
+        assert_eq!(Radix { base: 36 }.digit(0), '0' as u8);
+        assert_eq!(Radix { base: 36 }.digit(15), 'f' as u8);
+        assert_eq!(Radix { base: 36 }.digit(35), 'z' as u8);
+    }
+
+    #[test]
+    #[should_fail]
+    fn test_hex_radix_digit_overflow() {
+        let _ = LowerHex.digit(16);
+    }
+
+    #[test]
+    fn test_format_int() {
+        // Formatting integers should select the right implementation based off
+        // the type of the argument. Also, hex/octal/binary should be defined
+        // for integers, but they shouldn't emit the negative sign.
+        assert_eq!(format!("{}", 1i), "1".to_owned());
+        assert_eq!(format!("{}", 1i8), "1".to_owned());
+        assert_eq!(format!("{}", 1i16), "1".to_owned());
+        assert_eq!(format!("{}", 1i32), "1".to_owned());
+        assert_eq!(format!("{}", 1i64), "1".to_owned());
+        assert_eq!(format!("{:d}", -1i), "-1".to_owned());
+        assert_eq!(format!("{:d}", -1i8), "-1".to_owned());
+        assert_eq!(format!("{:d}", -1i16), "-1".to_owned());
+        assert_eq!(format!("{:d}", -1i32), "-1".to_owned());
+        assert_eq!(format!("{:d}", -1i64), "-1".to_owned());
+        assert_eq!(format!("{:t}", 1i), "1".to_owned());
+        assert_eq!(format!("{:t}", 1i8), "1".to_owned());
+        assert_eq!(format!("{:t}", 1i16), "1".to_owned());
+        assert_eq!(format!("{:t}", 1i32), "1".to_owned());
+        assert_eq!(format!("{:t}", 1i64), "1".to_owned());
+        assert_eq!(format!("{:x}", 1i), "1".to_owned());
+        assert_eq!(format!("{:x}", 1i8), "1".to_owned());
+        assert_eq!(format!("{:x}", 1i16), "1".to_owned());
+        assert_eq!(format!("{:x}", 1i32), "1".to_owned());
+        assert_eq!(format!("{:x}", 1i64), "1".to_owned());
+        assert_eq!(format!("{:X}", 1i), "1".to_owned());
+        assert_eq!(format!("{:X}", 1i8), "1".to_owned());
+        assert_eq!(format!("{:X}", 1i16), "1".to_owned());
+        assert_eq!(format!("{:X}", 1i32), "1".to_owned());
+        assert_eq!(format!("{:X}", 1i64), "1".to_owned());
+        assert_eq!(format!("{:o}", 1i), "1".to_owned());
+        assert_eq!(format!("{:o}", 1i8), "1".to_owned());
+        assert_eq!(format!("{:o}", 1i16), "1".to_owned());
+        assert_eq!(format!("{:o}", 1i32), "1".to_owned());
+        assert_eq!(format!("{:o}", 1i64), "1".to_owned());
+
+        assert_eq!(format!("{}", 1u), "1".to_owned());
+        assert_eq!(format!("{}", 1u8), "1".to_owned());
+        assert_eq!(format!("{}", 1u16), "1".to_owned());
+        assert_eq!(format!("{}", 1u32), "1".to_owned());
+        assert_eq!(format!("{}", 1u64), "1".to_owned());
+        assert_eq!(format!("{:u}", 1u), "1".to_owned());
+        assert_eq!(format!("{:u}", 1u8), "1".to_owned());
+        assert_eq!(format!("{:u}", 1u16), "1".to_owned());
+        assert_eq!(format!("{:u}", 1u32), "1".to_owned());
+        assert_eq!(format!("{:u}", 1u64), "1".to_owned());
+        assert_eq!(format!("{:t}", 1u), "1".to_owned());
+        assert_eq!(format!("{:t}", 1u8), "1".to_owned());
+        assert_eq!(format!("{:t}", 1u16), "1".to_owned());
+        assert_eq!(format!("{:t}", 1u32), "1".to_owned());
+        assert_eq!(format!("{:t}", 1u64), "1".to_owned());
+        assert_eq!(format!("{:x}", 1u), "1".to_owned());
+        assert_eq!(format!("{:x}", 1u8), "1".to_owned());
+        assert_eq!(format!("{:x}", 1u16), "1".to_owned());
+        assert_eq!(format!("{:x}", 1u32), "1".to_owned());
+        assert_eq!(format!("{:x}", 1u64), "1".to_owned());
+        assert_eq!(format!("{:X}", 1u), "1".to_owned());
+        assert_eq!(format!("{:X}", 1u8), "1".to_owned());
+        assert_eq!(format!("{:X}", 1u16), "1".to_owned());
+        assert_eq!(format!("{:X}", 1u32), "1".to_owned());
+        assert_eq!(format!("{:X}", 1u64), "1".to_owned());
+        assert_eq!(format!("{:o}", 1u), "1".to_owned());
+        assert_eq!(format!("{:o}", 1u8), "1".to_owned());
+        assert_eq!(format!("{:o}", 1u16), "1".to_owned());
+        assert_eq!(format!("{:o}", 1u32), "1".to_owned());
+        assert_eq!(format!("{:o}", 1u64), "1".to_owned());
+
+        // Test a larger number
+        assert_eq!(format!("{:t}", 55), "110111".to_owned());
+        assert_eq!(format!("{:o}", 55), "67".to_owned());
+        assert_eq!(format!("{:d}", 55), "55".to_owned());
+        assert_eq!(format!("{:x}", 55), "37".to_owned());
+        assert_eq!(format!("{:X}", 55), "37".to_owned());
+    }
+
+    #[test]
+    fn test_format_int_zero() {
+        assert_eq!(format!("{}", 0i), "0".to_owned());
+        assert_eq!(format!("{:d}", 0i), "0".to_owned());
+        assert_eq!(format!("{:t}", 0i), "0".to_owned());
+        assert_eq!(format!("{:o}", 0i), "0".to_owned());
+        assert_eq!(format!("{:x}", 0i), "0".to_owned());
+        assert_eq!(format!("{:X}", 0i), "0".to_owned());
+
+        assert_eq!(format!("{}", 0u), "0".to_owned());
+        assert_eq!(format!("{:u}", 0u), "0".to_owned());
+        assert_eq!(format!("{:t}", 0u), "0".to_owned());
+        assert_eq!(format!("{:o}", 0u), "0".to_owned());
+        assert_eq!(format!("{:x}", 0u), "0".to_owned());
+        assert_eq!(format!("{:X}", 0u), "0".to_owned());
+    }
+
+    #[test]
+    fn test_format_int_flags() {
+        assert_eq!(format!("{:3d}", 1), "  1".to_owned());
+        assert_eq!(format!("{:>3d}", 1), "  1".to_owned());
+        assert_eq!(format!("{:>+3d}", 1), " +1".to_owned());
+        assert_eq!(format!("{:<3d}", 1), "1  ".to_owned());
+        assert_eq!(format!("{:#d}", 1), "1".to_owned());
+        assert_eq!(format!("{:#x}", 10), "0xa".to_owned());
+        assert_eq!(format!("{:#X}", 10), "0xA".to_owned());
+        assert_eq!(format!("{:#5x}", 10), "  0xa".to_owned());
+        assert_eq!(format!("{:#o}", 10), "0o12".to_owned());
+        assert_eq!(format!("{:08x}", 10), "0000000a".to_owned());
+        assert_eq!(format!("{:8x}", 10), "       a".to_owned());
+        assert_eq!(format!("{:<8x}", 10), "a       ".to_owned());
+        assert_eq!(format!("{:>8x}", 10), "       a".to_owned());
+        assert_eq!(format!("{:#08x}", 10), "0x00000a".to_owned());
+        assert_eq!(format!("{:08d}", -10), "-0000010".to_owned());
+        assert_eq!(format!("{:x}", -1u8), "ff".to_owned());
+        assert_eq!(format!("{:X}", -1u8), "FF".to_owned());
+        assert_eq!(format!("{:t}", -1u8), "11111111".to_owned());
+        assert_eq!(format!("{:o}", -1u8), "377".to_owned());
+        assert_eq!(format!("{:#x}", -1u8), "0xff".to_owned());
+        assert_eq!(format!("{:#X}", -1u8), "0xFF".to_owned());
+        assert_eq!(format!("{:#t}", -1u8), "0b11111111".to_owned());
+        assert_eq!(format!("{:#o}", -1u8), "0o377".to_owned());
+    }
+
+    #[test]
+    fn test_format_int_sign_padding() {
+        assert_eq!(format!("{:+5d}", 1), "   +1".to_owned());
+        assert_eq!(format!("{:+5d}", -1), "   -1".to_owned());
+        assert_eq!(format!("{:05d}", 1), "00001".to_owned());
+        assert_eq!(format!("{:05d}", -1), "-0001".to_owned());
+        assert_eq!(format!("{:+05d}", 1), "+0001".to_owned());
+        assert_eq!(format!("{:+05d}", -1), "-0001".to_owned());
+    }
+
+    #[test]
+    fn test_format_int_twos_complement() {
+        use {i8, i16, i32, i64};
+        assert_eq!(format!("{}", i8::MIN), "-128".to_owned());
+        assert_eq!(format!("{}", i16::MIN), "-32768".to_owned());
+        assert_eq!(format!("{}", i32::MIN), "-2147483648".to_owned());
+        assert_eq!(format!("{}", i64::MIN), "-9223372036854775808".to_owned());
+    }
+
+    #[test]
+    fn test_format_radix() {
+        assert_eq!(format!("{:04}", radix(3, 2)), "0011".to_owned());
+        assert_eq!(format!("{}", radix(55, 36)), "1j".to_owned());
+    }
+
+    #[test]
+    #[should_fail]
+    fn test_radix_base_too_large() {
+        let _ = radix(55, 37);
+    }
+}
+
+#[cfg(test)]
+mod bench {
+    extern crate test;
+
+    mod uint {
+        use super::test::Bencher;
+        use fmt::radix;
+        use rand::{XorShiftRng, Rng};
+
+        #[bench]
+        fn format_bin(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:t}", rng.gen::<uint>()); })
+        }
+
+        #[bench]
+        fn format_oct(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:o}", rng.gen::<uint>()); })
+        }
+
+        #[bench]
+        fn format_dec(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:u}", rng.gen::<uint>()); })
+        }
+
+        #[bench]
+        fn format_hex(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:x}", rng.gen::<uint>()); })
+        }
+
+        #[bench]
+        fn format_base_36(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{}", radix(rng.gen::<uint>(), 36)); })
+        }
+    }
+
+    mod int {
+        use super::test::Bencher;
+        use fmt::radix;
+        use rand::{XorShiftRng, Rng};
+
+        #[bench]
+        fn format_bin(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:t}", rng.gen::<int>()); })
+        }
+
+        #[bench]
+        fn format_oct(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:o}", rng.gen::<int>()); })
+        }
+
+        #[bench]
+        fn format_dec(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:d}", rng.gen::<int>()); })
+        }
+
+        #[bench]
+        fn format_hex(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{:x}", rng.gen::<int>()); })
+        }
+
+        #[bench]
+        fn format_base_36(b: &mut Bencher) {
+            let mut rng = XorShiftRng::new().unwrap();
+            b.iter(|| { format!("{}", radix(rng.gen::<int>(), 36)); })
+        }
+    }
+}
diff --git a/src/libcore/fmt/rt.rs b/src/libcore/fmt/rt.rs
new file mode 100644 (file)
index 0000000..00c8661
--- /dev/null
@@ -0,0 +1,91 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! This is an internal module used by the ifmt! runtime. These structures are
+//! emitted to static arrays to precompile format strings ahead of time.
+//!
+//! These definitions are similar to their `ct` equivalents, but differ in that
+//! these can be statically allocated and are slightly optimized for the runtime
+
+#![allow(missing_doc)]
+#![doc(hidden)]
+
+use option::Option;
+
+pub enum Piece<'a> {
+    String(&'a str),
+    // FIXME(#8259): this shouldn't require the unit-value here
+    CurrentArgument(()),
+    Argument(Argument<'a>),
+}
+
+pub struct Argument<'a> {
+    pub position: Position,
+    pub format: FormatSpec,
+    pub method: Option<&'a Method<'a>>
+}
+
+pub struct FormatSpec {
+    pub fill: char,
+    pub align: Alignment,
+    pub flags: uint,
+    pub precision: Count,
+    pub width: Count,
+}
+
+#[deriving(Eq)]
+pub enum Alignment {
+    AlignLeft,
+    AlignRight,
+    AlignUnknown,
+}
+
+pub enum Count {
+    CountIs(uint), CountIsParam(uint), CountIsNextParam, CountImplied,
+}
+
+pub enum Position {
+    ArgumentNext, ArgumentIs(uint)
+}
+
+pub enum Flag {
+    FlagSignPlus,
+    FlagSignMinus,
+    FlagAlternate,
+    FlagSignAwareZeroPad,
+}
+
+pub enum Method<'a> {
+    Plural(Option<uint>, &'a [PluralArm<'a>], &'a [Piece<'a>]),
+    Select(&'a [SelectArm<'a>], &'a [Piece<'a>]),
+}
+
+pub enum PluralSelector {
+    Keyword(PluralKeyword),
+    Literal(uint),
+}
+
+pub enum PluralKeyword {
+    Zero,
+    One,
+    Two,
+    Few,
+    Many,
+}
+
+pub struct PluralArm<'a> {
+    pub selector: PluralSelector,
+    pub result: &'a [Piece<'a>],
+}
+
+pub struct SelectArm<'a> {
+    pub selector: &'a str,
+    pub result: &'a [Piece<'a>],
+}
index 2828c9bdc231dfdca4347ab1531008215926925d..a5003a1458e119a7bf14776bcd52f13e1a16dc2f 100644 (file)
@@ -471,7 +471,7 @@ fn visit_leave_fn(&mut self, purity: uint, proto: uint,
 /// `TypeId` represents a globally unique identifier for a type
 #[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and
                   // middle/lang_items.rs
-#[deriving(Eq, TotalEq)]
+#[deriving(Eq, TotalEq, Show)]
 #[cfg(not(test))]
 pub struct TypeId {
     t: u64,
index f6a77d6decae048c278ef1de1d7fac4695c0d7f6..d40701860f46eb09ed85117e205ef3fe0fec0623 100644 (file)
@@ -969,7 +969,7 @@ fn min_max(&mut self) -> MinMaxResult<A> {
 }
 
 /// `MinMaxResult` is an enum returned by `min_max`. See `OrdIterator::min_max` for more detail.
-#[deriving(Clone, Eq)]
+#[deriving(Clone, Eq, Show)]
 pub enum MinMaxResult<T> {
     /// Empty iterator
     NoElements,
@@ -2329,19 +2329,48 @@ fn test_lt() {
 
 #[cfg(test)]
 mod tests {
-    use realstd::prelude::*;
-    use realstd::iter::*;
-    use realstd::num;
+    use prelude::*;
+    use iter::*;
+    use num;
+    use realstd::vec::Vec;
+    use realstd::slice::Vector;
 
     use cmp;
     use realstd::owned::Box;
     use uint;
 
+    impl<T> FromIterator<T> for Vec<T> {
+        fn from_iter<I: Iterator<T>>(mut iterator: I) -> Vec<T> {
+            let mut v = Vec::new();
+            for e in iterator {
+                v.push(e);
+            }
+            return v;
+        }
+    }
+
+    impl<'a, T> Iterator<&'a T> for ::realcore::slice::Items<'a, T> {
+        fn next(&mut self) -> Option<&'a T> {
+            use RealSome = realcore::option::Some;
+            use RealNone = realcore::option::None;
+            fn mynext<T, I: ::realcore::iter::Iterator<T>>(i: &mut I)
+                -> ::realcore::option::Option<T>
+            {
+                use realcore::iter::Iterator;
+                i.next()
+            }
+            match mynext(self) {
+                RealSome(t) => Some(t),
+                RealNone => None,
+            }
+        }
+    }
+
     #[test]
     fn test_counter_from_iter() {
         let it = count(0, 5).take(10);
         let xs: Vec<int> = FromIterator::from_iter(it);
-        assert_eq!(xs, vec![0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
+        assert!(xs == vec![0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
     }
 
     #[test]
@@ -2371,7 +2400,7 @@ fn test_iterator_chain() {
     fn test_filter_map() {
         let mut it = count(0u, 1u).take(10)
             .filter_map(|x| if x % 2 == 0 { Some(x*x) } else { None });
-        assert_eq!(it.collect::<Vec<uint>>(), vec![0*0, 2*2, 4*4, 6*6, 8*8]);
+        assert!(it.collect::<Vec<uint>>() == vec![0*0, 2*2, 4*4, 6*6, 8*8]);
     }
 
     #[test]
@@ -2630,7 +2659,7 @@ fn test_iterator_size_hint() {
     fn test_collect() {
         let a = vec![1, 2, 3, 4, 5];
         let b: Vec<int> = a.iter().map(|&x| x).collect();
-        assert_eq!(a, b);
+        assert!(a == b);
     }
 
     #[test]
@@ -2702,7 +2731,8 @@ fn test_rev() {
         let mut it = xs.iter();
         it.next();
         it.next();
-        assert_eq!(it.rev().map(|&x| x).collect::<Vec<int>>(), vec![16, 14, 12, 10, 8, 6]);
+        assert!(it.rev().map(|&x| x).collect::<Vec<int>>() ==
+                vec![16, 14, 12, 10, 8, 6]);
     }
 
     #[test]
@@ -2940,12 +2970,12 @@ fn test_random_access_cycle() {
 
     #[test]
     fn test_double_ended_range() {
-        assert_eq!(range(11i, 14).rev().collect::<Vec<int>>(), vec![13i, 12, 11]);
+        assert!(range(11i, 14).rev().collect::<Vec<int>>() == vec![13i, 12, 11]);
         for _ in range(10i, 0).rev() {
             fail!("unreachable");
         }
 
-        assert_eq!(range(11u, 14).rev().collect::<Vec<uint>>(), vec![13u, 12, 11]);
+        assert!(range(11u, 14).rev().collect::<Vec<uint>>() == vec![13u, 12, 11]);
         for _ in range(10u, 0).rev() {
             fail!("unreachable");
         }
@@ -2997,14 +3027,14 @@ fn one() -> Foo {
             }
         }
 
-        assert_eq!(range(0i, 5).collect::<Vec<int>>(), vec![0i, 1, 2, 3, 4]);
-        assert_eq!(range(-10i, -1).collect::<Vec<int>>(),
+        assert!(range(0i, 5).collect::<Vec<int>>() == vec![0i, 1, 2, 3, 4]);
+        assert!(range(-10i, -1).collect::<Vec<int>>() ==
                    vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]);
-        assert_eq!(range(0i, 5).rev().collect::<Vec<int>>(), vec![4, 3, 2, 1, 0]);
-        assert_eq!(range(200, -5).collect::<Vec<int>>(), vec![]);
-        assert_eq!(range(200, -5).rev().collect::<Vec<int>>(), vec![]);
-        assert_eq!(range(200, 200).collect::<Vec<int>>(), vec![]);
-        assert_eq!(range(200, 200).rev().collect::<Vec<int>>(), vec![]);
+        assert!(range(0i, 5).rev().collect::<Vec<int>>() == vec![4, 3, 2, 1, 0]);
+        assert_eq!(range(200, -5).len(), 0);
+        assert_eq!(range(200, -5).rev().len(), 0);
+        assert_eq!(range(200, 200).len(), 0);
+        assert_eq!(range(200, 200).rev().len(), 0);
 
         assert_eq!(range(0i, 100).size_hint(), (100, Some(100)));
         // this test is only meaningful when sizeof uint < sizeof u64
@@ -3015,32 +3045,44 @@ fn one() -> Foo {
 
     #[test]
     fn test_range_inclusive() {
-        assert_eq!(range_inclusive(0i, 5).collect::<Vec<int>>(), vec![0i, 1, 2, 3, 4, 5]);
-        assert_eq!(range_inclusive(0i, 5).rev().collect::<Vec<int>>(), vec![5i, 4, 3, 2, 1, 0]);
-        assert_eq!(range_inclusive(200, -5).collect::<Vec<int>>(), vec![]);
-        assert_eq!(range_inclusive(200, -5).rev().collect::<Vec<int>>(), vec![]);
-        assert_eq!(range_inclusive(200, 200).collect::<Vec<int>>(), vec![200]);
-        assert_eq!(range_inclusive(200, 200).rev().collect::<Vec<int>>(), vec![200]);
+        assert!(range_inclusive(0i, 5).collect::<Vec<int>>() ==
+                vec![0i, 1, 2, 3, 4, 5]);
+        assert!(range_inclusive(0i, 5).rev().collect::<Vec<int>>() ==
+                vec![5i, 4, 3, 2, 1, 0]);
+        assert_eq!(range_inclusive(200, -5).len(), 0);
+        assert_eq!(range_inclusive(200, -5).rev().len(), 0);
+        assert!(range_inclusive(200, 200).collect::<Vec<int>>() == vec![200]);
+        assert!(range_inclusive(200, 200).rev().collect::<Vec<int>>() == vec![200]);
     }
 
     #[test]
     fn test_range_step() {
-        assert_eq!(range_step(0i, 20, 5).collect::<Vec<int>>(), vec![0, 5, 10, 15]);
-        assert_eq!(range_step(20i, 0, -5).collect::<Vec<int>>(), vec![20, 15, 10, 5]);
-        assert_eq!(range_step(20i, 0, -6).collect::<Vec<int>>(), vec![20, 14, 8, 2]);
-        assert_eq!(range_step(200u8, 255, 50).collect::<Vec<u8>>(), vec![200u8, 250]);
-        assert_eq!(range_step(200, -5, 1).collect::<Vec<int>>(), vec![]);
-        assert_eq!(range_step(200, 200, 1).collect::<Vec<int>>(), vec![]);
+        assert!(range_step(0i, 20, 5).collect::<Vec<int>>() ==
+                vec![0, 5, 10, 15]);
+        assert!(range_step(20i, 0, -5).collect::<Vec<int>>() ==
+                vec![20, 15, 10, 5]);
+        assert!(range_step(20i, 0, -6).collect::<Vec<int>>() ==
+                vec![20, 14, 8, 2]);
+        assert!(range_step(200u8, 255, 50).collect::<Vec<u8>>() ==
+                vec![200u8, 250]);
+        assert!(range_step(200, -5, 1).collect::<Vec<int>>() == vec![]);
+        assert!(range_step(200, 200, 1).collect::<Vec<int>>() == vec![]);
     }
 
     #[test]
     fn test_range_step_inclusive() {
-        assert_eq!(range_step_inclusive(0i, 20, 5).collect::<Vec<int>>(), vec![0, 5, 10, 15, 20]);
-        assert_eq!(range_step_inclusive(20i, 0, -5).collect::<Vec<int>>(), vec![20, 15, 10, 5, 0]);
-        assert_eq!(range_step_inclusive(20i, 0, -6).collect::<Vec<int>>(), vec![20, 14, 8, 2]);
-        assert_eq!(range_step_inclusive(200u8, 255, 50).collect::<Vec<u8>>(), vec![200u8, 250]);
-        assert_eq!(range_step_inclusive(200, -5, 1).collect::<Vec<int>>(), vec![]);
-        assert_eq!(range_step_inclusive(200, 200, 1).collect::<Vec<int>>(), vec![200]);
+        assert!(range_step_inclusive(0i, 20, 5).collect::<Vec<int>>() ==
+                vec![0, 5, 10, 15, 20]);
+        assert!(range_step_inclusive(20i, 0, -5).collect::<Vec<int>>() ==
+                vec![20, 15, 10, 5, 0]);
+        assert!(range_step_inclusive(20i, 0, -6).collect::<Vec<int>>() ==
+                vec![20, 14, 8, 2]);
+        assert!(range_step_inclusive(200u8, 255, 50).collect::<Vec<u8>>() ==
+                vec![200u8, 250]);
+        assert!(range_step_inclusive(200, -5, 1).collect::<Vec<int>>() ==
+                vec![]);
+        assert!(range_step_inclusive(200, 200, 1).collect::<Vec<int>>() ==
+                vec![200]);
     }
 
     #[test]
index 22719dc9f2d71daaf44e185b4a38f661b0c217bb..05b314b6998478a998fad6e2fc6b947cb5474ec0 100644 (file)
@@ -11,7 +11,7 @@
 //! The Rust core library
 //!
 //! This library is meant to represent the core functionality of rust that is
-//! maximally portable to other platforms. To that exent, this library has no
+//! maximally portable to other platforms. To that extent, this library has no
 //! knowledge of things like allocation, threads, I/O, etc. This library is
 //! built on the assumption of a few existing symbols:
 //!
 #[cfg(test)] extern crate realcore = "core";
 #[cfg(test)] extern crate libc;
 #[cfg(test)] extern crate native;
-#[phase(syntax, link)] #[cfg(test)] extern crate realstd = "std";
-#[phase(syntax, link)] #[cfg(test)] extern crate log;
+#[cfg(test)] extern crate rand;
+#[cfg(test)] extern crate realstd = "std";
 
 #[cfg(test)] pub use cmp = realcore::cmp;
 #[cfg(test)] pub use kinds = realcore::kinds;
 #[cfg(test)] pub use ops = realcore::ops;
 #[cfg(test)] pub use ty = realcore::ty;
 
-#[cfg(not(test))]
 mod macros;
 
 #[path = "num/float_macros.rs"] mod float_macros;
 pub mod slice;
 pub mod str;
 pub mod tuple;
+pub mod fmt;
 
 // FIXME: this module should not exist. Once owned allocations are no longer a
 //        language type, this module can move outside to the owned allocation
@@ -131,10 +131,12 @@ mod std {
     pub use clone;
     pub use cmp;
     pub use kinds;
+    pub use option;
+    pub use fmt;
 
-    #[cfg(test)] pub use realstd::fmt;    // needed for fail!()
     #[cfg(test)] pub use realstd::rt;     // needed for fail!()
-    #[cfg(test)] pub use realstd::option; // needed for assert!()
+    // #[cfg(test)] pub use realstd::option; // needed for fail!()
+    // #[cfg(test)] pub use realstd::fmt;    // needed for fail!()
     #[cfg(test)] pub use realstd::os;     // needed for tests
     #[cfg(test)] pub use realstd::slice;  // needed for tests
     #[cfg(test)] pub use realstd::vec;    // needed for vec![]
index 69be68a34a130eef16c8d8e5e41d80395cb6062e..6474c5e37a44b46cb6930a35fef275ee3f085526 100644 (file)
 #[macro_export]
 macro_rules! fail(
     () => (
-        fail!("explicit failure")
+        fail!("{}", "explicit failure")
     );
     ($msg:expr) => (
-        ::core::failure::begin_unwind($msg, file!(), line!())
+        fail!("{}", $msg)
     );
+    ($fmt:expr, $($arg:tt)*) => ({
+        // a closure can't have return type !, so we need a full
+        // function to pass to format_args!, *and* we need the
+        // file and line numbers right here; so an inner bare fn
+        // is our only choice.
+        //
+        // LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
+        // is #[cold] and #[inline(never)] and because this is flagged as cold
+        // as returning !. We really do want this to be inlined, however,
+        // because it's just a tiny wrapper. Small wins (156K to 149K in size)
+        // were seen when forcing this to be inlined, and that number just goes
+        // up with the number of calls to fail!()
+        #[inline(always)]
+        fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
+            ::core::failure::begin_unwind(fmt, file!(), line!())
+        }
+        format_args!(run_fmt, $fmt, $($arg)*)
+    });
 )
 
 /// Runtime assertion, for details see std::macros
@@ -29,6 +47,22 @@ macro_rules! assert(
             fail!(concat!("assertion failed: ", stringify!($cond)))
         }
     );
+    ($cond:expr, $($arg:tt)*) => (
+        if !$cond {
+            fail!($($arg)*)
+        }
+    );
+)
+
+/// Runtime assertion for equality, for details see std::macros
+macro_rules! assert_eq(
+    ($cond1:expr, $cond2:expr) => ({
+        let c1 = $cond1;
+        let c2 = $cond2;
+        if c1 != c2 || c2 != c1 {
+            fail!("expressions not equal, left: {}, right: {}", c1, c2);
+        }
+    })
 )
 
 /// Runtime assertion, disableable at compile time
@@ -36,3 +70,19 @@ macro_rules! assert(
 macro_rules! debug_assert(
     ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
 )
+
+/// Short circuiting evaluation on Err
+#[macro_export]
+macro_rules! try(
+    ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
+)
+
+#[cfg(test)]
+macro_rules! vec( ($($e:expr),*) => ({
+    let mut _v = ::std::vec::Vec::new();
+    $(_v.push($e);)*
+    _v
+}) )
+
+#[cfg(test)]
+macro_rules! format( ($($arg:tt)*) => (format_args!(::fmt::format, $($arg)*)) )
index c4cdc5a0a4017e90d3ce7af9d2c18b99b11cdbbe..694f3e9fbd1f90ee2292975927d140421c4009a3 100644 (file)
 
 use default::Default;
 use intrinsics;
-use num::{Zero, One, Bounded, Signed, Num, Primitive};
+use mem;
+use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
+use num::{Zero, One, Bounded, Signed, Num, Primitive, Float};
+use option::Option;
 
 #[cfg(not(test))] use cmp::{Eq, Ord};
 #[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg};
@@ -225,3 +228,270 @@ fn min_value() -> f32 { -MAX_VALUE }
     #[inline]
     fn max_value() -> f32 { MAX_VALUE }
 }
+
+impl Float for f32 {
+    #[inline]
+    fn nan() -> f32 { NAN }
+
+    #[inline]
+    fn infinity() -> f32 { INFINITY }
+
+    #[inline]
+    fn neg_infinity() -> f32 { NEG_INFINITY }
+
+    #[inline]
+    fn neg_zero() -> f32 { -0.0 }
+
+    /// Returns `true` if the number is NaN
+    #[inline]
+    fn is_nan(self) -> bool { self != self }
+
+    /// Returns `true` if the number is infinite
+    #[inline]
+    fn is_infinite(self) -> bool {
+        self == Float::infinity() || self == Float::neg_infinity()
+    }
+
+    /// Returns `true` if the number is neither infinite or NaN
+    #[inline]
+    fn is_finite(self) -> bool {
+        !(self.is_nan() || self.is_infinite())
+    }
+
+    /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
+    #[inline]
+    fn is_normal(self) -> bool {
+        self.classify() == FPNormal
+    }
+
+    /// Returns the floating point category of the number. If only one property
+    /// is going to be tested, it is generally faster to use the specific
+    /// predicate instead.
+    fn classify(self) -> FPCategory {
+        static EXP_MASK: u32 = 0x7f800000;
+        static MAN_MASK: u32 = 0x007fffff;
+
+        let bits: u32 = unsafe { mem::transmute(self) };
+        match (bits & MAN_MASK, bits & EXP_MASK) {
+            (0, 0)        => FPZero,
+            (_, 0)        => FPSubnormal,
+            (0, EXP_MASK) => FPInfinite,
+            (_, EXP_MASK) => FPNaN,
+            _             => FPNormal,
+        }
+    }
+
+    #[inline]
+    fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS }
+
+    #[inline]
+    fn digits(_: Option<f32>) -> uint { DIGITS }
+
+    #[inline]
+    fn epsilon() -> f32 { EPSILON }
+
+    #[inline]
+    fn min_exp(_: Option<f32>) -> int { MIN_EXP }
+
+    #[inline]
+    fn max_exp(_: Option<f32>) -> int { MAX_EXP }
+
+    #[inline]
+    fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP }
+
+    #[inline]
+    fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
+
+    #[inline]
+    fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
+
+    /// Returns the mantissa, exponent and sign as integers.
+    fn integer_decode(self) -> (u64, i16, i8) {
+        let bits: u32 = unsafe { mem::transmute(self) };
+        let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
+        let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
+        let mantissa = if exponent == 0 {
+            (bits & 0x7fffff) << 1
+        } else {
+            (bits & 0x7fffff) | 0x800000
+        };
+        // Exponent bias + mantissa shift
+        exponent -= 127 + 23;
+        (mantissa as u64, exponent, sign)
+    }
+
+    /// Round half-way cases toward `NEG_INFINITY`
+    #[inline]
+    fn floor(self) -> f32 {
+        unsafe { intrinsics::floorf32(self) }
+    }
+
+    /// Round half-way cases toward `INFINITY`
+    #[inline]
+    fn ceil(self) -> f32 {
+        unsafe { intrinsics::ceilf32(self) }
+    }
+
+    /// Round half-way cases away from `0.0`
+    #[inline]
+    fn round(self) -> f32 {
+        unsafe { intrinsics::roundf32(self) }
+    }
+
+    /// The integer part of the number (rounds towards `0.0`)
+    #[inline]
+    fn trunc(self) -> f32 {
+        unsafe { intrinsics::truncf32(self) }
+    }
+
+    /// The fractional part of the number, satisfying:
+    ///
+    /// ```rust
+    /// let x = 1.65f32;
+    /// assert!(x == x.trunc() + x.fract())
+    /// ```
+    #[inline]
+    fn fract(self) -> f32 { self - self.trunc() }
+
+    /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
+    /// error. This produces a more accurate result with better performance than
+    /// a separate multiplication operation followed by an add.
+    #[inline]
+    fn mul_add(self, a: f32, b: f32) -> f32 {
+        unsafe { intrinsics::fmaf32(self, a, b) }
+    }
+
+    /// The reciprocal (multiplicative inverse) of the number
+    #[inline]
+    fn recip(self) -> f32 { 1.0 / self }
+
+    fn powi(self, n: i32) -> f32 {
+        unsafe { intrinsics::powif32(self, n) }
+    }
+
+    #[inline]
+    fn powf(self, n: f32) -> f32 {
+        unsafe { intrinsics::powf32(self, n) }
+    }
+
+    /// sqrt(2.0)
+    #[inline]
+    fn sqrt2() -> f32 { consts::SQRT2 }
+
+    /// 1.0 / sqrt(2.0)
+    #[inline]
+    fn frac_1_sqrt2() -> f32 { consts::FRAC_1_SQRT2 }
+
+    #[inline]
+    fn sqrt(self) -> f32 {
+        unsafe { intrinsics::sqrtf32(self) }
+    }
+
+    #[inline]
+    fn rsqrt(self) -> f32 { self.sqrt().recip() }
+
+    /// Archimedes' constant
+    #[inline]
+    fn pi() -> f32 { consts::PI }
+
+    /// 2.0 * pi
+    #[inline]
+    fn two_pi() -> f32 { consts::PI_2 }
+
+    /// pi / 2.0
+    #[inline]
+    fn frac_pi_2() -> f32 { consts::FRAC_PI_2 }
+
+    /// pi / 3.0
+    #[inline]
+    fn frac_pi_3() -> f32 { consts::FRAC_PI_3 }
+
+    /// pi / 4.0
+    #[inline]
+    fn frac_pi_4() -> f32 { consts::FRAC_PI_4 }
+
+    /// pi / 6.0
+    #[inline]
+    fn frac_pi_6() -> f32 { consts::FRAC_PI_6 }
+
+    /// pi / 8.0
+    #[inline]
+    fn frac_pi_8() -> f32 { consts::FRAC_PI_8 }
+
+    /// 1 .0/ pi
+    #[inline]
+    fn frac_1_pi() -> f32 { consts::FRAC_1_PI }
+
+    /// 2.0 / pi
+    #[inline]
+    fn frac_2_pi() -> f32 { consts::FRAC_2_PI }
+
+    /// 2.0 / sqrt(pi)
+    #[inline]
+    fn frac_2_sqrtpi() -> f32 { consts::FRAC_2_SQRTPI }
+
+    /// Euler's number
+    #[inline]
+    fn e() -> f32 { consts::E }
+
+    /// log2(e)
+    #[inline]
+    fn log2_e() -> f32 { consts::LOG2_E }
+
+    /// log10(e)
+    #[inline]
+    fn log10_e() -> f32 { consts::LOG10_E }
+
+    /// ln(2.0)
+    #[inline]
+    fn ln_2() -> f32 { consts::LN_2 }
+
+    /// ln(10.0)
+    #[inline]
+    fn ln_10() -> f32 { consts::LN_10 }
+
+    /// Returns the exponential of the number
+    #[inline]
+    fn exp(self) -> f32 {
+        unsafe { intrinsics::expf32(self) }
+    }
+
+    /// Returns 2 raised to the power of the number
+    #[inline]
+    fn exp2(self) -> f32 {
+        unsafe { intrinsics::exp2f32(self) }
+    }
+
+    /// Returns the natural logarithm of the number
+    #[inline]
+    fn ln(self) -> f32 {
+        unsafe { intrinsics::logf32(self) }
+    }
+
+    /// Returns the logarithm of the number with respect to an arbitrary base
+    #[inline]
+    fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
+
+    /// Returns the base 2 logarithm of the number
+    #[inline]
+    fn log2(self) -> f32 {
+        unsafe { intrinsics::log2f32(self) }
+    }
+
+    /// Returns the base 10 logarithm of the number
+    #[inline]
+    fn log10(self) -> f32 {
+        unsafe { intrinsics::log10f32(self) }
+    }
+
+    /// Converts to degrees, assuming the number is in radians
+    #[inline]
+    fn to_degrees(self) -> f32 { self * (180.0f32 / Float::pi()) }
+
+    /// Converts to radians, assuming the number is in degrees
+    #[inline]
+    fn to_radians(self) -> f32 {
+        let value: f32 = Float::pi();
+        self * (value / 180.0f32)
+    }
+}
index b15b4566cdd689e9f1648fa0d96d7550d22d0137..2c802f5d059f1f3dcb7226321f0142a887b16396 100644 (file)
 
 use default::Default;
 use intrinsics;
-use num::{Zero, One, Bounded, Signed, Num, Primitive};
+use mem;
+use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
+use num::{Zero, One, Bounded, Signed, Num, Primitive, Float};
+use option::Option;
 
 #[cfg(not(test))] use cmp::{Eq, Ord};
 #[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg};
@@ -225,3 +228,273 @@ fn min_value() -> f64 { -MAX_VALUE }
     #[inline]
     fn max_value() -> f64 { MAX_VALUE }
 }
+
+impl Float for f64 {
+    #[inline]
+    fn nan() -> f64 { NAN }
+
+    #[inline]
+    fn infinity() -> f64 { INFINITY }
+
+    #[inline]
+    fn neg_infinity() -> f64 { NEG_INFINITY }
+
+    #[inline]
+    fn neg_zero() -> f64 { -0.0 }
+
+    /// Returns `true` if the number is NaN
+    #[inline]
+    fn is_nan(self) -> bool { self != self }
+
+    /// Returns `true` if the number is infinite
+    #[inline]
+    fn is_infinite(self) -> bool {
+        self == Float::infinity() || self == Float::neg_infinity()
+    }
+
+    /// Returns `true` if the number is neither infinite or NaN
+    #[inline]
+    fn is_finite(self) -> bool {
+        !(self.is_nan() || self.is_infinite())
+    }
+
+    /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
+    #[inline]
+    fn is_normal(self) -> bool {
+        self.classify() == FPNormal
+    }
+
+    /// Returns the floating point category of the number. If only one property
+    /// is going to be tested, it is generally faster to use the specific
+    /// predicate instead.
+    fn classify(self) -> FPCategory {
+        static EXP_MASK: u64 = 0x7ff0000000000000;
+        static MAN_MASK: u64 = 0x000fffffffffffff;
+
+        let bits: u64 = unsafe { mem::transmute(self) };
+        match (bits & MAN_MASK, bits & EXP_MASK) {
+            (0, 0)        => FPZero,
+            (_, 0)        => FPSubnormal,
+            (0, EXP_MASK) => FPInfinite,
+            (_, EXP_MASK) => FPNaN,
+            _             => FPNormal,
+        }
+    }
+
+    #[inline]
+    fn mantissa_digits(_: Option<f64>) -> uint { MANTISSA_DIGITS }
+
+    #[inline]
+    fn digits(_: Option<f64>) -> uint { DIGITS }
+
+    #[inline]
+    fn epsilon() -> f64 { EPSILON }
+
+    #[inline]
+    fn min_exp(_: Option<f64>) -> int { MIN_EXP }
+
+    #[inline]
+    fn max_exp(_: Option<f64>) -> int { MAX_EXP }
+
+    #[inline]
+    fn min_10_exp(_: Option<f64>) -> int { MIN_10_EXP }
+
+    #[inline]
+    fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP }
+
+    #[inline]
+    fn min_pos_value(_: Option<f64>) -> f64 { MIN_POS_VALUE }
+
+    /// Returns the mantissa, exponent and sign as integers.
+    fn integer_decode(self) -> (u64, i16, i8) {
+        let bits: u64 = unsafe { mem::transmute(self) };
+        let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
+        let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
+        let mantissa = if exponent == 0 {
+            (bits & 0xfffffffffffff) << 1
+        } else {
+            (bits & 0xfffffffffffff) | 0x10000000000000
+        };
+        // Exponent bias + mantissa shift
+        exponent -= 1023 + 52;
+        (mantissa, exponent, sign)
+    }
+
+    /// Round half-way cases toward `NEG_INFINITY`
+    #[inline]
+    fn floor(self) -> f64 {
+        unsafe { intrinsics::floorf64(self) }
+    }
+
+    /// Round half-way cases toward `INFINITY`
+    #[inline]
+    fn ceil(self) -> f64 {
+        unsafe { intrinsics::ceilf64(self) }
+    }
+
+    /// Round half-way cases away from `0.0`
+    #[inline]
+    fn round(self) -> f64 {
+        unsafe { intrinsics::roundf64(self) }
+    }
+
+    /// The integer part of the number (rounds towards `0.0`)
+    #[inline]
+    fn trunc(self) -> f64 {
+        unsafe { intrinsics::truncf64(self) }
+    }
+
+    /// The fractional part of the number, satisfying:
+    ///
+    /// ```rust
+    /// let x = 1.65f64;
+    /// assert!(x == x.trunc() + x.fract())
+    /// ```
+    #[inline]
+    fn fract(self) -> f64 { self - self.trunc() }
+
+    /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
+    /// error. This produces a more accurate result with better performance than
+    /// a separate multiplication operation followed by an add.
+    #[inline]
+    fn mul_add(self, a: f64, b: f64) -> f64 {
+        unsafe { intrinsics::fmaf64(self, a, b) }
+    }
+
+    /// The reciprocal (multiplicative inverse) of the number
+    #[inline]
+    fn recip(self) -> f64 { 1.0 / self }
+
+    #[inline]
+    fn powf(self, n: f64) -> f64 {
+        unsafe { intrinsics::powf64(self, n) }
+    }
+
+    #[inline]
+    fn powi(self, n: i32) -> f64 {
+        unsafe { intrinsics::powif64(self, n) }
+    }
+
+    /// sqrt(2.0)
+    #[inline]
+    fn sqrt2() -> f64 { consts::SQRT2 }
+
+    /// 1.0 / sqrt(2.0)
+    #[inline]
+    fn frac_1_sqrt2() -> f64 { consts::FRAC_1_SQRT2 }
+
+    #[inline]
+    fn sqrt(self) -> f64 {
+        unsafe { intrinsics::sqrtf64(self) }
+    }
+
+    #[inline]
+    fn rsqrt(self) -> f64 { self.sqrt().recip() }
+
+    /// Archimedes' constant
+    #[inline]
+    fn pi() -> f64 { consts::PI }
+
+    /// 2.0 * pi
+    #[inline]
+    fn two_pi() -> f64 { consts::PI_2 }
+
+    /// pi / 2.0
+    #[inline]
+    fn frac_pi_2() -> f64 { consts::FRAC_PI_2 }
+
+    /// pi / 3.0
+    #[inline]
+    fn frac_pi_3() -> f64 { consts::FRAC_PI_3 }
+
+    /// pi / 4.0
+    #[inline]
+    fn frac_pi_4() -> f64 { consts::FRAC_PI_4 }
+
+    /// pi / 6.0
+    #[inline]
+    fn frac_pi_6() -> f64 { consts::FRAC_PI_6 }
+
+    /// pi / 8.0
+    #[inline]
+    fn frac_pi_8() -> f64 { consts::FRAC_PI_8 }
+
+    /// 1.0 / pi
+    #[inline]
+    fn frac_1_pi() -> f64 { consts::FRAC_1_PI }
+
+    /// 2.0 / pi
+    #[inline]
+    fn frac_2_pi() -> f64 { consts::FRAC_2_PI }
+
+    /// 2.0 / sqrt(pi)
+    #[inline]
+    fn frac_2_sqrtpi() -> f64 { consts::FRAC_2_SQRTPI }
+
+    /// Euler's number
+    #[inline]
+    fn e() -> f64 { consts::E }
+
+    /// log2(e)
+    #[inline]
+    fn log2_e() -> f64 { consts::LOG2_E }
+
+    /// log10(e)
+    #[inline]
+    fn log10_e() -> f64 { consts::LOG10_E }
+
+    /// ln(2.0)
+    #[inline]
+    fn ln_2() -> f64 { consts::LN_2 }
+
+    /// ln(10.0)
+    #[inline]
+    fn ln_10() -> f64 { consts::LN_10 }
+
+    /// Returns the exponential of the number
+    #[inline]
+    fn exp(self) -> f64 {
+        unsafe { intrinsics::expf64(self) }
+    }
+
+    /// Returns 2 raised to the power of the number
+    #[inline]
+    fn exp2(self) -> f64 {
+        unsafe { intrinsics::exp2f64(self) }
+    }
+
+    /// Returns the natural logarithm of the number
+    #[inline]
+    fn ln(self) -> f64 {
+        unsafe { intrinsics::logf64(self) }
+    }
+
+    /// Returns the logarithm of the number with respect to an arbitrary base
+    #[inline]
+    fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
+
+    /// Returns the base 2 logarithm of the number
+    #[inline]
+    fn log2(self) -> f64 {
+        unsafe { intrinsics::log2f64(self) }
+    }
+
+    /// Returns the base 10 logarithm of the number
+    #[inline]
+    fn log10(self) -> f64 {
+        unsafe { intrinsics::log10f64(self) }
+    }
+
+
+    /// Converts to degrees, assuming the number is in radians
+    #[inline]
+    fn to_degrees(self) -> f64 { self * (180.0f64 / Float::pi()) }
+
+    /// Converts to radians, assuming the number is in degrees
+    #[inline]
+    fn to_radians(self) -> f64 {
+        let value: f64 = Float::pi();
+        self * (value / 180.0)
+    }
+}
+
index 22411fef3b26801884c5a5d94ae81274c08c9dac..47be5df67eabdb345c963e9cc58ccd9dfcaf8c39 100644 (file)
@@ -874,3 +874,157 @@ pub fn test_num<T:Num + NumCast + ::std::fmt::Show>(ten: T, two: T) {
     assert_eq!(ten.div(&two),  ten / two);
     assert_eq!(ten.rem(&two),  ten % two);
 }
+
+/// Used for representing the classification of floating point numbers
+#[deriving(Eq, Show)]
+pub enum FPCategory {
+    /// "Not a Number", often obtained by dividing by zero
+    FPNaN,
+    /// Positive or negative infinity
+    FPInfinite ,
+    /// Positive or negative zero
+    FPZero,
+    /// De-normalized floating point representation (less precise than `FPNormal`)
+    FPSubnormal,
+    /// A regular floating point number
+    FPNormal,
+}
+
+/// Operations on primitive floating point numbers.
+// FIXME(#5527): In a future version of Rust, many of these functions will
+//               become constants.
+//
+// FIXME(#8888): Several of these functions have a parameter named
+//               `unused_self`. Removing it requires #8888 to be fixed.
+pub trait Float: Signed + Primitive {
+    /// Returns the NaN value.
+    fn nan() -> Self;
+    /// Returns the infinite value.
+    fn infinity() -> Self;
+    /// Returns the negative infinite value.
+    fn neg_infinity() -> Self;
+    /// Returns -0.0.
+    fn neg_zero() -> Self;
+
+    /// Returns true if this value is NaN and false otherwise.
+    fn is_nan(self) -> bool;
+    /// Returns true if this value is positive infinity or negative infinity and
+    /// false otherwise.
+    fn is_infinite(self) -> bool;
+    /// Returns true if this number is neither infinite nor NaN.
+    fn is_finite(self) -> bool;
+    /// Returns true if this number is neither zero, infinite, denormal, or NaN.
+    fn is_normal(self) -> bool;
+    /// Returns the category that this number falls into.
+    fn classify(self) -> FPCategory;
+
+    // FIXME (#5527): These should be associated constants
+
+    /// Returns the number of binary digits of mantissa that this type supports.
+    fn mantissa_digits(unused_self: Option<Self>) -> uint;
+    /// Returns the number of base-10 digits of precision that this type supports.
+    fn digits(unused_self: Option<Self>) -> uint;
+    /// Returns the difference between 1.0 and the smallest representable number larger than 1.0.
+    fn epsilon() -> Self;
+    /// Returns the minimum binary exponent that this type can represent.
+    fn min_exp(unused_self: Option<Self>) -> int;
+    /// Returns the maximum binary exponent that this type can represent.
+    fn max_exp(unused_self: Option<Self>) -> int;
+    /// Returns the minimum base-10 exponent that this type can represent.
+    fn min_10_exp(unused_self: Option<Self>) -> int;
+    /// Returns the maximum base-10 exponent that this type can represent.
+    fn max_10_exp(unused_self: Option<Self>) -> int;
+    /// Returns the smallest normalized positive number that this type can represent.
+    fn min_pos_value(unused_self: Option<Self>) -> Self;
+
+    /// Returns the mantissa, exponent and sign as integers, respectively.
+    fn integer_decode(self) -> (u64, i16, i8);
+
+    /// Return the largest integer less than or equal to a number.
+    fn floor(self) -> Self;
+    /// Return the smallest integer greater than or equal to a number.
+    fn ceil(self) -> Self;
+    /// Return the nearest integer to a number. Round half-way cases away from
+    /// `0.0`.
+    fn round(self) -> Self;
+    /// Return the integer part of a number.
+    fn trunc(self) -> Self;
+    /// Return the fractional part of a number.
+    fn fract(self) -> Self;
+
+    /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
+    /// error. This produces a more accurate result with better performance than
+    /// a separate multiplication operation followed by an add.
+    fn mul_add(self, a: Self, b: Self) -> Self;
+    /// Take the reciprocal (inverse) of a number, `1/x`.
+    fn recip(self) -> Self;
+
+    /// Raise a number to an integer power.
+    ///
+    /// Using this function is generally faster than using `powf`
+    fn powi(self, n: i32) -> Self;
+    /// Raise a number to a floating point power.
+    fn powf(self, n: Self) -> Self;
+
+    /// sqrt(2.0).
+    fn sqrt2() -> Self;
+    /// 1.0 / sqrt(2.0).
+    fn frac_1_sqrt2() -> Self;
+
+    /// Take the square root of a number.
+    fn sqrt(self) -> Self;
+    /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
+    fn rsqrt(self) -> Self;
+
+    // FIXME (#5527): These should be associated constants
+
+    /// Archimedes' constant.
+    fn pi() -> Self;
+    /// 2.0 * pi.
+    fn two_pi() -> Self;
+    /// pi / 2.0.
+    fn frac_pi_2() -> Self;
+    /// pi / 3.0.
+    fn frac_pi_3() -> Self;
+    /// pi / 4.0.
+    fn frac_pi_4() -> Self;
+    /// pi / 6.0.
+    fn frac_pi_6() -> Self;
+    /// pi / 8.0.
+    fn frac_pi_8() -> Self;
+    /// 1.0 / pi.
+    fn frac_1_pi() -> Self;
+    /// 2.0 / pi.
+    fn frac_2_pi() -> Self;
+    /// 2.0 / sqrt(pi).
+    fn frac_2_sqrtpi() -> Self;
+
+    /// Euler's number.
+    fn e() -> Self;
+    /// log2(e).
+    fn log2_e() -> Self;
+    /// log10(e).
+    fn log10_e() -> Self;
+    /// ln(2.0).
+    fn ln_2() -> Self;
+    /// ln(10.0).
+    fn ln_10() -> Self;
+
+    /// Returns `e^(self)`, (the exponential function).
+    fn exp(self) -> Self;
+    /// Returns 2 raised to the power of the number, `2^(self)`.
+    fn exp2(self) -> Self;
+    /// Returns the natural logarithm of the number.
+    fn ln(self) -> Self;
+    /// Returns the logarithm of the number with respect to an arbitrary base.
+    fn log(self, base: Self) -> Self;
+    /// Returns the base 2 logarithm of the number.
+    fn log2(self) -> Self;
+    /// Returns the base 10 logarithm of the number.
+    fn log10(self) -> Self;
+
+    /// Convert radians to degrees.
+    fn to_degrees(self) -> Self;
+    /// Convert degrees to radians.
+    fn to_radians(self) -> Self;
+}
index 886b7315152b8e1eb54bbe94f32051ebf9adbf80..00f21ee4c9cedee495cd2d9dfd8e400cdc95d223 100644 (file)
 use slice;
 
 /// The `Option`
-#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
+#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd, Show)]
 pub enum Option<T> {
     /// No value
     None,
@@ -595,9 +595,11 @@ pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) ->
 
 #[cfg(test)]
 mod tests {
-    use realstd::option::collect;
-    use realstd::prelude::*;
-    use realstd::iter::range;
+    use realstd::vec::Vec;
+    use realstd::str::StrAllocating;
+    use option::collect;
+    use prelude::*;
+    use iter::range;
 
     use str::StrSlice;
     use kinds::marker;
@@ -638,7 +640,7 @@ struct R {
         impl ::ops::Drop for R {
            fn drop(&mut self) {
                 let ii = &*self.i;
-                let i = ii.borrow().clone();
+                let i = *ii.borrow();
                 *ii.borrow_mut() = i + 1;
             }
         }
@@ -649,9 +651,14 @@ fn R(i: Rc<RefCell<int>>) -> R {
             }
         }
 
+        fn realclone<T: ::realstd::clone::Clone>(t: &T) -> T {
+            use realstd::clone::Clone;
+            t.clone()
+        }
+
         let i = Rc::new(RefCell::new(0));
         {
-            let x = R(i.clone());
+            let x = R(realclone(&i));
             let opt = Some(x);
             let _y = opt.unwrap();
         }
@@ -849,21 +856,21 @@ fn test_mutate() {
     fn test_collect() {
         let v: Option<Vec<int>> = collect(range(0, 0)
                                           .map(|_| Some(0)));
-        assert_eq!(v, Some(vec![]));
+        assert!(v == Some(vec![]));
 
         let v: Option<Vec<int>> = collect(range(0, 3)
                                           .map(|x| Some(x)));
-        assert_eq!(v, Some(vec![0, 1, 2]));
+        assert!(v == Some(vec![0, 1, 2]));
 
         let v: Option<Vec<int>> = collect(range(0, 3)
                                           .map(|x| if x > 1 { None } else { Some(x) }));
-        assert_eq!(v, None);
+        assert!(v == None);
 
         // test that it does not take more elements than it needs
         let mut functions = [|| Some(()), || None, || fail!()];
 
         let v: Option<Vec<()>> = collect(functions.mut_iter().map(|f| (*f)()));
 
-        assert_eq!(v, None);
+        assert!(v == None);
     }
 }
index efd6732f6530a80360419c2a434bad6aa0eb29e4..2c6b0af8d94e6b34d2bc62d809c519c4ae90b11d 100644 (file)
@@ -35,7 +35,7 @@
 pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
 pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
 pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
-pub use num::{Signed, Unsigned};
+pub use num::{Signed, Unsigned, Float};
 pub use num::{Primitive, Int, ToPrimitive, FromPrimitive};
 pub use ptr::RawPtr;
 pub use str::{Str, StrSlice};
index 438e18d999b6c77b5369b8b01aafa02b16e07688..acdf0bf06589f6a7dcaa1874f1ad9b0316b1650b 100644 (file)
@@ -480,7 +480,7 @@ fn lt(&self, other: &*mut T) -> bool { *self < *other }
 #[cfg(test)]
 pub mod ptr_tests {
     use super::*;
-    use realstd::prelude::*;
+    use prelude::*;
 
     use realstd::c_str::ToCStr;
     use mem;
@@ -660,9 +660,6 @@ fn test_ptr_array_each_with_len() {
                     let expected = expected_arr[ctr].with_ref(|buf| {
                             str::raw::from_c_str(buf)
                         });
-                    debug!(
-                        "test_ptr_array_each_with_len e: {}, a: {}",
-                        expected, actual);
                     assert_eq!(actual, expected);
                     ctr += 1;
                     iteration_count += 1;
@@ -696,9 +693,6 @@ fn test_ptr_array_each() {
                     let expected = expected_arr[ctr].with_ref(|buf| {
                         str::raw::from_c_str(buf)
                     });
-                    debug!(
-                        "test_ptr_array_each e: {}, a: {}",
-                        expected, actual);
                     assert_eq!(actual, expected);
                     ctr += 1;
                     iteration_count += 1;
index cced502846032f0ac3713079898fd9a7762e55a3..3237269e4a64fb0dbe8a22e9822055c327e4ae29 100644 (file)
 
 use clone::Clone;
 use cmp::Eq;
+use std::fmt::Show;
 use iter::{Iterator, FromIterator};
 use option::{None, Option, Some};
 
 /// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
 ///
 /// See the [`std::result`](index.html) module documentation for details.
-#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
+#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd, Show)]
 #[must_use]
 pub enum Result<T, E> {
     /// Contains the success value
@@ -515,6 +516,34 @@ pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
     }
 }
 
+impl<T, E: Show> Result<T, E> {
+    /// Unwraps a result, yielding the content of an `Ok`.
+    ///
+    /// Fails if the value is an `Err`.
+    #[inline]
+    pub fn unwrap(self) -> T {
+        match self {
+            Ok(t) => t,
+            Err(e) =>
+                fail!("called `Result::unwrap()` on an `Err` value: {}", e)
+        }
+    }
+}
+
+impl<T: Show, E> Result<T, E> {
+    /// Unwraps a result, yielding the content of an `Err`.
+    ///
+    /// Fails if the value is an `Ok`.
+    #[inline]
+    pub fn unwrap_err(self) -> E {
+        match self {
+            Ok(t) =>
+                fail!("called `Result::unwrap_err()` on an `Ok` value: {}", t),
+            Err(e) => e
+        }
+    }
+}
+
 /////////////////////////////////////////////////////////////////////////////
 // Free functions
 /////////////////////////////////////////////////////////////////////////////
@@ -592,9 +621,12 @@ pub fn fold_<T,E,Iter:Iterator<Result<T,E>>>(iterator: Iter) -> Result<(),E> {
 
 #[cfg(test)]
 mod tests {
-    use realstd::result::{collect, fold, fold_};
-    use realstd::prelude::*;
-    use realstd::iter::range;
+    use realstd::vec::Vec;
+    use realstd::str::StrAllocating;
+
+    use result::{collect, fold, fold_};
+    use prelude::*;
+    use iter::range;
 
     pub fn op1() -> Result<int, ~str> { Ok(666) }
     pub fn op2() -> Result<int, ~str> { Err("sadface".to_owned()) }
@@ -641,33 +673,37 @@ pub fn test_or_else() {
 
     #[test]
     pub fn test_impl_map() {
-        assert_eq!(Ok::<~str, ~str>("a".to_owned()).map(|x| x + "b"), Ok("ab".to_owned()));
-        assert_eq!(Err::<~str, ~str>("a".to_owned()).map(|x| x + "b"), Err("a".to_owned()));
+        assert_eq!(Ok::<~str, ~str>("a".to_owned()).map(|x| x + "b"),
+                   Ok("ab".to_owned()));
+        assert_eq!(Err::<~str, ~str>("a".to_owned()).map(|x| x + "b"),
+                   Err("a".to_owned()));
     }
 
     #[test]
     pub fn test_impl_map_err() {
-        assert_eq!(Ok::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), Ok("a".to_owned()));
-        assert_eq!(Err::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), Err("ab".to_owned()));
+        assert_eq!(Ok::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"),
+                   Ok("a".to_owned()));
+        assert_eq!(Err::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"),
+                   Err("ab".to_owned()));
     }
 
     #[test]
     fn test_collect() {
         let v: Result<Vec<int>, ()> = collect(range(0, 0).map(|_| Ok::<int, ()>(0)));
-        assert_eq!(v, Ok(vec![]));
+        assert!(v == Ok(vec![]));
 
         let v: Result<Vec<int>, ()> = collect(range(0, 3).map(|x| Ok::<int, ()>(x)));
-        assert_eq!(v, Ok(vec![0, 1, 2]));
+        assert!(v == Ok(vec![0, 1, 2]));
 
         let v: Result<Vec<int>, int> = collect(range(0, 3)
                                                .map(|x| if x > 1 { Err(x) } else { Ok(x) }));
-        assert_eq!(v, Err(2));
+        assert!(v == Err(2));
 
         // test that it does not take more elements than it needs
         let mut functions = [|| Ok(()), || Err(1), || fail!()];
 
         let v: Result<Vec<()>, int> = collect(functions.mut_iter().map(|f| (*f)()));
-        assert_eq!(v, Err(1));
+        assert!(v == Err(1));
     }
 
     #[test]
@@ -691,15 +727,6 @@ fn test_fold() {
                    Err(1));
     }
 
-    #[test]
-    pub fn test_to_str() {
-        let ok: Result<int, ~str> = Ok(100);
-        let err: Result<int, ~str> = Err("Err".to_owned());
-
-        assert_eq!(ok.to_str(), "Ok(100)".to_owned());
-        assert_eq!(err.to_str(), "Err(Err)".to_owned());
-    }
-
     #[test]
     pub fn test_fmt_default() {
         let ok: Result<int, ~str> = Ok(100);
index 9272f24da9d40f83b179efa70d72adc3304f9313..b55952e70598a85a069f8ab6c7015802bb4630a7 100644 (file)
@@ -20,7 +20,7 @@
 //      1. Implement DST
 //      2. Make `Box<T>` not a language feature
 //      3. Move `Box<T>` to a separate crate, liballoc.
-//      4. Implement relevant trais in liballoc, not libcore
+//      4. Implement relevant traits in liballoc, not libcore
 //
 // Currently, no progress has been made on this list.
 
index 1481759297868a8be3095241f6c34783f75277ae..bd4534b19ac62989622f807092df7053b2fda490 100644 (file)
@@ -542,7 +542,7 @@ pub struct UTF16Items<'a> {
     iter: slice::Items<'a, u16>
 }
 /// The possibilities for values decoded from a `u16` stream.
-#[deriving(Eq, TotalEq, Clone)]
+#[deriving(Eq, TotalEq, Clone, Show)]
 pub enum UTF16Item {
     /// A valid codepoint.
     ScalarValue(char),
index 34e29b06f76ccb2a16939a838b730dbf5c0791fe..1921eef9f60d46d4d6fa28dc2f22485265ac0e7a 100644 (file)
@@ -51,11 +51,9 @@ macro_rules! rtabort (
 )
 
 pub fn dumb_println(args: &fmt::Arguments) {
-    use std::io;
     use std::rt;
-
     let mut w = rt::Stderr;
-    let _ = fmt::writeln(&mut w as &mut io::Writer, args);
+    let _ = writeln!(&mut w, "{}", args);
 }
 
 pub fn abort(msg: &str) -> ! {
index 9dd87a38fb63af5d139884bc4537216118ba7359..5981f87b4f2b6fbe988a7ea5fbc762d75ac5fae8 100644 (file)
@@ -188,7 +188,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::Signed for LogLevel {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         let LogLevel(level) = *self;
-        write!(fmt.buf, "{}", level)
+        write!(fmt, "{}", level)
     }
 }
 
index 9f66f767f20e70da9a86f7dc0fd8fd64d238c498..ecc48d5569c0e20a3a91f6be9a7c811893d9f67b 100644 (file)
@@ -120,7 +120,7 @@ fn default() -> BigUint { BigUint::new(Vec::new()) }
 
 impl fmt::Show for BigUint {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.to_str_radix(10))
+        write!(f, "{}", self.to_str_radix(10))
     }
 }
 
@@ -843,7 +843,7 @@ fn default() -> BigInt { BigInt::new(Zero, Vec::new()) }
 
 impl fmt::Show for BigInt {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.to_str_radix(10))
+        write!(f, "{}", self.to_str_radix(10))
     }
 }
 
index b82c4d177ba02e18a4dce036930f0f77b812ec75..3bc2408188da00b66dab362f53261a076960c6ea 100644 (file)
@@ -78,7 +78,7 @@ pub fn inv(&self) -> Complex<T> {
     }
 }
 
-impl<T: Clone + Float> Complex<T> {
+impl<T: Clone + FloatMath> Complex<T> {
     /// Calculate |self|
     #[inline]
     pub fn norm(&self) -> T {
@@ -86,7 +86,7 @@ pub fn norm(&self) -> T {
     }
 }
 
-impl<T: Clone + Float> Complex<T> {
+impl<T: Clone + FloatMath> Complex<T> {
     /// Calculate the principal Arg of self.
     #[inline]
     pub fn arg(&self) -> T {
@@ -171,9 +171,9 @@ fn one() -> Complex<T> {
 impl<T: fmt::Show + Num + Ord> fmt::Show for Complex<T> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         if self.im < Zero::zero() {
-            write!(f.buf, "{}-{}i", self.re, -self.im)
+            write!(f, "{}-{}i", self.re, -self.im)
         } else {
-            write!(f.buf, "{}+{}i", self.re, self.im)
+            write!(f, "{}+{}i", self.re, self.im)
         }
     }
 }
index bffca79f351d4d0cc2fadb1ea912a2d4e8b48248..cd5c82acf6e9a17fb11f9395371123231a0b05a9 100644 (file)
@@ -276,7 +276,7 @@ impl<T: Clone + Integer + Ord>
 impl<T: fmt::Show> fmt::Show for Ratio<T> {
     /// Renders as `numer/denom`.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}/{}", self.numer, self.denom)
+        write!(f, "{}/{}", self.numer, self.denom)
     }
 }
 impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
index d1a01cc974f8b3afbe2a3d17dddebfe0cfa6b19f..a695da9fa163e6cc592c585672eaf0f605beffde 100644 (file)
@@ -37,7 +37,7 @@ pub struct Error {
 
 impl fmt::Show for Error {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "Regex syntax error near position {}: {}",
+        write!(f, "Regex syntax error near position {}: {}",
                self.pos, self.msg)
     }
 }
index f22889b22a324b56cf27a5ec561ee109a781d0c7..899c54d601bdb1a9625e23e10db35fda4595e74f 100644 (file)
@@ -117,7 +117,7 @@ pub struct Regex {
 impl fmt::Show for Regex {
     /// Shows the original regular expression.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.original)
+        write!(f, "{}", self.original)
     }
 }
 
index 49b5d7b28640c80730d9986bcfe5197f519bbd22..c885fc49de25f7009e8321d5828f92d4a8659f52 100644 (file)
@@ -15,9 +15,7 @@
 
 use std::cell::RefCell;
 use collections::HashMap;
-use std::io;
 use std::io::MemWriter;
-use std::fmt;
 
 use middle::ty::param_ty;
 use middle::ty;
@@ -28,9 +26,7 @@
 use syntax::diagnostic::SpanHandler;
 use syntax::parse::token;
 
-macro_rules! mywrite( ($wr:expr, $($arg:tt)*) => (
-    format_args!(|a| { mywrite($wr, a) }, $($arg)*)
-) )
+macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )
 
 pub struct ctxt<'a> {
     pub diag: &'a SpanHandler,
@@ -52,10 +48,6 @@ pub struct ty_abbrev {
 
 pub type abbrev_map = RefCell<HashMap<ty::t, ty_abbrev>>;
 
-fn mywrite(w: &mut MemWriter, fmt: &fmt::Arguments) {
-    fmt::write(&mut *w as &mut io::Writer, fmt);
-}
-
 pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
     match cx.abbrevs.borrow_mut().find(&t) {
         Some(a) => { w.write(a.s.as_bytes()); return; }
index 02599d7a368c165287b739fcf972cacc1ef833b4..1954c6d4123eebb92f7fa440ea72142f5a19ada5 100644 (file)
@@ -183,13 +183,13 @@ pub fn check_crate(tcx: &ty::ctxt,
 
 impl fmt::Show for LiveNode {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "ln({})", self.get())
+        write!(f, "ln({})", self.get())
     }
 }
 
 impl fmt::Show for Variable {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "v({})", self.get())
+        write!(f, "v({})", self.get())
     }
 }
 
index 2ae925caab56395c1d6028d0dd3dd45e8541fad5..517be1bde2f3a980ecb45115a643cc3d789bbb40 100644 (file)
@@ -388,7 +388,7 @@ pub struct t { inner: *t_opaque }
 
 impl fmt::Show for t {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        f.buf.write_str("*t_opaque")
+        "*t_opaque".fmt(f)
     }
 }
 
@@ -912,7 +912,7 @@ impl Vid for TyVid {
 
 impl fmt::Show for TyVid {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
-        write!(f.buf, "<generic \\#{}>", self.to_uint())
+        write!(f, "<generic \\#{}>", self.to_uint())
     }
 }
 
@@ -922,7 +922,7 @@ impl Vid for IntVid {
 
 impl fmt::Show for IntVid {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "<generic integer \\#{}>", self.to_uint())
+        write!(f, "<generic integer \\#{}>", self.to_uint())
     }
 }
 
@@ -932,7 +932,7 @@ impl Vid for FloatVid {
 
 impl fmt::Show for FloatVid {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "<generic float \\#{}>", self.to_uint())
+        write!(f, "<generic float \\#{}>", self.to_uint())
     }
 }
 
@@ -949,7 +949,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::Show for FnSig {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         // grr, without tcx not much we can do.
-        write!(f.buf, "(...)")
+        write!(f, "(...)")
     }
 }
 
@@ -1987,7 +1987,7 @@ fn sub(&self, other: &TypeContents) -> TypeContents {
 
 impl fmt::Show for TypeContents {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "TypeContents({:t})", self.bits)
+        write!(f, "TypeContents({:t})", self.bits)
     }
 }
 
index f853b0dbad40f9254f3553c958b33da962da1ed8..d80e9f0888865b2e16415461f84c98524f44cd2e 100644 (file)
@@ -103,12 +103,24 @@ fn type_is_defined_in_local_crate(tcx: &ty::ctxt, original_type: t) -> bool {
     ty::walk_ty(original_type, |t| {
         match get(t).sty {
             ty_enum(def_id, _) |
-            ty_trait(box ty::TyTrait { def_id, .. }) |
             ty_struct(def_id, _) => {
                 if def_id.krate == ast::LOCAL_CRATE {
                     found_nominal = true;
                 }
             }
+            ty_trait(box ty::TyTrait { def_id, ref store, .. }) => {
+                if def_id.krate == ast::LOCAL_CRATE {
+                    found_nominal = true;
+                }
+                if *store == ty::UniqTraitStore {
+                    match tcx.lang_items.owned_box() {
+                        Some(did) if did.krate == ast::LOCAL_CRATE => {
+                            found_nominal = true;
+                        }
+                        _ => {}
+                    }
+                }
+            }
             ty_uniq(..) => {
                 match tcx.lang_items.owned_box() {
                     Some(did) if did.krate == ast::LOCAL_CRATE => {
index f2f86485b199716b5cd3edf1229675e2e8499696..42850f8876338d002114ee3b88ef054e40022258 100644 (file)
@@ -240,9 +240,9 @@ enum VarianceTerm<'a> {
 impl<'a> fmt::Show for VarianceTerm<'a> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
-            ConstantTerm(c1) => write!(f.buf, "{}", c1),
-            TransformTerm(v1, v2) => write!(f.buf, "({} \u00D7 {})", v1, v2),
-            InferredTerm(id) => write!(f.buf, "[{}]", { let InferredIndex(i) = id; i })
+            ConstantTerm(c1) => write!(f, "{}", c1),
+            TransformTerm(v1, v2) => write!(f, "({} \u00D7 {})", v1, v2),
+            InferredTerm(id) => write!(f, "[{}]", { let InferredIndex(i) = id; i })
         }
     }
 }
index 60fcbe33a1be956e8e6671530e7d1aea5517ec76..fe93dbbc081f8b688963eb8e9461073c62057ded 100644 (file)
@@ -29,7 +29,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         for (i, ch) in s.bytes().enumerate() {
             match ch as char {
                 '<' | '>' | '&' | '\'' | '"' => {
-                    try!(fmt.buf.write(pile_o_bits.slice(last, i).as_bytes()));
+                    try!(fmt.write(pile_o_bits.slice(last, i).as_bytes()));
                     let s = match ch as char {
                         '>' => "&gt;",
                         '<' => "&lt;",
@@ -38,7 +38,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
                         '"' => "&quot;",
                         _ => unreachable!()
                     };
-                    try!(fmt.buf.write(s.as_bytes()));
+                    try!(fmt.write(s.as_bytes()));
                     last = i + 1;
                 }
                 _ => {}
@@ -46,7 +46,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         }
 
         if last < s.len() {
-            try!(fmt.buf.write(pile_o_bits.slice_from(last).as_bytes()));
+            try!(fmt.write(pile_o_bits.slice_from(last).as_bytes()));
         }
         Ok(())
     }
index 342b96ba82f50c3f2599a539616fc93ff6f6bdf1..563da5318f7c13f1e67d2cccd3340e6aa3a404b5 100644 (file)
@@ -16,7 +16,6 @@
 //! them in the future to instead emit any format desired.
 
 use std::fmt;
-use std::io;
 use std::strbuf::StrBuf;
 
 use syntax::ast;
@@ -52,46 +51,46 @@ pub fn get(&self) -> ast::FnStyle {
 impl fmt::Show for clean::Generics {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         if self.lifetimes.len() == 0 && self.type_params.len() == 0 { return Ok(()) }
-        try!(f.buf.write("&lt;".as_bytes()));
+        try!(f.write("&lt;".as_bytes()));
 
         for (i, life) in self.lifetimes.iter().enumerate() {
             if i > 0 {
-                try!(f.buf.write(", ".as_bytes()));
+                try!(f.write(", ".as_bytes()));
             }
-            try!(write!(f.buf, "{}", *life));
+            try!(write!(f, "{}", *life));
         }
 
         if self.type_params.len() > 0 {
             if self.lifetimes.len() > 0 {
-                try!(f.buf.write(", ".as_bytes()));
+                try!(f.write(", ".as_bytes()));
             }
 
             for (i, tp) in self.type_params.iter().enumerate() {
                 if i > 0 {
-                    try!(f.buf.write(", ".as_bytes()))
+                    try!(f.write(", ".as_bytes()))
                 }
-                try!(f.buf.write(tp.name.as_bytes()));
+                try!(f.write(tp.name.as_bytes()));
 
                 if tp.bounds.len() > 0 {
-                    try!(f.buf.write(": ".as_bytes()));
+                    try!(f.write(": ".as_bytes()));
                     for (i, bound) in tp.bounds.iter().enumerate() {
                         if i > 0 {
-                            try!(f.buf.write(" + ".as_bytes()));
+                            try!(f.write(" + ".as_bytes()));
                         }
-                        try!(write!(f.buf, "{}", *bound));
+                        try!(write!(f, "{}", *bound));
                     }
                 }
             }
         }
-        try!(f.buf.write("&gt;".as_bytes()));
+        try!(f.write("&gt;".as_bytes()));
         Ok(())
     }
 }
 
 impl fmt::Show for clean::Lifetime {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(f.buf.write("'".as_bytes()));
-        try!(f.buf.write(self.get_ref().as_bytes()));
+        try!(f.write("'".as_bytes()));
+        try!(f.write(self.get_ref().as_bytes()));
         Ok(())
     }
 }
@@ -100,10 +99,10 @@ impl fmt::Show for clean::TyParamBound {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             clean::RegionBound => {
-                f.buf.write("'static".as_bytes())
+                f.write("::".as_bytes())
             }
             clean::TraitBound(ref ty) => {
-                write!(f.buf, "{}", *ty)
+                write!(f, "{}", *ty)
             }
         }
     }
@@ -112,32 +111,33 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::Show for clean::Path {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         if self.global {
-            try!(f.buf.write("::".as_bytes()))
+            try!(f.write("::".as_bytes()))
         }
+
         for (i, seg) in self.segments.iter().enumerate() {
             if i > 0 {
-                try!(f.buf.write("::".as_bytes()))
+                try!(f.write("::".as_bytes()))
             }
-            try!(f.buf.write(seg.name.as_bytes()));
+            try!(f.write(seg.name.as_bytes()));
 
             if seg.lifetimes.len() > 0 || seg.types.len() > 0 {
-                try!(f.buf.write("&lt;".as_bytes()));
+                try!(f.write("&lt;".as_bytes()));
                 let mut comma = false;
                 for lifetime in seg.lifetimes.iter() {
                     if comma {
-                        try!(f.buf.write(", ".as_bytes()));
+                        try!(f.write(", ".as_bytes()));
                     }
                     comma = true;
-                    try!(write!(f.buf, "{}", *lifetime));
+                    try!(write!(f, "{}", *lifetime));
                 }
                 for ty in seg.types.iter() {
                     if comma {
-                        try!(f.buf.write(", ".as_bytes()));
+                        try!(f.write(", ".as_bytes()));
                     }
                     comma = true;
-                    try!(write!(f.buf, "{}", *ty));
+                    try!(write!(f, "{}", *ty));
                 }
-                try!(f.buf.write("&gt;".as_bytes()));
+                try!(f.write("&gt;".as_bytes()));
             }
         }
         Ok(())
@@ -146,7 +146,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 
 /// Used when rendering a `ResolvedPath` structure. This invokes the `path`
 /// rendering function with the necessary arguments for linking to a local path.
-fn resolved_path(w: &mut io::Writer, did: ast::DefId, p: &clean::Path,
+fn resolved_path(w: &mut fmt::Formatter, did: ast::DefId, p: &clean::Path,
                  print_all: bool) -> fmt::Result {
     path(w, p, print_all,
         |cache, loc| {
@@ -170,7 +170,7 @@ fn resolved_path(w: &mut io::Writer, did: ast::DefId, p: &clean::Path,
         })
 }
 
-fn path(w: &mut io::Writer, path: &clean::Path, print_all: bool,
+fn path(w: &mut fmt::Formatter, path: &clean::Path, print_all: bool,
         root: |&render::Cache, &[StrBuf]| -> Option<StrBuf>,
         info: |&render::Cache| -> Option<(Vec<StrBuf> , ItemType)>)
     -> fmt::Result
@@ -264,7 +264,7 @@ fn path(w: &mut io::Writer, path: &clean::Path, print_all: bool,
 }
 
 /// Helper to render type parameters
-fn tybounds(w: &mut io::Writer,
+fn tybounds(w: &mut fmt::Formatter,
             typarams: &Option<Vec<clean::TyParamBound> >) -> fmt::Result {
     match *typarams {
         Some(ref params) => {
@@ -286,13 +286,13 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             clean::TyParamBinder(id) | clean::Generic(id) => {
                 let m = cache_key.get().unwrap();
-                f.buf.write(m.typarams.get(&id).as_bytes())
+                f.write(m.typarams.get(&id).as_bytes())
             }
             clean::ResolvedPath{ did, ref typarams, ref path} => {
-                try!(resolved_path(f.buf, did, path, false));
-                tybounds(f.buf, typarams)
+                try!(resolved_path(f, did, path, false));
+                tybounds(f, typarams)
             }
-            clean::Self(..) => f.buf.write("Self".as_bytes()),
+            clean::Self(..) => f.write("Self".as_bytes()),
             clean::Primitive(prim) => {
                 let s = match prim {
                     ast::TyInt(ast::TyI) => "int",
@@ -312,11 +312,11 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                     ast::TyBool => "bool",
                     ast::TyChar => "char",
                 };
-                f.buf.write(s.as_bytes())
+                f.write(s.as_bytes())
             }
             clean::Closure(ref decl, ref region) => {
-                write!(f.buf, "{style}{lifetimes}|{args}|{bounds}\
-                               {arrow, select, yes{ -&gt; {ret}} other{}}",
+                write!(f, "{style}{lifetimes}|{args}|{bounds}\
+                           {arrow, select, yes{ -&gt; {ret}} other{}}",
                        style = FnStyleSpace(decl.fn_style),
                        lifetimes = if decl.lifetimes.len() == 0 {
                            "".to_owned()
@@ -351,8 +351,8 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                        })
             }
             clean::Proc(ref decl) => {
-                write!(f.buf, "{style}{lifetimes}proc({args}){bounds}\
-                               {arrow, select, yes{ -&gt; {ret}} other{}}",
+                write!(f, "{style}{lifetimes}proc({args}){bounds}\
+                           {arrow, select, yes{ -&gt; {ret}} other{}}",
                        style = FnStyleSpace(decl.fn_style),
                        lifetimes = if decl.lifetimes.len() == 0 {
                            "".to_strbuf()
@@ -374,7 +374,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                        ret = decl.decl.output)
             }
             clean::BareFunction(ref decl) => {
-                write!(f.buf, "{}{}fn{}{}",
+                write!(f, "{}{}fn{}{}",
                        FnStyleSpace(decl.fn_style),
                        match decl.abi.as_slice() {
                            "" => " extern ".to_strbuf(),
@@ -385,27 +385,27 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                        decl.decl)
             }
             clean::Tuple(ref typs) => {
-                try!(f.buf.write("(".as_bytes()));
+                try!(f.write("(".as_bytes()));
                 for (i, typ) in typs.iter().enumerate() {
                     if i > 0 {
-                        try!(f.buf.write(", ".as_bytes()))
+                        try!(f.write(", ".as_bytes()))
                     }
-                    try!(write!(f.buf, "{}", *typ));
+                    try!(write!(f, "{}", *typ));
                 }
-                f.buf.write(")".as_bytes())
+                f.write(")".as_bytes())
             }
-            clean::Vector(ref t) => write!(f.buf, "[{}]", **t),
+            clean::Vector(ref t) => write!(f, "[{}]", **t),
             clean::FixedVector(ref t, ref s) => {
-                write!(f.buf, "[{}, ..{}]", **t, *s)
-            }
-            clean::String => f.buf.write("str".as_bytes()),
-            clean::Bool => f.buf.write("bool".as_bytes()),
-            clean::Unit => f.buf.write("()".as_bytes()),
-            clean::Bottom => f.buf.write("!".as_bytes()),
-            clean::Unique(ref t) => write!(f.buf, "~{}", **t),
-            clean::Managed(ref t) => write!(f.buf, "@{}", **t),
+                write!(f, "[{}, ..{}]", **t, *s)
+            }
+            clean::String => f.write("str".as_bytes()),
+            clean::Bool => f.write("bool".as_bytes()),
+            clean::Unit => f.write("()".as_bytes()),
+            clean::Bottom => f.write("!".as_bytes()),
+            clean::Unique(ref t) => write!(f, "~{}", **t),
+            clean::Managed(ref t) => write!(f, "@{}", **t),
             clean::RawPointer(m, ref t) => {
-                write!(f.buf, "*{}{}",
+                write!(f, "*{}{}",
                        match m {
                            clean::Mutable => "mut ",
                            clean::Immutable => "",
@@ -413,7 +413,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
             }
             clean::BorrowedRef{ lifetime: ref l, mutability, type_: ref ty} => {
                 let lt = match *l { Some(ref l) => format!("{} ", *l), _ => "".to_owned() };
-                write!(f.buf, "&amp;{}{}{}",
+                write!(f, "&amp;{}{}{}",
                        lt,
                        match mutability {
                            clean::Mutable => "mut ",
@@ -428,11 +428,11 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::Show for clean::Arguments {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         for (i, input) in self.values.iter().enumerate() {
-            if i > 0 { try!(write!(f.buf, ", ")); }
+            if i > 0 { try!(write!(f, ", ")); }
             if input.name.len() > 0 {
-                try!(write!(f.buf, "{}: ", input.name));
+                try!(write!(f, "{}: ", input.name));
             }
-            try!(write!(f.buf, "{}", input.type_));
+            try!(write!(f, "{}", input.type_));
         }
         Ok(())
     }
@@ -440,7 +440,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 
 impl fmt::Show for clean::FnDecl {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "({args}){arrow, select, yes{ -&gt; {ret}} other{}}",
+        write!(f, "({args}){arrow, select, yes{ -&gt; {ret}} other{}}",
                args = self.inputs,
                arrow = match self.output { clean::Unit => "no", _ => "yes" },
                ret = self.output)
@@ -475,7 +475,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
             }
             args.push_str(format!("{}", input.type_));
         }
-        write!(f.buf,
+        write!(f,
                "({args}){arrow, select, yes{ -&gt; {ret}} other{}}",
                args = args,
                arrow = match d.output { clean::Unit => "no", _ => "yes" },
@@ -486,7 +486,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::Show for VisSpace {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self.get() {
-            Some(ast::Public) => write!(f.buf, "pub "),
+            Some(ast::Public) => write!(f, "pub "),
             Some(ast::Inherited) | None => Ok(())
         }
     }
@@ -495,7 +495,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::Show for FnStyleSpace {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self.get() {
-            ast::UnsafeFn => write!(f.buf, "unsafe "),
+            ast::UnsafeFn => write!(f, "unsafe "),
             ast::NormalFn => Ok(())
         }
     }
@@ -506,23 +506,23 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             clean::SimpleImport(ref name, ref src) => {
                 if *name == src.path.segments.last().unwrap().name {
-                    write!(f.buf, "use {};", *src)
+                    write!(f, "use {};", *src)
                 } else {
-                    write!(f.buf, "use {} = {};", *name, *src)
+                    write!(f, "use {} = {};", *name, *src)
                 }
             }
             clean::GlobImport(ref src) => {
-                write!(f.buf, "use {}::*;", *src)
+                write!(f, "use {}::*;", *src)
             }
             clean::ImportList(ref src, ref names) => {
-                try!(write!(f.buf, "use {}::\\{", *src));
+                try!(write!(f, "use {}::\\{", *src));
                 for (i, n) in names.iter().enumerate() {
                     if i > 0 {
-                        try!(write!(f.buf, ", "));
+                        try!(write!(f, ", "));
                     }
-                    try!(write!(f.buf, "{}", *n));
+                    try!(write!(f, "{}", *n));
                 }
-                write!(f.buf, "\\};")
+                write!(f, "\\};")
             }
         }
     }
@@ -531,13 +531,13 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl fmt::Show for clean::ImportSource {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self.did {
-            Some(did) => resolved_path(f.buf, did, &self.path, true),
+            Some(did) => resolved_path(f, did, &self.path, true),
             _ => {
                 for (i, seg) in self.path.segments.iter().enumerate() {
                     if i > 0 {
-                        try!(write!(f.buf, "::"))
+                        try!(write!(f, "::"))
                     }
-                    try!(write!(f.buf, "{}", seg.name));
+                    try!(write!(f, "{}", seg.name));
                 }
                 Ok(())
             }
@@ -557,9 +557,9 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                         types: Vec::new(),
                     })
                 };
-                resolved_path(f.buf, did, &path, false)
+                resolved_path(f, did, &path, false)
             }
-            _ => write!(f.buf, "{}", self.name),
+            _ => write!(f, "{}", self.name),
         }
     }
 }
index e667f7a57f14bfb7ff4092ae4adbd8074f11c037..dd465df1db7e4a3f5c1d217dff7d02d88b5308dc 100644 (file)
@@ -26,7 +26,7 @@ pub struct Page<'a> {
 
 pub fn render<T: fmt::Show, S: fmt::Show>(
     dst: &mut io::Writer, layout: &Layout, page: &Page, sidebar: &S, t: &T)
-    -> fmt::Result
+    -> io::IoResult<()>
 {
     write!(dst,
 r##"<!DOCTYPE html>
index d6831e225bc2931384cf9a663a44170146e9386b..b64e77615e1f5905f1632d5971b3bd1de453f064 100644 (file)
@@ -29,7 +29,6 @@
 use libc;
 use std::cell::RefCell;
 use std::fmt;
-use std::io;
 use std::slice;
 use std::str;
 use collections::HashMap;
@@ -141,7 +140,7 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
 
 local_data_key!(used_header_map: RefCell<HashMap<StrBuf, uint>>)
 
-pub fn render(w: &mut io::Writer, s: &str, print_toc: bool) -> fmt::Result {
+pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
     extern fn block(ob: *mut hoedown_buffer, text: *hoedown_buffer,
                     lang: *hoedown_buffer, opaque: *mut libc::c_void) {
         unsafe {
@@ -355,13 +354,13 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         let Markdown(md) = *self;
         // This is actually common enough to special-case
         if md.len() == 0 { return Ok(()) }
-        render(fmt.buf, md.as_slice(), false)
+        render(fmt, md.as_slice(), false)
     }
 }
 
 impl<'a> fmt::Show for MarkdownWithToc<'a> {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         let MarkdownWithToc(md) = *self;
-        render(fmt.buf, md.as_slice(), true)
+        render(fmt, md.as_slice(), true)
     }
 }
index 0883d25770ef75bd255c60ffca3ea2e20f5e8ef7..8ae29d7d273c2d89ca03303d7e403507e534f17a 100644 (file)
@@ -587,7 +587,7 @@ fn emit_source(&mut self, filename: &str) -> io::IoResult<()> {
             root_path: root_path.as_slice(),
         };
         try!(layout::render(&mut w as &mut Writer, &self.cx.layout,
-                              &page, &(""), &Source(contents)));
+                            &page, &(""), &Source(contents)));
         try!(w.flush());
         return Ok(());
     }
@@ -925,8 +925,8 @@ fn render(w: io::File, cx: &mut Context, it: &clean::Item,
             // write sycall all the time.
             let mut writer = BufferedWriter::new(w);
             try!(layout::render(&mut writer as &mut Writer, &cx.layout, &page,
-                                  &Sidebar{ cx: cx, item: it },
-                                  &Item{ cx: cx, item: it }));
+                                &Sidebar{ cx: cx, item: it },
+                                &Item{ cx: cx, item: it }));
             writer.flush()
         }
 
@@ -997,17 +997,17 @@ fn link(&self) -> StrBuf {
 impl<'a> fmt::Show for Item<'a> {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         // Write the breadcrumb trail header for the top
-        try!(write!(fmt.buf, "\n<h1 class='fqn'>"));
+        try!(write!(fmt, "\n<h1 class='fqn'>"));
         match self.item.inner {
             clean::ModuleItem(ref m) => if m.is_crate {
-                    try!(write!(fmt.buf, "Crate "));
+                    try!(write!(fmt, "Crate "));
                 } else {
-                    try!(write!(fmt.buf, "Module "));
+                    try!(write!(fmt, "Module "));
                 },
-            clean::FunctionItem(..) => try!(write!(fmt.buf, "Function ")),
-            clean::TraitItem(..) => try!(write!(fmt.buf, "Trait ")),
-            clean::StructItem(..) => try!(write!(fmt.buf, "Struct ")),
-            clean::EnumItem(..) => try!(write!(fmt.buf, "Enum ")),
+            clean::FunctionItem(..) => try!(write!(fmt, "Function ")),
+            clean::TraitItem(..) => try!(write!(fmt, "Trait ")),
+            clean::StructItem(..) => try!(write!(fmt, "Struct ")),
+            clean::EnumItem(..) => try!(write!(fmt, "Enum ")),
             _ => {}
         }
         let cur = self.cx.current.as_slice();
@@ -1017,16 +1017,16 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
             for _ in range(0, cur.len() - i - 1) {
                 trail.push_str("../");
             }
-            try!(write!(fmt.buf, "<a href='{}index.html'>{}</a>::",
-                          trail, component.as_slice()));
+            try!(write!(fmt, "<a href='{}index.html'>{}</a>::",
+                        trail, component.as_slice()));
         }
-        try!(write!(fmt.buf, "<a class='{}' href=''>{}</a>",
-                      shortty(self.item), self.item.name.get_ref().as_slice()));
+        try!(write!(fmt, "<a class='{}' href=''>{}</a>",
+                    shortty(self.item), self.item.name.get_ref().as_slice()));
 
         // Write stability attributes
         match attr::find_stability(self.item.attrs.iter()) {
             Some(ref stability) => {
-                try!(write!(fmt.buf,
+                try!(write!(fmt,
                        "<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
                        lvl = stability.level.to_str(),
                        reason = match stability.text {
@@ -1039,22 +1039,22 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
 
         // Write `src` tag
         if self.cx.include_sources {
-            try!(write!(fmt.buf, "<a class='source' href='{}'>[src]</a>",
+            try!(write!(fmt, "<a class='source' href='{}'>[src]</a>",
                         self.link()));
         }
-        try!(write!(fmt.buf, "</h1>\n"));
+        try!(write!(fmt, "</h1>\n"));
 
         match self.item.inner {
             clean::ModuleItem(ref m) => {
-                item_module(fmt.buf, self.cx, self.item, m.items.as_slice())
+                item_module(fmt, self.cx, self.item, m.items.as_slice())
             }
             clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) =>
-                item_function(fmt.buf, self.item, f),
-            clean::TraitItem(ref t) => item_trait(fmt.buf, self.item, t),
-            clean::StructItem(ref s) => item_struct(fmt.buf, self.item, s),
-            clean::EnumItem(ref e) => item_enum(fmt.buf, self.item, e),
-            clean::TypedefItem(ref t) => item_typedef(fmt.buf, self.item, t),
-            clean::MacroItem(ref m) => item_macro(fmt.buf, self.item, m),
+                item_function(fmt, self.item, f),
+            clean::TraitItem(ref t) => item_trait(fmt, self.item, t),
+            clean::StructItem(ref s) => item_struct(fmt, self.item, s),
+            clean::EnumItem(ref e) => item_enum(fmt, self.item, e),
+            clean::TypedefItem(ref t) => item_typedef(fmt, self.item, t),
+            clean::MacroItem(ref m) => item_macro(fmt, self.item, m),
             _ => Ok(())
         }
     }
@@ -1097,7 +1097,7 @@ fn shorter<'a>(s: Option<&'a str>) -> &'a str {
     }
 }
 
-fn document(w: &mut Writer, item: &clean::Item) -> fmt::Result {
+fn document(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::Result {
     match item.doc_value() {
         Some(s) => {
             try!(write!(w, "<div class='docblock'>{}</div>", Markdown(s)));
@@ -1107,7 +1107,7 @@ fn document(w: &mut Writer, item: &clean::Item) -> fmt::Result {
     Ok(())
 }
 
-fn item_module(w: &mut Writer, cx: &Context,
+fn item_module(w: &mut fmt::Formatter, cx: &Context,
                item: &clean::Item, items: &[clean::Item]) -> fmt::Result {
     try!(document(w, item));
     debug!("{:?}", items);
@@ -1196,13 +1196,12 @@ impl<'a> fmt::Show for Initializer<'a> {
                     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                         let Initializer(s, item) = *self;
                         if s.len() == 0 { return Ok(()); }
-                        try!(write!(f.buf, "<code> = </code>"));
+                        try!(write!(f, "<code> = </code>"));
                         if s.contains("\n") {
-                            write!(f.buf,
-                                   "<a href='{}'>[definition]</a>",
+                            write!(f, "<a href='{}'>[definition]</a>",
                                    item.link())
                         } else {
-                            write!(f.buf, "<code>{}</code>", s.as_slice())
+                            write!(f, "<code>{}</code>", s.as_slice())
                         }
                     }
                 }
@@ -1262,7 +1261,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     write!(w, "</table>")
 }
 
-fn item_function(w: &mut Writer, it: &clean::Item,
+fn item_function(w: &mut fmt::Formatter, it: &clean::Item,
                  f: &clean::Function) -> fmt::Result {
     try!(write!(w, "<pre class='rust fn'>{vis}{fn_style}fn \
                     {name}{generics}{decl}</pre>",
@@ -1274,7 +1273,7 @@ fn item_function(w: &mut Writer, it: &clean::Item,
     document(w, it)
 }
 
-fn item_trait(w: &mut Writer, it: &clean::Item,
+fn item_trait(w: &mut fmt::Formatter, it: &clean::Item,
               t: &clean::Trait) -> fmt::Result {
     let mut parents = StrBuf::new();
     if t.parents.len() > 0 {
@@ -1318,7 +1317,7 @@ fn item_trait(w: &mut Writer, it: &clean::Item,
     // Trait documentation
     try!(document(w, it));
 
-    fn meth(w: &mut Writer, m: &clean::TraitMethod) -> fmt::Result {
+    fn meth(w: &mut fmt::Formatter, m: &clean::TraitMethod) -> fmt::Result {
         try!(write!(w, "<h3 id='{}.{}' class='method'><code>",
                       shortty(m.item()),
                       *m.item().name.get_ref()));
@@ -1374,8 +1373,8 @@ fn meth(w: &mut Writer, m: &clean::TraitMethod) -> fmt::Result {
     Ok(())
 }
 
-fn render_method(w: &mut Writer, meth: &clean::Item) -> fmt::Result {
-    fn fun(w: &mut Writer, it: &clean::Item, fn_style: ast::FnStyle,
+fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
+    fn fun(w: &mut fmt::Formatter, it: &clean::Item, fn_style: ast::FnStyle,
            g: &clean::Generics, selfty: &clean::SelfTy,
            d: &clean::FnDecl) -> fmt::Result {
         write!(w, "{}fn <a href='\\#{ty}.{name}' class='fnname'>{name}</a>\
@@ -1400,7 +1399,7 @@ fn fun(w: &mut Writer, it: &clean::Item, fn_style: ast::FnStyle,
     }
 }
 
-fn item_struct(w: &mut Writer, it: &clean::Item,
+fn item_struct(w: &mut fmt::Formatter, it: &clean::Item,
                s: &clean::Struct) -> fmt::Result {
     try!(write!(w, "<pre class='rust struct'>"));
     try!(render_struct(w,
@@ -1437,7 +1436,8 @@ fn item_struct(w: &mut Writer, it: &clean::Item,
     render_methods(w, it)
 }
 
-fn item_enum(w: &mut Writer, it: &clean::Item, e: &clean::Enum) -> fmt::Result {
+fn item_enum(w: &mut fmt::Formatter, it: &clean::Item,
+             e: &clean::Enum) -> fmt::Result {
     try!(write!(w, "<pre class='rust enum'>{}enum {}{}",
                   VisSpace(it.visibility),
                   it.name.get_ref().as_slice(),
@@ -1533,7 +1533,7 @@ fn item_enum(w: &mut Writer, it: &clean::Item, e: &clean::Enum) -> fmt::Result {
     Ok(())
 }
 
-fn render_struct(w: &mut Writer, it: &clean::Item,
+fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
                  g: Option<&clean::Generics>,
                  ty: doctree::StructType,
                  fields: &[clean::Item],
@@ -1597,7 +1597,7 @@ fn render_struct(w: &mut Writer, it: &clean::Item,
     Ok(())
 }
 
-fn render_methods(w: &mut Writer, it: &clean::Item) -> fmt::Result {
+fn render_methods(w: &mut fmt::Formatter, it: &clean::Item) -> fmt::Result {
     match cache_key.get().unwrap().impls.find(&it.id) {
         Some(v) => {
             let mut non_trait = v.iter().filter(|p| {
@@ -1642,7 +1642,7 @@ fn render_methods(w: &mut Writer, it: &clean::Item) -> fmt::Result {
     Ok(())
 }
 
-fn render_impl(w: &mut Writer, i: &clean::Impl,
+fn render_impl(w: &mut fmt::Formatter, i: &clean::Impl,
                dox: &Option<StrBuf>) -> fmt::Result {
     try!(write!(w, "<h3 class='impl'><code>impl{} ", i.generics));
     let trait_id = match i.trait_ {
@@ -1664,8 +1664,8 @@ fn render_impl(w: &mut Writer, i: &clean::Impl,
         None => {}
     }
 
-    fn docmeth(w: &mut Writer, item: &clean::Item,
-               dox: bool) -> io::IoResult<()> {
+    fn docmeth(w: &mut fmt::Formatter, item: &clean::Item,
+               dox: bool) -> fmt::Result {
         try!(write!(w, "<h4 id='method.{}' class='method'><code>",
                       *item.name.get_ref()));
         try!(render_method(w, item));
@@ -1714,7 +1714,7 @@ fn docmeth(w: &mut Writer, item: &clean::Item,
     Ok(())
 }
 
-fn item_typedef(w: &mut Writer, it: &clean::Item,
+fn item_typedef(w: &mut fmt::Formatter, it: &clean::Item,
                 t: &clean::Typedef) -> fmt::Result {
     try!(write!(w, "<pre class='rust typedef'>type {}{} = {};</pre>",
                   it.name.get_ref().as_slice(),
@@ -1728,21 +1728,21 @@ impl<'a> fmt::Show for Sidebar<'a> {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         let cx = self.cx;
         let it = self.item;
-        try!(write!(fmt.buf, "<p class='location'>"));
+        try!(write!(fmt, "<p class='location'>"));
         let len = cx.current.len() - if it.is_mod() {1} else {0};
         for (i, name) in cx.current.iter().take(len).enumerate() {
             if i > 0 {
-                try!(write!(fmt.buf, "&\\#8203;::"));
+                try!(write!(fmt, "&\\#8203;::"));
             }
-            try!(write!(fmt.buf, "<a href='{}index.html'>{}</a>",
+            try!(write!(fmt, "<a href='{}index.html'>{}</a>",
                           cx.root_path
                             .as_slice()
                             .slice_to((cx.current.len() - i - 1) * 3),
                           *name));
         }
-        try!(write!(fmt.buf, "</p>"));
+        try!(write!(fmt, "</p>"));
 
-        fn block(w: &mut Writer, short: &str, longty: &str,
+        fn block(w: &mut fmt::Formatter, short: &str, longty: &str,
                  cur: &clean::Item, cx: &Context) -> fmt::Result {
             let items = match cx.sidebar.find_equiv(&short) {
                 Some(items) => items.as_slice(),
@@ -1770,12 +1770,12 @@ fn block(w: &mut Writer, short: &str, longty: &str,
             Ok(())
         }
 
-        try!(block(fmt.buf, "mod", "Modules", it, cx));
-        try!(block(fmt.buf, "struct", "Structs", it, cx));
-        try!(block(fmt.buf, "enum", "Enums", it, cx));
-        try!(block(fmt.buf, "trait", "Traits", it, cx));
-        try!(block(fmt.buf, "fn", "Functions", it, cx));
-        try!(block(fmt.buf, "macro", "Macros", it, cx));
+        try!(block(fmt, "mod", "Modules", it, cx));
+        try!(block(fmt, "struct", "Structs", it, cx));
+        try!(block(fmt, "enum", "Enums", it, cx));
+        try!(block(fmt, "trait", "Traits", it, cx));
+        try!(block(fmt, "fn", "Functions", it, cx));
+        try!(block(fmt, "macro", "Macros", it, cx));
         Ok(())
     }
 }
@@ -1808,19 +1808,18 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
             cols += 1;
             tmp /= 10;
         }
-        try!(write!(fmt.buf, "<pre class='line-numbers'>"));
+        try!(write!(fmt, "<pre class='line-numbers'>"));
         for i in range(1, lines + 1) {
-            try!(write!(fmt.buf, "<span id='{0:u}'>{0:1$u}</span>\n", i, cols));
+            try!(write!(fmt, "<span id='{0:u}'>{0:1$u}</span>\n", i, cols));
         }
-        try!(write!(fmt.buf, "</pre>"));
-        try!(write!(fmt.buf, "{}", highlight::highlight(s.as_slice(), None)));
+        try!(write!(fmt, "</pre>"));
+        try!(write!(fmt, "{}", highlight::highlight(s.as_slice(), None)));
         Ok(())
     }
 }
 
-fn item_macro(w: &mut Writer, it: &clean::Item,
+fn item_macro(w: &mut fmt::Formatter, it: &clean::Item,
               t: &clean::Macro) -> fmt::Result {
-    try!(w.write_str(highlight::highlight(t.source.as_slice(),
-                                          Some("macro")).as_slice()));
+    try!(w.write(highlight::highlight(t.source.as_slice(), Some("macro")).as_bytes()));
     document(w, it)
 }
index 893214dc9c9c7d433b963c95f601e8bd0cf19a17..4dabdf64f8102da691d785e5ead46326bacb6165 100644 (file)
@@ -174,17 +174,17 @@ pub fn push<'a>(&'a mut self, level: u32, name: StrBuf, id: StrBuf) -> &'a str {
 
 impl fmt::Show for Toc {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(fmt.buf, "<ul>"));
+        try!(write!(fmt, "<ul>"));
         for entry in self.entries.iter() {
             // recursively format this table of contents (the
             // `{children}` is the key).
-            try!(write!(fmt.buf,
+            try!(write!(fmt,
                         "\n<li><a href=\"\\#{id}\">{num} {name}</a>{children}</li>",
                         id = entry.id,
                         num = entry.sec_number, name = entry.name,
                         children = entry.children))
         }
-        write!(fmt.buf, "</ul>")
+        write!(fmt, "</ul>")
     }
 }
 
index 53515ec58e580c53b0d5fc96c1a0fc436fc59607..141e3e515ac96ab9f759f8ffd5597205a6c8c366 100644 (file)
@@ -379,7 +379,7 @@ pub fn is_eof(&self) -> bool {
 
 impl fmt::Show for UvError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}: {}", self.name(), self.desc())
+        write!(f, "{}: {}", self.name(), self.desc())
     }
 }
 
index 8e827703cb2b8985de8a3b685ea6915dc86227cf..deb7036848f96413bd1c26c7038f8eebda416f9e 100644 (file)
@@ -28,9 +28,7 @@ macro_rules! uvdebug (
 )
 
 pub fn dumb_println(args: &fmt::Arguments) {
-    use std::io;
     use std::rt;
-
     let mut w = rt::Stderr;
-    let _ = fmt::writeln(&mut w as &mut io::Writer, args);
+    let _ = writeln!(&mut w, "{}", args);
 }
index 3035b305617879d6fa62300c1ad07be5417e9066..2f1d59b23a280b8326a68eee0ecb245d8668ed00 100644 (file)
@@ -96,18 +96,18 @@ pub struct Version {
 impl fmt::Show for Version {
     #[inline]
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, "{}.{}.{}", self.major, self.minor, self.patch))
+        try!(write!(f, "{}.{}.{}", self.major, self.minor, self.patch))
         if !self.pre.is_empty() {
-            try!(write!(f.buf, "-"));
+            try!(write!(f, "-"));
             for (i, x) in self.pre.iter().enumerate() {
-                if i != 0 { try!(write!(f.buf, ".")) };
+                if i != 0 { try!(write!(f, ".")) };
                 try!(x.fmt(f));
             }
         }
         if !self.build.is_empty() {
-            try!(write!(f.buf, "+"));
+            try!(write!(f, "+"));
             for (i, x) in self.build.iter().enumerate() {
-                if i != 0 { try!(write!(f.buf, ".")) };
+                if i != 0 { try!(write!(f, ".")) };
                 try!(x.fmt(f));
             }
         }
index 5702557526fa82f805328b79f9ea2f9a92992e0a..5ed778b49ebdd99367defc3a32f1b54b99d271db 100644 (file)
@@ -170,8 +170,8 @@ impl fmt::Show for FromBase64Error {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             InvalidBase64Character(ch, idx) =>
-                write!(f.buf, "Invalid character '{}' at position {}", ch, idx),
-            InvalidBase64Length => write!(f.buf, "Invalid length"),
+                write!(f, "Invalid character '{}' at position {}", ch, idx),
+            InvalidBase64Length => write!(f, "Invalid length"),
         }
     }
 }
index 2b9ba763b2ebb45c2b29afecebfa32da2d359730..623bf85424a289faec8ac88fa3c1b943f114bfcb 100644 (file)
@@ -69,8 +69,8 @@ impl fmt::Show for FromHexError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             InvalidHexCharacter(ch, idx) =>
-                write!(f.buf, "Invalid character '{}' at position {}", ch, idx),
-            InvalidHexLength => write!(f.buf, "Invalid input length"),
+                write!(f, "Invalid character '{}' at position {}", ch, idx),
+            InvalidHexLength => write!(f, "Invalid input length"),
         }
     }
 }
index bf04f10fcf0d337a6b6d0c58b7c88ead3a0b100c..17a864d286232b12ee1b7281babfea4bb57d757c 100644 (file)
@@ -2267,7 +2267,7 @@ fn to_json(&self) -> Json {
 impl fmt::Show for Json {
     /// Encodes a json value into a string
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        self.to_writer(f.buf)
+        self.to_writer(f).map_err(|_| fmt::WriteError)
     }
 }
 
index 163ccd22552d36fed65495580b6c4a5c1b7798a0..6b3939872811d4874af371baa2b120aeaf221201 100644 (file)
@@ -59,7 +59,7 @@
 //!
 //! impl fmt::Show for Flags {
 //!     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-//!         write!(f.buf, "hi!")
+//!         write!(f, "hi!")
 //!     }
 //! }
 //!
diff --git a/src/libstd/fmt.rs b/src/libstd/fmt.rs
new file mode 100644 (file)
index 0000000..86b77a4
--- /dev/null
@@ -0,0 +1,588 @@
+// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/*!
+
+Utilities for formatting and printing strings
+
+This module contains the runtime support for the `format!` syntax extension.
+This macro is implemented in the compiler to emit calls to this module in order
+to format arguments at runtime into strings and streams.
+
+The functions contained in this module should not normally be used in everyday
+use cases of `format!`. The assumptions made by these functions are unsafe for
+all inputs, and the compiler performs a large amount of validation on the
+arguments to `format!` in order to ensure safety at runtime. While it is
+possible to call these functions directly, it is not recommended to do so in the
+general case.
+
+## Usage
+
+The `format!` macro is intended to be familiar to those coming from C's
+printf/fprintf functions or Python's `str.format` function. In its current
+revision, the `format!` macro returns a `~str` type which is the result of the
+formatting. In the future it will also be able to pass in a stream to format
+arguments directly while performing minimal allocations.
+
+Some examples of the `format!` extension are:
+
+```rust
+format!("Hello");                 // => "Hello".to_owned()
+format!("Hello, {:s}!", "world"); // => "Hello, world!".to_owned()
+format!("The number is {:d}", 1); // => "The number is 1".to_owned()
+format!("{:?}", ~[3, 4]);         // => "~[3, 4]".to_owned()
+format!("{value}", value=4);      // => "4".to_owned()
+format!("{} {}", 1, 2);           // => "1 2".to_owned()
+```
+
+From these, you can see that the first argument is a format string. It is
+required by the compiler for this to be a string literal; it cannot be a
+variable passed in (in order to perform validity checking). The compiler will
+then parse the format string and determine if the list of arguments provided is
+suitable to pass to this format string.
+
+### Positional parameters
+
+Each formatting argument is allowed to specify which value argument it's
+referencing, and if omitted it is assumed to be "the next argument". For
+example, the format string `{} {} {}` would take three parameters, and they
+would be formatted in the same order as they're given. The format string
+`{2} {1} {0}`, however, would format arguments in reverse order.
+
+Things can get a little tricky once you start intermingling the two types of
+positional specifiers. The "next argument" specifier can be thought of as an
+iterator over the argument. Each time a "next argument" specifier is seen, the
+iterator advances. This leads to behavior like this:
+
+```rust
+format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2".to_owned()
+```
+
+The internal iterator over the argument has not been advanced by the time the
+first `{}` is seen, so it prints the first argument. Then upon reaching the
+second `{}`, the iterator has advanced forward to the second argument.
+Essentially, parameters which explicitly name their argument do not affect
+parameters which do not name an argument in terms of positional specifiers.
+
+A format string is required to use all of its arguments, otherwise it is a
+compile-time error. You may refer to the same argument more than once in the
+format string, although it must always be referred to with the same type.
+
+### Named parameters
+
+Rust itself does not have a Python-like equivalent of named parameters to a
+function, but the `format!` macro is a syntax extension which allows it to
+leverage named parameters. Named parameters are listed at the end of the
+argument list and have the syntax:
+
+```notrust
+identifier '=' expression
+```
+
+For example, the following `format!` expressions all use named argument:
+
+```rust
+format!("{argument}", argument = "test");       // => "test".to_owned()
+format!("{name} {}", 1, name = 2);              // => "2 1".to_owned()
+format!("{a:s} {c:d} {b:?}", a="a", b=(), c=3); // => "a 3 ()".to_owned()
+```
+
+It is illegal to put positional parameters (those without names) after arguments
+which have names. Like positional parameters, it is illegal to provided named
+parameters that are unused by the format string.
+
+### Argument types
+
+Each argument's type is dictated by the format string. It is a requirement that
+every argument is only ever referred to by one type. When specifying the format
+of an argument, however, a string like `{}` indicates no type. This is allowed,
+and if all references to one argument do not provide a type, then the format `?`
+is used (the type's rust-representation is printed). For example, this is an
+invalid format string:
+
+```notrust
+{0:d} {0:s}
+```
+
+Because the first argument is both referred to as an integer as well as a
+string.
+
+Because formatting is done via traits, there is no requirement that the
+`d` format actually takes an `int`, but rather it simply requires a type which
+ascribes to the `Signed` formatting trait. There are various parameters which do
+require a particular type, however. Namely if the syntax `{:.*s}` is used, then
+the number of characters to print from the string precedes the actual string and
+must have the type `uint`. Although a `uint` can be printed with `{:u}`, it is
+illegal to reference an argument as such. For example, this is another invalid
+format string:
+
+```notrust
+{:.*s} {0:u}
+```
+
+### Formatting traits
+
+When requesting that an argument be formatted with a particular type, you are
+actually requesting that an argument ascribes to a particular trait. This allows
+multiple actual types to be formatted via `{:d}` (like `i8` as well as `int`).
+The current mapping of types to traits is:
+
+* `?` â‡’ `Poly`
+* `d` â‡’ `Signed`
+* `i` â‡’ `Signed`
+* `u` â‡’ `Unsigned`
+* `b` â‡’ `Bool`
+* `c` â‡’ `Char`
+* `o` â‡’ `Octal`
+* `x` â‡’ `LowerHex`
+* `X` â‡’ `UpperHex`
+* `s` â‡’ `String`
+* `p` â‡’ `Pointer`
+* `t` â‡’ `Binary`
+* `f` â‡’ `Float`
+* `e` â‡’ `LowerExp`
+* `E` â‡’ `UpperExp`
+* *nothing* â‡’ `Show`
+
+What this means is that any type of argument which implements the
+`std::fmt::Binary` trait can then be formatted with `{:t}`. Implementations are
+provided for these traits for a number of primitive types by the standard
+library as well. If no format is specified (as in `{}` or `{:6}`), then the
+format trait used is the `Show` trait. This is one of the more commonly
+implemented traits when formatting a custom type.
+
+When implementing a format trait for your own type, you will have to implement a
+method of the signature:
+
+```rust
+# use std;
+# mod fmt { pub type Result = (); }
+# struct T;
+# trait SomeName<T> {
+fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result;
+# }
+```
+
+Your type will be passed as `self` by-reference, and then the function should
+emit output into the `f.buf` stream. It is up to each format trait
+implementation to correctly adhere to the requested formatting parameters. The
+values of these parameters will be listed in the fields of the `Formatter`
+struct. In order to help with this, the `Formatter` struct also provides some
+helper methods.
+
+Additionally, the return value of this function is `fmt::Result` which is a
+typedef to `Result<(), IoError>` (also known as `IoError<()>`). Formatting
+implementations should ensure that they return errors from `write!` correctly
+(propagating errors upward).
+
+An example of implementing the formatting traits would look
+like:
+
+```rust
+use std::fmt;
+use std::f64;
+
+struct Vector2D {
+    x: int,
+    y: int,
+}
+
+impl fmt::Show for Vector2D {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        // The `f` value implements the `Writer` trait, which is what the
+        // write! macro is expecting. Note that this formatting ignores the
+        // various flags provided to format strings.
+        write!(f, "({}, {})", self.x, self.y)
+    }
+}
+
+// Different traits allow different forms of output of a type. The meaning of
+// this format is to print the magnitude of a vector.
+impl fmt::Binary for Vector2D {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        let magnitude = (self.x * self.x + self.y * self.y) as f64;
+        let magnitude = magnitude.sqrt();
+
+        // Respect the formatting flags by using the helper method
+        // `pad_integral` on the Formatter object. See the method documentation
+        // for details, and the function `pad` can be used to pad strings.
+        let decimals = f.precision.unwrap_or(3);
+        let string = f64::to_str_exact(magnitude, decimals);
+        f.pad_integral(true, "", string.as_bytes())
+    }
+}
+
+fn main() {
+    let myvector = Vector2D { x: 3, y: 4 };
+
+    println!("{}", myvector);       // => "(3, 4)"
+    println!("{:10.3t}", myvector); // => "     5.000"
+}
+```
+
+### Related macros
+
+There are a number of related macros in the `format!` family. The ones that are
+currently implemented are:
+
+```ignore
+format!      // described above
+write!       // first argument is a &mut io::Writer, the destination
+writeln!     // same as write but appends a newline
+print!       // the format string is printed to the standard output
+println!     // same as print but appends a newline
+format_args! // described below.
+```
+
+
+#### `write!`
+
+This and `writeln` are two macros which are used to emit the format string to a
+specified stream. This is used to prevent intermediate allocations of format
+strings and instead directly write the output. Under the hood, this function is
+actually invoking the `write` function defined in this module. Example usage is:
+
+```rust
+# #![allow(unused_must_use)]
+use std::io;
+
+let mut w = io::MemWriter::new();
+write!(&mut w as &mut io::Writer, "Hello {}!", "world");
+```
+
+#### `print!`
+
+This and `println` emit their output to stdout. Similarly to the `write!` macro,
+the goal of these macros is to avoid intermediate allocations when printing
+output. Example usage is:
+
+```rust
+print!("Hello {}!", "world");
+println!("I have a newline {}", "character at the end");
+```
+
+#### `format_args!`
+This is a curious macro which is used to safely pass around
+an opaque object describing the format string. This object
+does not require any heap allocations to create, and it only
+references information on the stack. Under the hood, all of
+the related macros are implemented in terms of this. First
+off, some example usage is:
+
+```
+use std::fmt;
+use std::io;
+
+# #[allow(unused_must_use)]
+# fn main() {
+format_args!(fmt::format, "this returns {}", "~str");
+
+let some_writer: &mut io::Writer = &mut io::stdout();
+format_args!(|args| { write!(some_writer, "{}", args) }, "print with a {}", "closure");
+
+fn my_fmt_fn(args: &fmt::Arguments) {
+    write!(&mut io::stdout(), "{}", args);
+}
+format_args!(my_fmt_fn, "or a {} too", "function");
+# }
+```
+
+The first argument of the `format_args!` macro is a function (or closure) which
+takes one argument of type `&fmt::Arguments`. This structure can then be
+passed to the `write` and `format` functions inside this module in order to
+process the format string. The goal of this macro is to even further prevent
+intermediate allocations when dealing formatting strings.
+
+For example, a logging library could use the standard formatting syntax, but it
+would internally pass around this structure until it has been determined where
+output should go to.
+
+It is unsafe to programmatically create an instance of `fmt::Arguments` because
+the operations performed when executing a format string require the compile-time
+checks provided by the compiler. The `format_args!` macro is the only method of
+safely creating these structures, but they can be unsafely created with the
+constructor provided.
+
+## Internationalization
+
+The formatting syntax supported by the `format!` extension supports
+internationalization by providing "methods" which execute various different
+outputs depending on the input. The syntax and methods provided are similar to
+other internationalization systems, so again nothing should seem alien.
+Currently two methods are supported by this extension: "select" and "plural".
+
+Each method will execute one of a number of clauses, and then the value of the
+clause will become what's the result of the argument's format. Inside of the
+cases, nested argument strings may be provided, but all formatting arguments
+must not be done through implicit positional means. All arguments inside of each
+case of a method must be explicitly selected by their name or their integer
+position.
+
+Furthermore, whenever a case is running, the special character `#` can be used
+to reference the string value of the argument which was selected upon. As an
+example:
+
+```rust
+format!("{0, select, other{#}}", "hello"); // => "hello".to_owned()
+```
+
+This example is the equivalent of `{0:s}` essentially.
+
+### Select
+
+The select method is a switch over a `&str` parameter, and the parameter *must*
+be of the type `&str`. An example of the syntax is:
+
+```notrust
+{0, select, male{...} female{...} other{...}}
+```
+
+Breaking this down, the `0`-th argument is selected upon with the `select`
+method, and then a number of cases follow. Each case is preceded by an
+identifier which is the match-clause to execute the given arm. In this case,
+there are two explicit cases, `male` and `female`. The case will be executed if
+the string argument provided is an exact match to the case selected.
+
+The `other` case is also a required case for all `select` methods. This arm will
+be executed if none of the other arms matched the word being selected over.
+
+### Plural
+
+The plural method is a switch statement over a `uint` parameter, and the
+parameter *must* be a `uint`. A plural method in its full glory can be specified
+as:
+
+```notrust
+{0, plural, offset=1 =1{...} two{...} many{...} other{...}}
+```
+
+To break this down, the first `0` indicates that this method is selecting over
+the value of the first positional parameter to the format string. Next, the
+`plural` method is being executed. An optionally-supplied `offset` is then given
+which indicates a number to subtract from argument `0` when matching. This is
+then followed by a list of cases.
+
+Each case is allowed to supply a specific value to match upon with the syntax
+`=N`. This case is executed if the value at argument `0` matches N exactly,
+without taking the offset into account. A case may also be specified by one of
+five keywords: `zero`, `one`, `two`, `few`, and `many`. These cases are matched
+on after argument `0` has the offset taken into account. Currently the
+definitions of `many` and `few` are hardcoded, but they are in theory defined by
+the current locale.
+
+Finally, all `plural` methods must have an `other` case supplied which will be
+executed if none of the other cases match.
+
+## Syntax
+
+The syntax for the formatting language used is drawn from other languages, so it
+should not be too alien. Arguments are formatted with python-like syntax,
+meaning that arguments are surrounded by `{}` instead of the C-like `%`. The
+actual grammar for the formatting syntax is:
+
+```notrust
+format_string := <text> [ format <text> ] *
+format := '{' [ argument ] [ ':' format_spec ] [ ',' function_spec ] '}'
+argument := integer | identifier
+
+format_spec := [[fill]align][sign]['#'][0][width]['.' precision][type]
+fill := character
+align := '<' | '>'
+sign := '+' | '-'
+width := count
+precision := count | '*'
+type := identifier | ''
+count := parameter | integer
+parameter := integer '$'
+
+function_spec := plural | select
+select := 'select' ',' ( identifier arm ) *
+plural := 'plural' ',' [ 'offset:' integer ] ( selector arm ) *
+selector := '=' integer | keyword
+keyword := 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'
+arm := '{' format_string '}'
+```
+
+## Formatting Parameters
+
+Each argument being formatted can be transformed by a number of formatting
+parameters (corresponding to `format_spec` in the syntax above). These
+parameters affect the string representation of what's being formatted. This
+syntax draws heavily from Python's, so it may seem a bit familiar.
+
+### Fill/Alignment
+
+The fill character is provided normally in conjunction with the `width`
+parameter. This indicates that if the value being formatted is smaller than
+`width` some extra characters will be printed around it. The extra characters
+are specified by `fill`, and the alignment can be one of two options:
+
+* `<` - the argument is left-aligned in `width` columns
+* `>` - the argument is right-aligned in `width` columns
+
+### Sign/#/0
+
+These can all be interpreted as flags for a particular formatter.
+
+* '+' - This is intended for numeric types and indicates that the sign should
+        always be printed. Positive signs are never printed by default, and the
+        negative sign is only printed by default for the `Signed` trait. This
+        flag indicates that the correct sign (+ or -) should always be printed.
+* '-' - Currently not used
+* '#' - This flag is indicates that the "alternate" form of printing should be
+        used. By default, this only applies to the integer formatting traits and
+        performs like:
+    * `x` - precedes the argument with a "0x"
+    * `X` - precedes the argument with a "0x"
+    * `t` - precedes the argument with a "0b"
+    * `o` - precedes the argument with a "0o"
+* '0' - This is used to indicate for integer formats that the padding should
+        both be done with a `0` character as well as be sign-aware. A format
+        like `{:08d}` would yield `00000001` for the integer `1`, while the same
+        format would yield `-0000001` for the integer `-1`. Notice that the
+        negative version has one fewer zero than the positive version.
+
+### Width
+
+This is a parameter for the "minimum width" that the format should take up. If
+the value's string does not fill up this many characters, then the padding
+specified by fill/alignment will be used to take up the required space.
+
+The default fill/alignment for non-numerics is a space and left-aligned. The
+defaults for numeric formatters is also a space but with right-alignment. If the
+'0' flag is specified for numerics, then the implicit fill character is '0'.
+
+The value for the width can also be provided as a `uint` in the list of
+parameters by using the `2$` syntax indicating that the second argument is a
+`uint` specifying the width.
+
+### Precision
+
+For non-numeric types, this can be considered a "maximum width". If the
+resulting string is longer than this width, then it is truncated down to this
+many characters and only those are emitted.
+
+For integral types, this has no meaning currently.
+
+For floating-point types, this indicates how many digits after the decimal point
+should be printed.
+
+## Escaping
+
+The literal characters `{`, `}`, or `#` may be included in a string by
+preceding them with the `\` character. Since `\` is already an
+escape character in Rust strings, a string literal using this escape
+will look like `"\\{"`.
+
+*/
+
+use io::Writer;
+use io;
+use option::None;
+use repr;
+use result::{Ok, Err};
+use str::{StrAllocating};
+use str;
+use strbuf::StrBuf;
+use slice::Vector;
+
+pub use core::fmt::{Formatter, Result, FormatWriter, Show, rt};
+pub use core::fmt::{Show, Bool, Char, Signed, Unsigned, Octal, Binary};
+pub use core::fmt::{LowerHex, UpperHex, String, Pointer};
+pub use core::fmt::{Float, LowerExp, UpperExp};
+pub use core::fmt::{FormatError, WriteError};
+pub use core::fmt::{Argument, Arguments, write, radix, Radix, RadixFmt};
+
+#[doc(hidden)]
+pub use core::fmt::{argument, argumentstr, argumentuint};
+#[doc(hidden)]
+pub use core::fmt::{secret_show, secret_string, secret_unsigned};
+#[doc(hidden)]
+pub use core::fmt::{secret_signed, secret_lower_hex, secret_upper_hex};
+#[doc(hidden)]
+pub use core::fmt::{secret_bool, secret_char, secret_octal, secret_binary};
+#[doc(hidden)]
+pub use core::fmt::{secret_bool, secret_char, secret_octal, secret_binary};
+#[doc(hidden)]
+pub use core::fmt::{secret_float, secret_upper_exp, secret_lower_exp};
+#[doc(hidden)]
+pub use core::fmt::{secret_pointer};
+
+#[doc(hidden)]
+pub fn secret_poly<T: Poly>(x: &T, fmt: &mut Formatter) -> Result {
+    // FIXME #11938 - UFCS would make us able call the this method
+    //                directly Poly::fmt(x, fmt).
+    x.fmt(fmt)
+}
+
+/// Format trait for the `?` character
+pub trait Poly {
+    /// Formats the value using the given formatter.
+    fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// The format function takes a precompiled format string and a list of
+/// arguments, to return the resulting formatted string.
+///
+/// # Arguments
+///
+///   * args - a structure of arguments generated via the `format_args!` macro.
+///            Because this structure can only be safely generated at
+///            compile-time, this function is safe.
+///
+/// # Example
+///
+/// ```rust
+/// use std::fmt;
+///
+/// let s = format_args!(fmt::format, "Hello, {}!", "world");
+/// assert_eq!(s, "Hello, world!".to_owned());
+/// ```
+pub fn format(args: &Arguments) -> ~str {
+    let mut output = io::MemWriter::new();
+    let _ = write!(&mut output, "{}", args);
+    str::from_utf8(output.unwrap().as_slice()).unwrap().to_owned()
+}
+
+/// Temporary transition utility
+pub fn format_strbuf(args: &Arguments) -> StrBuf {
+    let mut output = io::MemWriter::new();
+    let _ = write!(&mut output, "{}", args);
+    str::from_utf8(output.unwrap().as_slice()).unwrap().into_strbuf()
+}
+
+impl<T> Poly for T {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        match (f.width, f.precision) {
+            (None, None) => {
+                match repr::write_repr(f, self) {
+                    Ok(()) => Ok(()),
+                    Err(..) => Err(WriteError),
+                }
+            }
+
+            // If we have a specified width for formatting, then we have to make
+            // this allocation of a new string
+            _ => {
+                let s = repr::repr_to_str(self);
+                f.pad(s)
+            }
+        }
+    }
+}
+
+impl<'a> Writer for Formatter<'a> {
+    fn write(&mut self, b: &[u8]) -> io::IoResult<()> {
+        match (*self).write(b) {
+            Ok(()) => Ok(()),
+            Err(WriteError) => Err(io::standard_error(io::OtherIoError))
+        }
+    }
+}
diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs
deleted file mode 100644 (file)
index d4f12f5..0000000
+++ /dev/null
@@ -1,1405 +0,0 @@
-// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-/*!
-
-Utilities for formatting and printing strings
-
-This module contains the runtime support for the `format!` syntax extension.
-This macro is implemented in the compiler to emit calls to this module in order
-to format arguments at runtime into strings and streams.
-
-The functions contained in this module should not normally be used in everyday
-use cases of `format!`. The assumptions made by these functions are unsafe for
-all inputs, and the compiler performs a large amount of validation on the
-arguments to `format!` in order to ensure safety at runtime. While it is
-possible to call these functions directly, it is not recommended to do so in the
-general case.
-
-## Usage
-
-The `format!` macro is intended to be familiar to those coming from C's
-printf/fprintf functions or Python's `str.format` function. In its current
-revision, the `format!` macro returns a `~str` type which is the result of the
-formatting. In the future it will also be able to pass in a stream to format
-arguments directly while performing minimal allocations.
-
-Some examples of the `format!` extension are:
-
-```rust
-format!("Hello");                 // => "Hello".to_owned()
-format!("Hello, {:s}!", "world"); // => "Hello, world!".to_owned()
-format!("The number is {:d}", 1); // => "The number is 1".to_owned()
-format!("{:?}", ~[3, 4]);         // => "~[3, 4]".to_owned()
-format!("{value}", value=4);      // => "4".to_owned()
-format!("{} {}", 1, 2);           // => "1 2".to_owned()
-```
-
-From these, you can see that the first argument is a format string. It is
-required by the compiler for this to be a string literal; it cannot be a
-variable passed in (in order to perform validity checking). The compiler will
-then parse the format string and determine if the list of arguments provided is
-suitable to pass to this format string.
-
-### Positional parameters
-
-Each formatting argument is allowed to specify which value argument it's
-referencing, and if omitted it is assumed to be "the next argument". For
-example, the format string `{} {} {}` would take three parameters, and they
-would be formatted in the same order as they're given. The format string
-`{2} {1} {0}`, however, would format arguments in reverse order.
-
-Things can get a little tricky once you start intermingling the two types of
-positional specifiers. The "next argument" specifier can be thought of as an
-iterator over the argument. Each time a "next argument" specifier is seen, the
-iterator advances. This leads to behavior like this:
-
-```rust
-format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2".to_owned()
-```
-
-The internal iterator over the argument has not been advanced by the time the
-first `{}` is seen, so it prints the first argument. Then upon reaching the
-second `{}`, the iterator has advanced forward to the second argument.
-Essentially, parameters which explicitly name their argument do not affect
-parameters which do not name an argument in terms of positional specifiers.
-
-A format string is required to use all of its arguments, otherwise it is a
-compile-time error. You may refer to the same argument more than once in the
-format string, although it must always be referred to with the same type.
-
-### Named parameters
-
-Rust itself does not have a Python-like equivalent of named parameters to a
-function, but the `format!` macro is a syntax extension which allows it to
-leverage named parameters. Named parameters are listed at the end of the
-argument list and have the syntax:
-
-```notrust
-identifier '=' expression
-```
-
-For example, the following `format!` expressions all use named argument:
-
-```rust
-format!("{argument}", argument = "test");       // => "test".to_owned()
-format!("{name} {}", 1, name = 2);              // => "2 1".to_owned()
-format!("{a:s} {c:d} {b:?}", a="a", b=(), c=3); // => "a 3 ()".to_owned()
-```
-
-It is illegal to put positional parameters (those without names) after arguments
-which have names. Like positional parameters, it is illegal to provided named
-parameters that are unused by the format string.
-
-### Argument types
-
-Each argument's type is dictated by the format string. It is a requirement that
-every argument is only ever referred to by one type. When specifying the format
-of an argument, however, a string like `{}` indicates no type. This is allowed,
-and if all references to one argument do not provide a type, then the format `?`
-is used (the type's rust-representation is printed). For example, this is an
-invalid format string:
-
-```notrust
-{0:d} {0:s}
-```
-
-Because the first argument is both referred to as an integer as well as a
-string.
-
-Because formatting is done via traits, there is no requirement that the
-`d` format actually takes an `int`, but rather it simply requires a type which
-ascribes to the `Signed` formatting trait. There are various parameters which do
-require a particular type, however. Namely if the syntax `{:.*s}` is used, then
-the number of characters to print from the string precedes the actual string and
-must have the type `uint`. Although a `uint` can be printed with `{:u}`, it is
-illegal to reference an argument as such. For example, this is another invalid
-format string:
-
-```notrust
-{:.*s} {0:u}
-```
-
-### Formatting traits
-
-When requesting that an argument be formatted with a particular type, you are
-actually requesting that an argument ascribes to a particular trait. This allows
-multiple actual types to be formatted via `{:d}` (like `i8` as well as `int`).
-The current mapping of types to traits is:
-
-* `?` â‡’ `Poly`
-* `d` â‡’ `Signed`
-* `i` â‡’ `Signed`
-* `u` â‡’ `Unsigned`
-* `b` â‡’ `Bool`
-* `c` â‡’ `Char`
-* `o` â‡’ `Octal`
-* `x` â‡’ `LowerHex`
-* `X` â‡’ `UpperHex`
-* `s` â‡’ `String`
-* `p` â‡’ `Pointer`
-* `t` â‡’ `Binary`
-* `f` â‡’ `Float`
-* `e` â‡’ `LowerExp`
-* `E` â‡’ `UpperExp`
-* *nothing* â‡’ `Show`
-
-What this means is that any type of argument which implements the
-`std::fmt::Binary` trait can then be formatted with `{:t}`. Implementations are
-provided for these traits for a number of primitive types by the standard
-library as well. If no format is specified (as in `{}` or `{:6}`), then the
-format trait used is the `Show` trait. This is one of the more commonly
-implemented traits when formatting a custom type.
-
-When implementing a format trait for your own type, you will have to implement a
-method of the signature:
-
-```rust
-# use std;
-# mod fmt { pub type Result = (); }
-# struct T;
-# trait SomeName<T> {
-fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result;
-# }
-```
-
-Your type will be passed as `self` by-reference, and then the function should
-emit output into the `f.buf` stream. It is up to each format trait
-implementation to correctly adhere to the requested formatting parameters. The
-values of these parameters will be listed in the fields of the `Formatter`
-struct. In order to help with this, the `Formatter` struct also provides some
-helper methods.
-
-Additionally, the return value of this function is `fmt::Result` which is a
-typedef to `Result<(), IoError>` (also known as `IoError<()>`). Formatting
-implementations should ensure that they return errors from `write!` correctly
-(propagating errors upward).
-
-An example of implementing the formatting traits would look
-like:
-
-```rust
-use std::fmt;
-use std::f64;
-
-struct Vector2D {
-    x: int,
-    y: int,
-}
-
-impl fmt::Show for Vector2D {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        // The `f.buf` value is of the type `&mut io::Writer`, which is what the
-        // write! macro is expecting. Note that this formatting ignores the
-        // various flags provided to format strings.
-        write!(f.buf, "({}, {})", self.x, self.y)
-    }
-}
-
-// Different traits allow different forms of output of a type. The meaning of
-// this format is to print the magnitude of a vector.
-impl fmt::Binary for Vector2D {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        let magnitude = (self.x * self.x + self.y * self.y) as f64;
-        let magnitude = magnitude.sqrt();
-
-        // Respect the formatting flags by using the helper method
-        // `pad_integral` on the Formatter object. See the method documentation
-        // for details, and the function `pad` can be used to pad strings.
-        let decimals = f.precision.unwrap_or(3);
-        let string = f64::to_str_exact(magnitude, decimals);
-        f.pad_integral(true, "", string.as_bytes())
-    }
-}
-
-fn main() {
-    let myvector = Vector2D { x: 3, y: 4 };
-
-    println!("{}", myvector);       // => "(3, 4)"
-    println!("{:10.3t}", myvector); // => "     5.000"
-}
-```
-
-### Related macros
-
-There are a number of related macros in the `format!` family. The ones that are
-currently implemented are:
-
-```ignore
-format!      // described above
-write!       // first argument is a &mut io::Writer, the destination
-writeln!     // same as write but appends a newline
-print!       // the format string is printed to the standard output
-println!     // same as print but appends a newline
-format_args! // described below.
-```
-
-
-#### `write!`
-
-This and `writeln` are two macros which are used to emit the format string to a
-specified stream. This is used to prevent intermediate allocations of format
-strings and instead directly write the output. Under the hood, this function is
-actually invoking the `write` function defined in this module. Example usage is:
-
-```rust
-# #![allow(unused_must_use)]
-use std::io;
-
-let mut w = io::MemWriter::new();
-write!(&mut w as &mut io::Writer, "Hello {}!", "world");
-```
-
-#### `print!`
-
-This and `println` emit their output to stdout. Similarly to the `write!` macro,
-the goal of these macros is to avoid intermediate allocations when printing
-output. Example usage is:
-
-```rust
-print!("Hello {}!", "world");
-println!("I have a newline {}", "character at the end");
-```
-
-#### `format_args!`
-This is a curious macro which is used to safely pass around
-an opaque object describing the format string. This object
-does not require any heap allocations to create, and it only
-references information on the stack. Under the hood, all of
-the related macros are implemented in terms of this. First
-off, some example usage is:
-
-```
-use std::fmt;
-use std::io;
-
-# #[allow(unused_must_use)]
-# fn main() {
-format_args!(fmt::format, "this returns {}", "~str");
-
-let some_writer: &mut io::Writer = &mut io::stdout();
-format_args!(|args| { fmt::write(some_writer, args) }, "print with a {}", "closure");
-
-fn my_fmt_fn(args: &fmt::Arguments) {
-    fmt::write(&mut io::stdout(), args);
-}
-format_args!(my_fmt_fn, "or a {} too", "function");
-# }
-```
-
-The first argument of the `format_args!` macro is a function (or closure) which
-takes one argument of type `&fmt::Arguments`. This structure can then be
-passed to the `write` and `format` functions inside this module in order to
-process the format string. The goal of this macro is to even further prevent
-intermediate allocations when dealing formatting strings.
-
-For example, a logging library could use the standard formatting syntax, but it
-would internally pass around this structure until it has been determined where
-output should go to.
-
-It is unsafe to programmatically create an instance of `fmt::Arguments` because
-the operations performed when executing a format string require the compile-time
-checks provided by the compiler. The `format_args!` macro is the only method of
-safely creating these structures, but they can be unsafely created with the
-constructor provided.
-
-## Internationalization
-
-The formatting syntax supported by the `format!` extension supports
-internationalization by providing "methods" which execute various different
-outputs depending on the input. The syntax and methods provided are similar to
-other internationalization systems, so again nothing should seem alien.
-Currently two methods are supported by this extension: "select" and "plural".
-
-Each method will execute one of a number of clauses, and then the value of the
-clause will become what's the result of the argument's format. Inside of the
-cases, nested argument strings may be provided, but all formatting arguments
-must not be done through implicit positional means. All arguments inside of each
-case of a method must be explicitly selected by their name or their integer
-position.
-
-Furthermore, whenever a case is running, the special character `#` can be used
-to reference the string value of the argument which was selected upon. As an
-example:
-
-```rust
-format!("{0, select, other{#}}", "hello"); // => "hello".to_owned()
-```
-
-This example is the equivalent of `{0:s}` essentially.
-
-### Select
-
-The select method is a switch over a `&str` parameter, and the parameter *must*
-be of the type `&str`. An example of the syntax is:
-
-```notrust
-{0, select, male{...} female{...} other{...}}
-```
-
-Breaking this down, the `0`-th argument is selected upon with the `select`
-method, and then a number of cases follow. Each case is preceded by an
-identifier which is the match-clause to execute the given arm. In this case,
-there are two explicit cases, `male` and `female`. The case will be executed if
-the string argument provided is an exact match to the case selected.
-
-The `other` case is also a required case for all `select` methods. This arm will
-be executed if none of the other arms matched the word being selected over.
-
-### Plural
-
-The plural method is a switch statement over a `uint` parameter, and the
-parameter *must* be a `uint`. A plural method in its full glory can be specified
-as:
-
-```notrust
-{0, plural, offset=1 =1{...} two{...} many{...} other{...}}
-```
-
-To break this down, the first `0` indicates that this method is selecting over
-the value of the first positional parameter to the format string. Next, the
-`plural` method is being executed. An optionally-supplied `offset` is then given
-which indicates a number to subtract from argument `0` when matching. This is
-then followed by a list of cases.
-
-Each case is allowed to supply a specific value to match upon with the syntax
-`=N`. This case is executed if the value at argument `0` matches N exactly,
-without taking the offset into account. A case may also be specified by one of
-five keywords: `zero`, `one`, `two`, `few`, and `many`. These cases are matched
-on after argument `0` has the offset taken into account. Currently the
-definitions of `many` and `few` are hardcoded, but they are in theory defined by
-the current locale.
-
-Finally, all `plural` methods must have an `other` case supplied which will be
-executed if none of the other cases match.
-
-## Syntax
-
-The syntax for the formatting language used is drawn from other languages, so it
-should not be too alien. Arguments are formatted with python-like syntax,
-meaning that arguments are surrounded by `{}` instead of the C-like `%`. The
-actual grammar for the formatting syntax is:
-
-```notrust
-format_string := <text> [ format <text> ] *
-format := '{' [ argument ] [ ':' format_spec ] [ ',' function_spec ] '}'
-argument := integer | identifier
-
-format_spec := [[fill]align][sign]['#'][0][width]['.' precision][type]
-fill := character
-align := '<' | '>'
-sign := '+' | '-'
-width := count
-precision := count | '*'
-type := identifier | ''
-count := parameter | integer
-parameter := integer '$'
-
-function_spec := plural | select
-select := 'select' ',' ( identifier arm ) *
-plural := 'plural' ',' [ 'offset:' integer ] ( selector arm ) *
-selector := '=' integer | keyword
-keyword := 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'
-arm := '{' format_string '}'
-```
-
-## Formatting Parameters
-
-Each argument being formatted can be transformed by a number of formatting
-parameters (corresponding to `format_spec` in the syntax above). These
-parameters affect the string representation of what's being formatted. This
-syntax draws heavily from Python's, so it may seem a bit familiar.
-
-### Fill/Alignment
-
-The fill character is provided normally in conjunction with the `width`
-parameter. This indicates that if the value being formatted is smaller than
-`width` some extra characters will be printed around it. The extra characters
-are specified by `fill`, and the alignment can be one of two options:
-
-* `<` - the argument is left-aligned in `width` columns
-* `>` - the argument is right-aligned in `width` columns
-
-### Sign/#/0
-
-These can all be interpreted as flags for a particular formatter.
-
-* '+' - This is intended for numeric types and indicates that the sign should
-        always be printed. Positive signs are never printed by default, and the
-        negative sign is only printed by default for the `Signed` trait. This
-        flag indicates that the correct sign (+ or -) should always be printed.
-* '-' - Currently not used
-* '#' - This flag is indicates that the "alternate" form of printing should be
-        used. By default, this only applies to the integer formatting traits and
-        performs like:
-    * `x` - precedes the argument with a "0x"
-    * `X` - precedes the argument with a "0x"
-    * `t` - precedes the argument with a "0b"
-    * `o` - precedes the argument with a "0o"
-* '0' - This is used to indicate for integer formats that the padding should
-        both be done with a `0` character as well as be sign-aware. A format
-        like `{:08d}` would yield `00000001` for the integer `1`, while the same
-        format would yield `-0000001` for the integer `-1`. Notice that the
-        negative version has one fewer zero than the positive version.
-
-### Width
-
-This is a parameter for the "minimum width" that the format should take up. If
-the value's string does not fill up this many characters, then the padding
-specified by fill/alignment will be used to take up the required space.
-
-The default fill/alignment for non-numerics is a space and left-aligned. The
-defaults for numeric formatters is also a space but with right-alignment. If the
-'0' flag is specified for numerics, then the implicit fill character is '0'.
-
-The value for the width can also be provided as a `uint` in the list of
-parameters by using the `2$` syntax indicating that the second argument is a
-`uint` specifying the width.
-
-### Precision
-
-For non-numeric types, this can be considered a "maximum width". If the
-resulting string is longer than this width, then it is truncated down to this
-many characters and only those are emitted.
-
-For integral types, this has no meaning currently.
-
-For floating-point types, this indicates how many digits after the decimal point
-should be printed.
-
-## Escaping
-
-The literal characters `{`, `}`, or `#` may be included in a string by
-preceding them with the `\` character. Since `\` is already an
-escape character in Rust strings, a string literal using this escape
-will look like `"\\{"`.
-
-*/
-
-use any;
-use cell::Cell;
-use char::Char;
-use cmp;
-use container::Container;
-use intrinsics::TypeId;
-use io::MemWriter;
-use io;
-use iter::{Iterator, range};
-use iter;
-use kinds::Copy;
-use mem;
-use num::Signed;
-use option::{Option, Some, None};
-use owned::Box;
-use repr;
-use result::{Ok, Err, ResultUnwrap};
-use slice::{Vector, ImmutableVector};
-use slice;
-use str::{StrSlice, StrAllocating, UTF16Item, ScalarValue, LoneSurrogate};
-use str;
-use strbuf::StrBuf;
-
-pub use self::num::radix;
-pub use self::num::Radix;
-pub use self::num::RadixFmt;
-
-mod num;
-pub mod rt;
-
-pub type Result = io::IoResult<()>;
-
-/// A struct to represent both where to emit formatting strings to and how they
-/// should be formatted. A mutable version of this is passed to all formatting
-/// traits.
-pub struct Formatter<'a> {
-    /// Flags for formatting (packed version of rt::Flag)
-    pub flags: uint,
-    /// Character used as 'fill' whenever there is alignment
-    pub fill: char,
-    /// Boolean indication of whether the output should be left-aligned
-    pub align: rt::Alignment,
-    /// Optionally specified integer width that the output should be
-    pub width: Option<uint>,
-    /// Optionally specified precision for numeric types
-    pub precision: Option<uint>,
-
-    /// Output buffer.
-    pub buf: &'a mut io::Writer,
-    curarg: slice::Items<'a, Argument<'a>>,
-    args: &'a [Argument<'a>],
-}
-
-/// This struct represents the generic "argument" which is taken by the Xprintf
-/// family of functions. It contains a function to format the given value. At
-/// compile time it is ensured that the function and the value have the correct
-/// types, and then this struct is used to canonicalize arguments to one type.
-pub struct Argument<'a> {
-    formatter: extern "Rust" fn(&any::Void, &mut Formatter) -> Result,
-    value: &'a any::Void,
-}
-
-impl<'a> Arguments<'a> {
-    /// When using the format_args!() macro, this function is used to generate the
-    /// Arguments structure. The compiler inserts an `unsafe` block to call this,
-    /// which is valid because the compiler performs all necessary validation to
-    /// ensure that the resulting call to format/write would be safe.
-    #[doc(hidden)] #[inline]
-    pub unsafe fn new<'a>(fmt: &'static [rt::Piece<'static>],
-                          args: &'a [Argument<'a>]) -> Arguments<'a> {
-        Arguments{ fmt: mem::transmute(fmt), args: args }
-    }
-}
-
-/// This structure represents a safely precompiled version of a format string
-/// and its arguments. This cannot be generated at runtime because it cannot
-/// safely be done so, so no constructors are given and the fields are private
-/// to prevent modification.
-///
-/// The `format_args!` macro will safely create an instance of this structure
-/// and pass it to a user-supplied function. The macro validates the format
-/// string at compile-time so usage of the `write` and `format` functions can
-/// be safely performed.
-pub struct Arguments<'a> {
-    fmt: &'a [rt::Piece<'a>],
-    args: &'a [Argument<'a>],
-}
-
-impl<'a> Show for Arguments<'a> {
-    fn fmt(&self, fmt: &mut Formatter) -> Result {
-        write(fmt.buf, self)
-    }
-}
-
-/// When a format is not otherwise specified, types are formatted by ascribing
-/// to this trait. There is not an explicit way of selecting this trait to be
-/// used for formatting, it is only if no other format is specified.
-pub trait Show {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `b` character
-pub trait Bool {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `c` character
-pub trait Char {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `i` and `d` characters
-pub trait Signed {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `u` character
-pub trait Unsigned {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `o` character
-pub trait Octal {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `t` character
-pub trait Binary {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `x` character
-pub trait LowerHex {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `X` character
-pub trait UpperHex {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `s` character
-pub trait String {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `?` character
-pub trait Poly {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `p` character
-pub trait Pointer {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `f` character
-pub trait Float {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `e` character
-pub trait LowerExp {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-/// Format trait for the `E` character
-pub trait UpperExp {
-    /// Formats the value using the given formatter.
-    fn fmt(&self, &mut Formatter) -> Result;
-}
-
-// FIXME #11938 - UFCS would make us able call the above methods
-// directly Show::show(x, fmt).
-macro_rules! uniform_fn_call_workaround {
-    ($( $name: ident, $trait_: ident; )*) => {
-        $(
-            #[doc(hidden)]
-            pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) -> Result {
-                x.fmt(fmt)
-            }
-            )*
-    }
-}
-uniform_fn_call_workaround! {
-    secret_show, Show;
-    secret_bool, Bool;
-    secret_char, Char;
-    secret_signed, Signed;
-    secret_unsigned, Unsigned;
-    secret_octal, Octal;
-    secret_binary, Binary;
-    secret_lower_hex, LowerHex;
-    secret_upper_hex, UpperHex;
-    secret_string, String;
-    secret_poly, Poly;
-    secret_pointer, Pointer;
-    secret_float, Float;
-    secret_lower_exp, LowerExp;
-    secret_upper_exp, UpperExp;
-}
-
-/// The `write` function takes an output stream, a precompiled format string,
-/// and a list of arguments. The arguments will be formatted according to the
-/// specified format string into the output stream provided.
-///
-/// # Arguments
-///
-///   * output - the buffer to write output to
-///   * args - the precompiled arguments generated by `format_args!`
-///
-/// # Example
-///
-/// ```rust
-/// # #![allow(unused_must_use)]
-/// use std::fmt;
-/// use std::io;
-///
-/// let mut w = io::stdout();
-/// format_args!(|args| { fmt::write(&mut w, args); }, "Hello, {}!", "world");
-/// ```
-pub fn write(output: &mut io::Writer, args: &Arguments) -> Result {
-    unsafe { write_unsafe(output, args.fmt, args.args) }
-}
-
-/// The `writeln` function takes the same arguments as `write`, except that it
-/// will also write a newline (`\n`) character at the end of the format string.
-pub fn writeln(output: &mut io::Writer, args: &Arguments) -> Result {
-    let first = unsafe { write_unsafe(output, args.fmt, args.args) };
-    first.and_then(|()| output.write(['\n' as u8]))
-}
-
-/// The `write_unsafe` function takes an output stream, a precompiled format
-/// string, and a list of arguments. The arguments will be formatted according
-/// to the specified format string into the output stream provided.
-///
-/// See the documentation for `format` for why this function is unsafe and care
-/// should be taken if calling it manually.
-///
-/// Thankfully the rust compiler provides macros like `write!` and
-/// `format_args!` which perform all of this validation at compile-time
-/// and provide a safe interface for invoking this function.
-///
-/// # Arguments
-///
-///   * output - the buffer to write output to
-///   * fmts - the precompiled format string to emit
-///   * args - the list of arguments to the format string. These are only the
-///            positional arguments (not named)
-///
-/// Note that this function assumes that there are enough arguments for the
-/// format string.
-pub unsafe fn write_unsafe(output: &mut io::Writer,
-                           fmt: &[rt::Piece],
-                           args: &[Argument]) -> Result {
-    let mut formatter = Formatter {
-        flags: 0,
-        width: None,
-        precision: None,
-        buf: output,
-        align: rt::AlignUnknown,
-        fill: ' ',
-        args: args,
-        curarg: args.iter(),
-    };
-    for piece in fmt.iter() {
-        try!(formatter.run(piece, None));
-    }
-    Ok(())
-}
-
-/// The format function takes a precompiled format string and a list of
-/// arguments, to return the resulting formatted string.
-///
-/// # Arguments
-///
-///   * args - a structure of arguments generated via the `format_args!` macro.
-///            Because this structure can only be safely generated at
-///            compile-time, this function is safe.
-///
-/// # Example
-///
-/// ```rust
-/// use std::fmt;
-///
-/// let s = format_args!(fmt::format, "Hello, {}!", "world");
-/// assert_eq!(s, "Hello, world!".to_owned());
-/// ```
-pub fn format(args: &Arguments) -> ~str {
-    unsafe { format_unsafe(args.fmt, args.args) }
-}
-
-/// Temporary transitionary thing.
-pub fn format_strbuf(args: &Arguments) -> StrBuf {
-    unsafe { format_unsafe_strbuf(args.fmt, args.args) }
-}
-
-/// The unsafe version of the formatting function.
-///
-/// This is currently an unsafe function because the types of all arguments
-/// aren't verified by immediate callers of this function. This currently does
-/// not validate that the correct types of arguments are specified for each
-/// format specifier, nor that each argument itself contains the right function
-/// for formatting the right type value. Because of this, the function is marked
-/// as `unsafe` if this is being called manually.
-///
-/// Thankfully the rust compiler provides the macro `format!` which will perform
-/// all of this validation at compile-time and provides a safe interface for
-/// invoking this function.
-///
-/// # Arguments
-///
-///   * fmts - the precompiled format string to emit.
-///   * args - the list of arguments to the format string. These are only the
-///            positional arguments (not named)
-///
-/// Note that this function assumes that there are enough arguments for the
-/// format string.
-pub unsafe fn format_unsafe(fmt: &[rt::Piece], args: &[Argument]) -> ~str {
-    let mut output = MemWriter::new();
-    write_unsafe(&mut output as &mut io::Writer, fmt, args).unwrap();
-    return str::from_utf8(output.unwrap().as_slice()).unwrap().to_owned();
-}
-
-/// Temporary transitionary thing.
-pub unsafe fn format_unsafe_strbuf(fmt: &[rt::Piece], args: &[Argument])
-                                   -> StrBuf {
-    let mut output = MemWriter::new();
-    write_unsafe(&mut output as &mut io::Writer, fmt, args).unwrap();
-    return str::from_utf8(output.unwrap().as_slice()).unwrap().into_strbuf();
-}
-
-impl<'a> Formatter<'a> {
-
-    // First up is the collection of functions used to execute a format string
-    // at runtime. This consumes all of the compile-time statics generated by
-    // the format! syntax extension.
-
-    fn run(&mut self, piece: &rt::Piece, cur: Option<&str>) -> Result {
-        match *piece {
-            rt::String(s) => self.buf.write(s.as_bytes()),
-            rt::CurrentArgument(()) => self.buf.write(cur.unwrap().as_bytes()),
-            rt::Argument(ref arg) => {
-                // Fill in the format parameters into the formatter
-                self.fill = arg.format.fill;
-                self.align = arg.format.align;
-                self.flags = arg.format.flags;
-                self.width = self.getcount(&arg.format.width);
-                self.precision = self.getcount(&arg.format.precision);
-
-                // Extract the correct argument
-                let value = match arg.position {
-                    rt::ArgumentNext => { *self.curarg.next().unwrap() }
-                    rt::ArgumentIs(i) => self.args[i],
-                };
-
-                // Then actually do some printing
-                match arg.method {
-                    None => (value.formatter)(value.value, self),
-                    Some(ref method) => self.execute(*method, value)
-                }
-            }
-        }
-    }
-
-    fn getcount(&mut self, cnt: &rt::Count) -> Option<uint> {
-        match *cnt {
-            rt::CountIs(n) => { Some(n) }
-            rt::CountImplied => { None }
-            rt::CountIsParam(i) => {
-                let v = self.args[i].value;
-                unsafe { Some(*(v as *any::Void as *uint)) }
-            }
-            rt::CountIsNextParam => {
-                let v = self.curarg.next().unwrap().value;
-                unsafe { Some(*(v as *any::Void as *uint)) }
-            }
-        }
-    }
-
-    fn execute(&mut self, method: &rt::Method, arg: Argument) -> Result {
-        match *method {
-            // Pluralization is selection upon a numeric value specified as the
-            // parameter.
-            rt::Plural(offset, ref selectors, ref default) => {
-                // This is validated at compile-time to be a pointer to a
-                // '&uint' value.
-                let value: &uint = unsafe { mem::transmute(arg.value) };
-                let value = *value;
-
-                // First, attempt to match against explicit values without the
-                // offsetted value
-                for s in selectors.iter() {
-                    match s.selector {
-                        rt::Literal(val) if value == val => {
-                            return self.runplural(value, s.result);
-                        }
-                        _ => {}
-                    }
-                }
-
-                // Next, offset the value and attempt to match against the
-                // keyword selectors.
-                let value = value - match offset { Some(i) => i, None => 0 };
-                for s in selectors.iter() {
-                    let run = match s.selector {
-                        rt::Keyword(rt::Zero) => value == 0,
-                        rt::Keyword(rt::One) => value == 1,
-                        rt::Keyword(rt::Two) => value == 2,
-
-                        // FIXME: Few/Many should have a user-specified boundary
-                        //      One possible option would be in the function
-                        //      pointer of the 'arg: Argument' struct.
-                        rt::Keyword(rt::Few) => value < 8,
-                        rt::Keyword(rt::Many) => value >= 8,
-
-                        rt::Literal(..) => false
-                    };
-                    if run {
-                        return self.runplural(value, s.result);
-                    }
-                }
-
-                self.runplural(value, *default)
-            }
-
-            // Select is just a matching against the string specified.
-            rt::Select(ref selectors, ref default) => {
-                // This is validated at compile-time to be a pointer to a
-                // string slice,
-                let value: & &str = unsafe { mem::transmute(arg.value) };
-                let value = *value;
-
-                for s in selectors.iter() {
-                    if s.selector == value {
-                        for piece in s.result.iter() {
-                            try!(self.run(piece, Some(value)));
-                        }
-                        return Ok(());
-                    }
-                }
-                for piece in default.iter() {
-                    try!(self.run(piece, Some(value)));
-                }
-                Ok(())
-            }
-        }
-    }
-
-    fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) -> Result {
-        ::uint::to_str_bytes(value, 10, |buf| {
-            let valuestr = str::from_utf8(buf).unwrap();
-            for piece in pieces.iter() {
-                try!(self.run(piece, Some(valuestr)));
-            }
-            Ok(())
-        })
-    }
-
-    // Helper methods used for padding and processing formatting arguments that
-    // all formatting traits can use.
-
-    /// Performs the correct padding for an integer which has already been
-    /// emitted into a byte-array. The byte-array should *not* contain the sign
-    /// for the integer, that will be added by this method.
-    ///
-    /// # Arguments
-    ///
-    /// * is_positive - whether the original integer was positive or not.
-    /// * prefix - if the '#' character (FlagAlternate) is provided, this
-    ///   is the prefix to put in front of the number.
-    /// * buf - the byte array that the number has been formatted into
-    ///
-    /// This function will correctly account for the flags provided as well as
-    /// the minimum width. It will not take precision into account.
-    pub fn pad_integral(&mut self, is_positive: bool, prefix: &str, buf: &[u8]) -> Result {
-        use fmt::rt::{FlagAlternate, FlagSignPlus, FlagSignAwareZeroPad};
-
-        let mut width = buf.len();
-
-        let mut sign = None;
-        if !is_positive {
-            sign = Some('-'); width += 1;
-        } else if self.flags & (1 << (FlagSignPlus as uint)) != 0 {
-            sign = Some('+'); width += 1;
-        }
-
-        let mut prefixed = false;
-        if self.flags & (1 << (FlagAlternate as uint)) != 0 {
-            prefixed = true; width += prefix.len();
-        }
-
-        // Writes the sign if it exists, and then the prefix if it was requested
-        let write_prefix = |f: &mut Formatter| {
-            for c in sign.move_iter() { try!(f.buf.write_char(c)); }
-            if prefixed { f.buf.write_str(prefix) }
-            else { Ok(()) }
-        };
-
-        // The `width` field is more of a `min-width` parameter at this point.
-        match self.width {
-            // If there's no minimum length requirements then we can just
-            // write the bytes.
-            None => {
-                try!(write_prefix(self)); self.buf.write(buf)
-            }
-            // Check if we're over the minimum width, if so then we can also
-            // just write the bytes.
-            Some(min) if width >= min => {
-                try!(write_prefix(self)); self.buf.write(buf)
-            }
-            // The sign and prefix goes before the padding if the fill character
-            // is zero
-            Some(min) if self.flags & (1 << (FlagSignAwareZeroPad as uint)) != 0 => {
-                self.fill = '0';
-                try!(write_prefix(self));
-                self.with_padding(min - width, rt::AlignRight, |f| f.buf.write(buf))
-            }
-            // Otherwise, the sign and prefix goes after the padding
-            Some(min) => {
-                self.with_padding(min - width, rt::AlignRight, |f| {
-                    try!(write_prefix(f)); f.buf.write(buf)
-                })
-            }
-        }
-    }
-
-    /// This function takes a string slice and emits it to the internal buffer
-    /// after applying the relevant formatting flags specified. The flags
-    /// recognized for generic strings are:
-    ///
-    /// * width - the minimum width of what to emit
-    /// * fill/align - what to emit and where to emit it if the string
-    ///                provided needs to be padded
-    /// * precision - the maximum length to emit, the string is truncated if it
-    ///               is longer than this length
-    ///
-    /// Notably this function ignored the `flag` parameters
-    pub fn pad(&mut self, s: &str) -> Result {
-        // Make sure there's a fast path up front
-        if self.width.is_none() && self.precision.is_none() {
-            return self.buf.write(s.as_bytes());
-        }
-        // The `precision` field can be interpreted as a `max-width` for the
-        // string being formatted
-        match self.precision {
-            Some(max) => {
-                // If there's a maximum width and our string is longer than
-                // that, then we must always have truncation. This is the only
-                // case where the maximum length will matter.
-                let char_len = s.char_len();
-                if char_len >= max {
-                    let nchars = ::cmp::min(max, char_len);
-                    return self.buf.write(s.slice_chars(0, nchars).as_bytes());
-                }
-            }
-            None => {}
-        }
-        // The `width` field is more of a `min-width` parameter at this point.
-        match self.width {
-            // If we're under the maximum length, and there's no minimum length
-            // requirements, then we can just emit the string
-            None => self.buf.write(s.as_bytes()),
-            // If we're under the maximum width, check if we're over the minimum
-            // width, if so it's as easy as just emitting the string.
-            Some(width) if s.char_len() >= width => {
-                self.buf.write(s.as_bytes())
-            }
-            // If we're under both the maximum and the minimum width, then fill
-            // up the minimum width with the specified string + some alignment.
-            Some(width) => {
-                self.with_padding(width - s.len(), rt::AlignLeft, |me| {
-                    me.buf.write(s.as_bytes())
-                })
-            }
-        }
-    }
-
-    /// Runs a callback, emitting the correct padding either before or
-    /// afterwards depending on whether right or left alingment is requested.
-    fn with_padding(&mut self,
-                    padding: uint,
-                    default: rt::Alignment,
-                    f: |&mut Formatter| -> Result) -> Result {
-        let align = match self.align {
-            rt::AlignUnknown => default,
-            rt::AlignLeft | rt::AlignRight => self.align
-        };
-        if align == rt::AlignLeft {
-            try!(f(self));
-        }
-        let mut fill = [0u8, ..4];
-        let len = self.fill.encode_utf8(fill);
-        for _ in range(0, padding) {
-            try!(self.buf.write(fill.slice_to(len)));
-        }
-        if align == rt::AlignRight {
-            try!(f(self));
-        }
-        Ok(())
-    }
-}
-
-/// This is a function which calls are emitted to by the compiler itself to
-/// create the Argument structures that are passed into the `format` function.
-#[doc(hidden)] #[inline]
-pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
-                       t: &'a T) -> Argument<'a> {
-    unsafe {
-        Argument {
-            formatter: mem::transmute(f),
-            value: mem::transmute(t)
-        }
-    }
-}
-
-/// When the compiler determines that the type of an argument *must* be a string
-/// (such as for select), then it invokes this method.
-#[doc(hidden)] #[inline]
-pub fn argumentstr<'a>(s: &'a &str) -> Argument<'a> {
-    argument(secret_string, s)
-}
-
-/// When the compiler determines that the type of an argument *must* be a uint
-/// (such as for plural), then it invokes this method.
-#[doc(hidden)] #[inline]
-pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
-    argument(secret_unsigned, s)
-}
-
-// Implementations of the core formatting traits
-
-impl<T: Show> Show for @T {
-    fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
-}
-impl<T: Show> Show for Box<T> {
-    fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
-}
-impl<'a, T: Show> Show for &'a T {
-    fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
-}
-impl<'a, T: Show> Show for &'a mut T {
-    fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
-}
-
-impl Bool for bool {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        secret_string(&(if *self {"true"} else {"false"}), f)
-    }
-}
-
-impl<'a, T: str::Str> String for T {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        f.pad(self.as_slice())
-    }
-}
-
-impl Char for char {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        let mut utf8 = [0u8, ..4];
-        let amt = self.encode_utf8(utf8);
-        let s: &str = unsafe { mem::transmute(utf8.slice_to(amt)) };
-        secret_string(&s, f)
-    }
-}
-
-macro_rules! floating(($ty:ident) => {
-    impl Float for $ty {
-        fn fmt(&self, fmt: &mut Formatter) -> Result {
-            // FIXME: this shouldn't perform an allocation
-            let s = match fmt.precision {
-                Some(i) => ::$ty::to_str_exact(self.abs(), i),
-                None => ::$ty::to_str_digits(self.abs(), 6)
-            };
-            fmt.pad_integral(*self >= 0.0, "", s.as_bytes())
-        }
-    }
-
-    impl LowerExp for $ty {
-        fn fmt(&self, fmt: &mut Formatter) -> Result {
-            // FIXME: this shouldn't perform an allocation
-            let s = match fmt.precision {
-                Some(i) => ::$ty::to_str_exp_exact(self.abs(), i, false),
-                None => ::$ty::to_str_exp_digits(self.abs(), 6, false)
-            };
-            fmt.pad_integral(*self >= 0.0, "", s.as_bytes())
-        }
-    }
-
-    impl UpperExp for $ty {
-        fn fmt(&self, fmt: &mut Formatter) -> Result {
-            // FIXME: this shouldn't perform an allocation
-            let s = match fmt.precision {
-                Some(i) => ::$ty::to_str_exp_exact(self.abs(), i, true),
-                None => ::$ty::to_str_exp_digits(self.abs(), 6, true)
-            };
-            fmt.pad_integral(*self >= 0.0, "", s.as_bytes())
-        }
-    }
-})
-floating!(f32)
-floating!(f64)
-
-impl<T> Poly for T {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        match (f.width, f.precision) {
-            (None, None) => {
-                repr::write_repr(f.buf, self)
-            }
-
-            // If we have a specified width for formatting, then we have to make
-            // this allocation of a new string
-            _ => {
-                let s = repr::repr_to_str(self);
-                f.pad(s)
-            }
-        }
-    }
-}
-
-impl<T> Pointer for *T {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        f.flags |= 1 << (rt::FlagAlternate as uint);
-        secret_lower_hex::<uint>(&(*self as uint), f)
-    }
-}
-impl<T> Pointer for *mut T {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        secret_pointer::<*T>(&(*self as *T), f)
-    }
-}
-impl<'a, T> Pointer for &'a T {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        secret_pointer::<*T>(&(&**self as *T), f)
-    }
-}
-impl<'a, T> Pointer for &'a mut T {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        secret_pointer::<*T>(&(&**self as *T), f)
-    }
-}
-
-// Implementation of Show for various core types
-
-macro_rules! delegate(($ty:ty to $other:ident) => {
-    impl<'a> Show for $ty {
-        fn fmt(&self, f: &mut Formatter) -> Result {
-            (concat_idents!(secret_, $other)(self, f))
-        }
-    }
-})
-delegate!(~str to string)
-delegate!(&'a str to string)
-delegate!(bool to bool)
-delegate!(char to char)
-delegate!(f32 to float)
-delegate!(f64 to float)
-
-impl<T> Show for *T {
-    fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
-}
-impl<T> Show for *mut T {
-    fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
-}
-
-macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
-
-macro_rules! tuple (
-    () => ();
-    ( $($name:ident,)+ ) => (
-        impl<$($name:Show),*> Show for ($($name,)*) {
-            #[allow(uppercase_variables, dead_assignment)]
-            fn fmt(&self, f: &mut Formatter) -> Result {
-                try!(write!(f.buf, "("));
-                let ($(ref $name,)*) = *self;
-                let mut n = 0;
-                $(
-                    if n > 0 {
-                        try!(write!(f.buf, ", "));
-                    }
-                    try!(write!(f.buf, "{}", *$name));
-                    n += 1;
-                )*
-                if n == 1 {
-                    try!(write!(f.buf, ","));
-                }
-                write!(f.buf, ")")
-            }
-        }
-        peel!($($name,)*)
-    )
-)
-
-tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
-
-impl Show for Box<any::Any> {
-    fn fmt(&self, f: &mut Formatter) -> Result { f.pad("Box<Any>") }
-}
-
-impl<'a> Show for &'a any::Any {
-    fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") }
-}
-
-impl<T: Show> Show for Option<T> {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        match *self {
-            Some(ref t) => write!(f.buf, "Some({})", *t),
-            None => write!(f.buf, "None"),
-        }
-    }
-}
-
-impl<T: Show, U: Show> Show for ::result::Result<T, U> {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        match *self {
-            Ok(ref t) => write!(f.buf, "Ok({})", *t),
-            Err(ref t) => write!(f.buf, "Err({})", *t),
-        }
-    }
-}
-
-impl<'a, T: Show> Show for &'a [T] {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
-            try!(write!(f.buf, "["));
-        }
-        let mut is_first = true;
-        for x in self.iter() {
-            if is_first {
-                is_first = false;
-            } else {
-                try!(write!(f.buf, ", "));
-            }
-            try!(write!(f.buf, "{}", *x))
-        }
-        if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
-            try!(write!(f.buf, "]"));
-        }
-        Ok(())
-    }
-}
-
-impl<'a, T: Show> Show for &'a mut [T] {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        secret_show(&self.as_slice(), f)
-    }
-}
-
-impl<T: Show> Show for ~[T] {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        secret_show(&self.as_slice(), f)
-    }
-}
-
-impl Show for () {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        f.pad("()")
-    }
-}
-
-impl Show for TypeId {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        write!(f.buf, "TypeId \\{ {} \\}", self.hash())
-    }
-}
-
-impl<T: Show> Show for iter::MinMaxResult<T> {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        match *self {
-            iter::NoElements =>
-                write!(f.buf, "NoElements"),
-            iter::OneElement(ref t) =>
-                write!(f.buf, "OneElement({})", *t),
-            iter::MinMax(ref t1, ref t2) =>
-                write!(f.buf, "MinMax({}, {})", *t1, *t2),
-        }
-    }
-}
-
-impl Show for cmp::Ordering {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        match *self {
-            cmp::Less => write!(f.buf, "Less"),
-            cmp::Greater => write!(f.buf, "Greater"),
-            cmp::Equal => write!(f.buf, "Equal"),
-        }
-    }
-}
-
-impl<T: Copy + Show> Show for Cell<T> {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        write!(f.buf, r"Cell \{ value: {} \}", self.get())
-    }
-}
-
-impl Show for UTF16Item {
-    fn fmt(&self, f: &mut Formatter) -> Result {
-        match *self {
-            ScalarValue(c) => write!(f.buf, "ScalarValue({})", c),
-            LoneSurrogate(u) => write!(f.buf, "LoneSurrogate({})", u),
-        }
-    }
-}
-
-// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
-// it's a lot easier than creating all of the rt::Piece structures here.
diff --git a/src/libstd/fmt/num.rs b/src/libstd/fmt/num.rs
deleted file mode 100644 (file)
index 839b740..0000000
+++ /dev/null
@@ -1,472 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! Integer and floating-point number formatting
-
-// FIXME: #6220 Implement floating point formatting
-
-#![allow(unsigned_negate)]
-
-use container::Container;
-use fmt;
-use iter::{Iterator, DoubleEndedIterator};
-use num::{Int, cast, zero};
-use option::{Some, None};
-use slice::{ImmutableVector, MutableVector};
-
-/// A type that represents a specific radix
-trait GenericRadix {
-    /// The number of digits.
-    fn base(&self) -> u8;
-
-    /// A radix-specific prefix string.
-    fn prefix(&self) -> &'static str { "" }
-
-    /// Converts an integer to corresponding radix digit.
-    fn digit(&self, x: u8) -> u8;
-
-    /// Format an integer using the radix using a formatter.
-    fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
-        // The radix can be as low as 2, so we need a buffer of at least 64
-        // characters for a base 2 number.
-        let mut buf = [0u8, ..64];
-        let base = cast(self.base()).unwrap();
-        let mut curr = buf.len();
-        let is_positive = x >= zero();
-        if is_positive {
-            // Accumulate each digit of the number from the least significant
-            // to the most significant figure.
-            for byte in buf.mut_iter().rev() {
-                let n = x % base;                         // Get the current place value.
-                x = x / base;                             // Deaccumulate the number.
-                *byte = self.digit(cast(n).unwrap());     // Store the digit in the buffer.
-                curr -= 1;
-                if x == zero() { break; }                 // No more digits left to accumulate.
-            }
-        } else {
-            // Do the same as above, but accounting for two's complement.
-            for byte in buf.mut_iter().rev() {
-                let n = -(x % base);                      // Get the current place value.
-                x = x / base;                             // Deaccumulate the number.
-                *byte = self.digit(cast(n).unwrap());     // Store the digit in the buffer.
-                curr -= 1;
-                if x == zero() { break; }                 // No more digits left to accumulate.
-            }
-        }
-        f.pad_integral(is_positive, self.prefix(), buf.slice_from(curr))
-    }
-}
-
-/// A binary (base 2) radix
-#[deriving(Clone, Eq)]
-struct Binary;
-
-/// An octal (base 8) radix
-#[deriving(Clone, Eq)]
-struct Octal;
-
-/// A decimal (base 10) radix
-#[deriving(Clone, Eq)]
-struct Decimal;
-
-/// A hexadecimal (base 16) radix, formatted with lower-case characters
-#[deriving(Clone, Eq)]
-struct LowerHex;
-
-/// A hexadecimal (base 16) radix, formatted with upper-case characters
-#[deriving(Clone, Eq)]
-pub struct UpperHex;
-
-macro_rules! radix {
-    ($T:ident, $base:expr, $prefix:expr, $($x:pat => $conv:expr),+) => {
-        impl GenericRadix for $T {
-            fn base(&self) -> u8 { $base }
-            fn prefix(&self) -> &'static str { $prefix }
-            fn digit(&self, x: u8) -> u8 {
-                match x {
-                    $($x => $conv,)+
-                    x => fail!("number not in the range 0..{}: {}", self.base() - 1, x),
-                }
-            }
-        }
-    }
-}
-
-radix!(Binary,    2, "0b", x @  0 .. 2 => '0' as u8 + x)
-radix!(Octal,     8, "0o", x @  0 .. 7 => '0' as u8 + x)
-radix!(Decimal,  10, "",   x @  0 .. 9 => '0' as u8 + x)
-radix!(LowerHex, 16, "0x", x @  0 .. 9 => '0' as u8 + x,
-                           x @ 10 ..15 => 'a' as u8 + (x - 10))
-radix!(UpperHex, 16, "0x", x @  0 .. 9 => '0' as u8 + x,
-                           x @ 10 ..15 => 'A' as u8 + (x - 10))
-
-/// A radix with in the range of `2..36`.
-#[deriving(Clone, Eq)]
-pub struct Radix {
-    base: u8,
-}
-
-impl Radix {
-    fn new(base: u8) -> Radix {
-        assert!(2 <= base && base <= 36, "the base must be in the range of 0..36: {}", base);
-        Radix { base: base }
-    }
-}
-
-impl GenericRadix for Radix {
-    fn base(&self) -> u8 { self.base }
-    fn digit(&self, x: u8) -> u8 {
-        match x {
-            x @  0 ..9 => '0' as u8 + x,
-            x if x < self.base() => 'a' as u8 + (x - 10),
-            x => fail!("number not in the range 0..{}: {}", self.base() - 1, x),
-        }
-    }
-}
-
-/// A helper type for formatting radixes.
-pub struct RadixFmt<T, R>(T, R);
-
-/// Constructs a radix formatter in the range of `2..36`.
-///
-/// # Example
-///
-/// ~~~
-/// use std::fmt::radix;
-/// assert_eq!(format!("{}", radix(55, 36)), "1j".to_owned());
-/// ~~~
-pub fn radix<T>(x: T, base: u8) -> RadixFmt<T, Radix> {
-    RadixFmt(x, Radix::new(base))
-}
-
-macro_rules! radix_fmt {
-    ($T:ty as $U:ty, $fmt:ident) => {
-        impl fmt::Show for RadixFmt<$T, Radix> {
-            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-                match *self { RadixFmt(ref x, radix) => radix.$fmt(*x as $U, f) }
-            }
-        }
-    }
-}
-macro_rules! int_base {
-    ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {
-        impl fmt::$Trait for $T {
-            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-                $Radix.fmt_int(*self as $U, f)
-            }
-        }
-    }
-}
-macro_rules! integer {
-    ($Int:ident, $Uint:ident) => {
-        int_base!(Show     for $Int as $Int   -> Decimal)
-        int_base!(Signed   for $Int as $Int   -> Decimal)
-        int_base!(Binary   for $Int as $Uint  -> Binary)
-        int_base!(Octal    for $Int as $Uint  -> Octal)
-        int_base!(LowerHex for $Int as $Uint  -> LowerHex)
-        int_base!(UpperHex for $Int as $Uint  -> UpperHex)
-        radix_fmt!($Int as $Uint, fmt_int)
-
-        int_base!(Show     for $Uint as $Uint -> Decimal)
-        int_base!(Unsigned for $Uint as $Uint -> Decimal)
-        int_base!(Binary   for $Uint as $Uint -> Binary)
-        int_base!(Octal    for $Uint as $Uint -> Octal)
-        int_base!(LowerHex for $Uint as $Uint -> LowerHex)
-        int_base!(UpperHex for $Uint as $Uint -> UpperHex)
-        radix_fmt!($Uint as $Uint, fmt_int)
-    }
-}
-integer!(int, uint)
-integer!(i8, u8)
-integer!(i16, u16)
-integer!(i32, u32)
-integer!(i64, u64)
-
-#[cfg(test)]
-mod tests {
-    use fmt::radix;
-    use super::{Binary, Octal, Decimal, LowerHex, UpperHex};
-    use super::{GenericRadix, Radix};
-    use str::StrAllocating;
-
-    #[test]
-    fn test_radix_base() {
-        assert_eq!(Binary.base(), 2);
-        assert_eq!(Octal.base(), 8);
-        assert_eq!(Decimal.base(), 10);
-        assert_eq!(LowerHex.base(), 16);
-        assert_eq!(UpperHex.base(), 16);
-        assert_eq!(Radix { base: 36 }.base(), 36);
-    }
-
-    #[test]
-    fn test_radix_prefix() {
-        assert_eq!(Binary.prefix(), "0b");
-        assert_eq!(Octal.prefix(), "0o");
-        assert_eq!(Decimal.prefix(), "");
-        assert_eq!(LowerHex.prefix(), "0x");
-        assert_eq!(UpperHex.prefix(), "0x");
-        assert_eq!(Radix { base: 36 }.prefix(), "");
-    }
-
-    #[test]
-    fn test_radix_digit() {
-        assert_eq!(Binary.digit(0), '0' as u8);
-        assert_eq!(Binary.digit(2), '2' as u8);
-        assert_eq!(Octal.digit(0), '0' as u8);
-        assert_eq!(Octal.digit(7), '7' as u8);
-        assert_eq!(Decimal.digit(0), '0' as u8);
-        assert_eq!(Decimal.digit(9), '9' as u8);
-        assert_eq!(LowerHex.digit(0), '0' as u8);
-        assert_eq!(LowerHex.digit(10), 'a' as u8);
-        assert_eq!(LowerHex.digit(15), 'f' as u8);
-        assert_eq!(UpperHex.digit(0), '0' as u8);
-        assert_eq!(UpperHex.digit(10), 'A' as u8);
-        assert_eq!(UpperHex.digit(15), 'F' as u8);
-        assert_eq!(Radix { base: 36 }.digit(0), '0' as u8);
-        assert_eq!(Radix { base: 36 }.digit(15), 'f' as u8);
-        assert_eq!(Radix { base: 36 }.digit(35), 'z' as u8);
-    }
-
-    #[test]
-    #[should_fail]
-    fn test_hex_radix_digit_overflow() {
-        let _ = LowerHex.digit(16);
-    }
-
-    #[test]
-    fn test_format_int() {
-        // Formatting integers should select the right implementation based off
-        // the type of the argument. Also, hex/octal/binary should be defined
-        // for integers, but they shouldn't emit the negative sign.
-        assert_eq!(format!("{}", 1i), "1".to_owned());
-        assert_eq!(format!("{}", 1i8), "1".to_owned());
-        assert_eq!(format!("{}", 1i16), "1".to_owned());
-        assert_eq!(format!("{}", 1i32), "1".to_owned());
-        assert_eq!(format!("{}", 1i64), "1".to_owned());
-        assert_eq!(format!("{:d}", -1i), "-1".to_owned());
-        assert_eq!(format!("{:d}", -1i8), "-1".to_owned());
-        assert_eq!(format!("{:d}", -1i16), "-1".to_owned());
-        assert_eq!(format!("{:d}", -1i32), "-1".to_owned());
-        assert_eq!(format!("{:d}", -1i64), "-1".to_owned());
-        assert_eq!(format!("{:t}", 1i), "1".to_owned());
-        assert_eq!(format!("{:t}", 1i8), "1".to_owned());
-        assert_eq!(format!("{:t}", 1i16), "1".to_owned());
-        assert_eq!(format!("{:t}", 1i32), "1".to_owned());
-        assert_eq!(format!("{:t}", 1i64), "1".to_owned());
-        assert_eq!(format!("{:x}", 1i), "1".to_owned());
-        assert_eq!(format!("{:x}", 1i8), "1".to_owned());
-        assert_eq!(format!("{:x}", 1i16), "1".to_owned());
-        assert_eq!(format!("{:x}", 1i32), "1".to_owned());
-        assert_eq!(format!("{:x}", 1i64), "1".to_owned());
-        assert_eq!(format!("{:X}", 1i), "1".to_owned());
-        assert_eq!(format!("{:X}", 1i8), "1".to_owned());
-        assert_eq!(format!("{:X}", 1i16), "1".to_owned());
-        assert_eq!(format!("{:X}", 1i32), "1".to_owned());
-        assert_eq!(format!("{:X}", 1i64), "1".to_owned());
-        assert_eq!(format!("{:o}", 1i), "1".to_owned());
-        assert_eq!(format!("{:o}", 1i8), "1".to_owned());
-        assert_eq!(format!("{:o}", 1i16), "1".to_owned());
-        assert_eq!(format!("{:o}", 1i32), "1".to_owned());
-        assert_eq!(format!("{:o}", 1i64), "1".to_owned());
-
-        assert_eq!(format!("{}", 1u), "1".to_owned());
-        assert_eq!(format!("{}", 1u8), "1".to_owned());
-        assert_eq!(format!("{}", 1u16), "1".to_owned());
-        assert_eq!(format!("{}", 1u32), "1".to_owned());
-        assert_eq!(format!("{}", 1u64), "1".to_owned());
-        assert_eq!(format!("{:u}", 1u), "1".to_owned());
-        assert_eq!(format!("{:u}", 1u8), "1".to_owned());
-        assert_eq!(format!("{:u}", 1u16), "1".to_owned());
-        assert_eq!(format!("{:u}", 1u32), "1".to_owned());
-        assert_eq!(format!("{:u}", 1u64), "1".to_owned());
-        assert_eq!(format!("{:t}", 1u), "1".to_owned());
-        assert_eq!(format!("{:t}", 1u8), "1".to_owned());
-        assert_eq!(format!("{:t}", 1u16), "1".to_owned());
-        assert_eq!(format!("{:t}", 1u32), "1".to_owned());
-        assert_eq!(format!("{:t}", 1u64), "1".to_owned());
-        assert_eq!(format!("{:x}", 1u), "1".to_owned());
-        assert_eq!(format!("{:x}", 1u8), "1".to_owned());
-        assert_eq!(format!("{:x}", 1u16), "1".to_owned());
-        assert_eq!(format!("{:x}", 1u32), "1".to_owned());
-        assert_eq!(format!("{:x}", 1u64), "1".to_owned());
-        assert_eq!(format!("{:X}", 1u), "1".to_owned());
-        assert_eq!(format!("{:X}", 1u8), "1".to_owned());
-        assert_eq!(format!("{:X}", 1u16), "1".to_owned());
-        assert_eq!(format!("{:X}", 1u32), "1".to_owned());
-        assert_eq!(format!("{:X}", 1u64), "1".to_owned());
-        assert_eq!(format!("{:o}", 1u), "1".to_owned());
-        assert_eq!(format!("{:o}", 1u8), "1".to_owned());
-        assert_eq!(format!("{:o}", 1u16), "1".to_owned());
-        assert_eq!(format!("{:o}", 1u32), "1".to_owned());
-        assert_eq!(format!("{:o}", 1u64), "1".to_owned());
-
-        // Test a larger number
-        assert_eq!(format!("{:t}", 55), "110111".to_owned());
-        assert_eq!(format!("{:o}", 55), "67".to_owned());
-        assert_eq!(format!("{:d}", 55), "55".to_owned());
-        assert_eq!(format!("{:x}", 55), "37".to_owned());
-        assert_eq!(format!("{:X}", 55), "37".to_owned());
-    }
-
-    #[test]
-    fn test_format_int_zero() {
-        assert_eq!(format!("{}", 0i), "0".to_owned());
-        assert_eq!(format!("{:d}", 0i), "0".to_owned());
-        assert_eq!(format!("{:t}", 0i), "0".to_owned());
-        assert_eq!(format!("{:o}", 0i), "0".to_owned());
-        assert_eq!(format!("{:x}", 0i), "0".to_owned());
-        assert_eq!(format!("{:X}", 0i), "0".to_owned());
-
-        assert_eq!(format!("{}", 0u), "0".to_owned());
-        assert_eq!(format!("{:u}", 0u), "0".to_owned());
-        assert_eq!(format!("{:t}", 0u), "0".to_owned());
-        assert_eq!(format!("{:o}", 0u), "0".to_owned());
-        assert_eq!(format!("{:x}", 0u), "0".to_owned());
-        assert_eq!(format!("{:X}", 0u), "0".to_owned());
-    }
-
-    #[test]
-    fn test_format_int_flags() {
-        assert_eq!(format!("{:3d}", 1), "  1".to_owned());
-        assert_eq!(format!("{:>3d}", 1), "  1".to_owned());
-        assert_eq!(format!("{:>+3d}", 1), " +1".to_owned());
-        assert_eq!(format!("{:<3d}", 1), "1  ".to_owned());
-        assert_eq!(format!("{:#d}", 1), "1".to_owned());
-        assert_eq!(format!("{:#x}", 10), "0xa".to_owned());
-        assert_eq!(format!("{:#X}", 10), "0xA".to_owned());
-        assert_eq!(format!("{:#5x}", 10), "  0xa".to_owned());
-        assert_eq!(format!("{:#o}", 10), "0o12".to_owned());
-        assert_eq!(format!("{:08x}", 10), "0000000a".to_owned());
-        assert_eq!(format!("{:8x}", 10), "       a".to_owned());
-        assert_eq!(format!("{:<8x}", 10), "a       ".to_owned());
-        assert_eq!(format!("{:>8x}", 10), "       a".to_owned());
-        assert_eq!(format!("{:#08x}", 10), "0x00000a".to_owned());
-        assert_eq!(format!("{:08d}", -10), "-0000010".to_owned());
-        assert_eq!(format!("{:x}", -1u8), "ff".to_owned());
-        assert_eq!(format!("{:X}", -1u8), "FF".to_owned());
-        assert_eq!(format!("{:t}", -1u8), "11111111".to_owned());
-        assert_eq!(format!("{:o}", -1u8), "377".to_owned());
-        assert_eq!(format!("{:#x}", -1u8), "0xff".to_owned());
-        assert_eq!(format!("{:#X}", -1u8), "0xFF".to_owned());
-        assert_eq!(format!("{:#t}", -1u8), "0b11111111".to_owned());
-        assert_eq!(format!("{:#o}", -1u8), "0o377".to_owned());
-    }
-
-    #[test]
-    fn test_format_int_sign_padding() {
-        assert_eq!(format!("{:+5d}", 1), "   +1".to_owned());
-        assert_eq!(format!("{:+5d}", -1), "   -1".to_owned());
-        assert_eq!(format!("{:05d}", 1), "00001".to_owned());
-        assert_eq!(format!("{:05d}", -1), "-0001".to_owned());
-        assert_eq!(format!("{:+05d}", 1), "+0001".to_owned());
-        assert_eq!(format!("{:+05d}", -1), "-0001".to_owned());
-    }
-
-    #[test]
-    fn test_format_int_twos_complement() {
-        use {i8, i16, i32, i64};
-        assert_eq!(format!("{}", i8::MIN), "-128".to_owned());
-        assert_eq!(format!("{}", i16::MIN), "-32768".to_owned());
-        assert_eq!(format!("{}", i32::MIN), "-2147483648".to_owned());
-        assert_eq!(format!("{}", i64::MIN), "-9223372036854775808".to_owned());
-    }
-
-    #[test]
-    fn test_format_radix() {
-        assert_eq!(format!("{:04}", radix(3, 2)), "0011".to_owned());
-        assert_eq!(format!("{}", radix(55, 36)), "1j".to_owned());
-    }
-
-    #[test]
-    #[should_fail]
-    fn test_radix_base_too_large() {
-        let _ = radix(55, 37);
-    }
-}
-
-#[cfg(test)]
-mod bench {
-    extern crate test;
-
-    mod uint {
-        use super::test::Bencher;
-        use fmt::radix;
-        use rand::{XorShiftRng, Rng};
-        use realstd::result::ResultUnwrap;
-
-        #[bench]
-        fn format_bin(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:t}", rng.gen::<uint>()); })
-        }
-
-        #[bench]
-        fn format_oct(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:o}", rng.gen::<uint>()); })
-        }
-
-        #[bench]
-        fn format_dec(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:u}", rng.gen::<uint>()); })
-        }
-
-        #[bench]
-        fn format_hex(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:x}", rng.gen::<uint>()); })
-        }
-
-        #[bench]
-        fn format_base_36(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{}", radix(rng.gen::<uint>(), 36)); })
-        }
-    }
-
-    mod int {
-        use super::test::Bencher;
-        use fmt::radix;
-        use rand::{XorShiftRng, Rng};
-        use realstd::result::ResultUnwrap;
-
-        #[bench]
-        fn format_bin(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:t}", rng.gen::<int>()); })
-        }
-
-        #[bench]
-        fn format_oct(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:o}", rng.gen::<int>()); })
-        }
-
-        #[bench]
-        fn format_dec(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:d}", rng.gen::<int>()); })
-        }
-
-        #[bench]
-        fn format_hex(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{:x}", rng.gen::<int>()); })
-        }
-
-        #[bench]
-        fn format_base_36(b: &mut Bencher) {
-            let mut rng = XorShiftRng::new().unwrap();
-            b.iter(|| { format!("{}", radix(rng.gen::<int>(), 36)); })
-        }
-    }
-}
diff --git a/src/libstd/fmt/rt.rs b/src/libstd/fmt/rt.rs
deleted file mode 100644 (file)
index 00c8661..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! This is an internal module used by the ifmt! runtime. These structures are
-//! emitted to static arrays to precompile format strings ahead of time.
-//!
-//! These definitions are similar to their `ct` equivalents, but differ in that
-//! these can be statically allocated and are slightly optimized for the runtime
-
-#![allow(missing_doc)]
-#![doc(hidden)]
-
-use option::Option;
-
-pub enum Piece<'a> {
-    String(&'a str),
-    // FIXME(#8259): this shouldn't require the unit-value here
-    CurrentArgument(()),
-    Argument(Argument<'a>),
-}
-
-pub struct Argument<'a> {
-    pub position: Position,
-    pub format: FormatSpec,
-    pub method: Option<&'a Method<'a>>
-}
-
-pub struct FormatSpec {
-    pub fill: char,
-    pub align: Alignment,
-    pub flags: uint,
-    pub precision: Count,
-    pub width: Count,
-}
-
-#[deriving(Eq)]
-pub enum Alignment {
-    AlignLeft,
-    AlignRight,
-    AlignUnknown,
-}
-
-pub enum Count {
-    CountIs(uint), CountIsParam(uint), CountIsNextParam, CountImplied,
-}
-
-pub enum Position {
-    ArgumentNext, ArgumentIs(uint)
-}
-
-pub enum Flag {
-    FlagSignPlus,
-    FlagSignMinus,
-    FlagAlternate,
-    FlagSignAwareZeroPad,
-}
-
-pub enum Method<'a> {
-    Plural(Option<uint>, &'a [PluralArm<'a>], &'a [Piece<'a>]),
-    Select(&'a [SelectArm<'a>], &'a [Piece<'a>]),
-}
-
-pub enum PluralSelector {
-    Keyword(PluralKeyword),
-    Literal(uint),
-}
-
-pub enum PluralKeyword {
-    Zero,
-    One,
-    Two,
-    Few,
-    Many,
-}
-
-pub struct PluralArm<'a> {
-    pub selector: PluralSelector,
-    pub result: &'a [Piece<'a>],
-}
-
-pub struct SelectArm<'a> {
-    pub selector: &'a str,
-    pub result: &'a [Piece<'a>],
-}
index 68cbdd2e0aa4740c5c2bd457de0acba6d593075e..2880365cf348f5c6a15f63988af56ded515bb8b9 100644 (file)
@@ -16,7 +16,7 @@
 use iter::ExactSize;
 use ops::Drop;
 use option::{Some, None, Option};
-use result::{Ok, Err, ResultUnwrap};
+use result::{Ok, Err};
 use slice::{ImmutableVector, MutableVector};
 use slice;
 use vec::Vec;
index 5c9d5feab10360230f03497f8f41a85caba4e238..a043722581ba26d337d839bfed215c293b41a312 100644 (file)
@@ -381,9 +381,9 @@ pub fn last_error() -> IoError {
 
 impl fmt::Show for IoError {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
-        try!(fmt.buf.write_str(self.desc));
+        try!(write!(fmt, "{}", self.desc));
         match self.detail {
-            Some(ref s) => write!(fmt.buf, " ({})", *s),
+            Some(ref s) => write!(fmt, " ({})", *s),
             None => Ok(())
         }
     }
@@ -964,6 +964,42 @@ pub trait Writer {
     /// decide whether their stream needs to be buffered or not.
     fn flush(&mut self) -> IoResult<()> { Ok(()) }
 
+    /// Writes a formatted string into this writer, returning any error
+    /// encountered.
+    ///
+    /// This method is primarily used to interface with the `format_args!`
+    /// macro, but it is rare that this should explicitly be called. The
+    /// `write!` macro should be favored to invoke this method instead.
+    ///
+    /// # Errors
+    ///
+    /// This function will return any I/O error reported while formatting.
+    fn write_fmt(&mut self, fmt: &fmt::Arguments) -> IoResult<()> {
+        // Create a shim which translates a Writer to a FormatWriter and saves
+        // off I/O errors. instead of discarding them
+        struct Adaptor<'a, T> {
+            inner: &'a mut T,
+            error: IoResult<()>,
+        }
+        impl<'a, T: Writer> fmt::FormatWriter for Adaptor<'a, T> {
+            fn write(&mut self, bytes: &[u8]) -> fmt::Result {
+                match self.inner.write(bytes) {
+                    Ok(()) => Ok(()),
+                    Err(e) => {
+                        self.error = Err(e);
+                        Err(fmt::WriteError)
+                    }
+                }
+            }
+        }
+
+        let mut output = Adaptor { inner: self, error: Ok(()) };
+        match fmt::write(&mut output, fmt) {
+            Ok(()) => Ok(()),
+            Err(..) => output.error
+        }
+    }
+
     /// Write a rust string into this sink.
     ///
     /// The bytes written will be the UTF-8 encoded version of the input string.
index 7621a7ec4cd5efdc948a9d9e20438225a1f7429b..f469c419e8ef7962b0994a7a8f1c8082aed5fd1b 100644 (file)
@@ -35,22 +35,22 @@ impl fmt::Show for IpAddr {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             Ipv4Addr(a, b, c, d) =>
-                write!(fmt.buf, "{}.{}.{}.{}", a, b, c, d),
+                write!(fmt, "{}.{}.{}.{}", a, b, c, d),
 
             // Ipv4 Compatible address
             Ipv6Addr(0, 0, 0, 0, 0, 0, g, h) => {
-                write!(fmt.buf, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
+                write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
                        (h >> 8) as u8, h as u8)
             }
 
             // Ipv4-Mapped address
             Ipv6Addr(0, 0, 0, 0, 0, 0xFFFF, g, h) => {
-                write!(fmt.buf, "::FFFF:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
+                write!(fmt, "::FFFF:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
                        (h >> 8) as u8, h as u8)
             }
 
             Ipv6Addr(a, b, c, d, e, f, g, h) =>
-                write!(fmt.buf, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
+                write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
                        a, b, c, d, e, f, g, h)
         }
     }
@@ -65,8 +65,8 @@ pub struct SocketAddr {
 impl fmt::Show for SocketAddr {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self.ip {
-            Ipv4Addr(..) => write!(f.buf, "{}:{}", self.ip, self.port),
-            Ipv6Addr(..) => write!(f.buf, "[{}]:{}", self.ip, self.port),
+            Ipv4Addr(..) => write!(f, "{}:{}", self.ip, self.port),
+            Ipv6Addr(..) => write!(f, "[{}]:{}", self.ip, self.port),
         }
     }
 }
index 864a70105412ab30c2e75c6cb531ce751344a06f..875dd01be823bfcf6f99bb6235824a98e2f65b61 100644 (file)
@@ -384,8 +384,6 @@ mod test {
     })
 
     pub fn socket_name(addr: SocketAddr) {
-        use result::ResultUnwrap;
-
         let server = UdpSocket::bind(addr);
 
         assert!(server.is_ok());
index fe51615285a0081d9e622644501ccf08b391117a..fc760e6fe4ca312cd874369a7ac17df517b2dc1a 100644 (file)
@@ -308,9 +308,9 @@ impl fmt::Show for Command {
     /// non-utf8 data is lossily converted using the utf8 replacement
     /// character.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, "{}", str::from_utf8_lossy(self.program.as_bytes_no_nul())));
+        try!(write!(f, "{}", str::from_utf8_lossy(self.program.as_bytes_no_nul())));
         for arg in self.args.iter() {
-            try!(write!(f.buf, " '{}'", str::from_utf8_lossy(arg.as_bytes_no_nul())));
+            try!(write!(f, " '{}'", str::from_utf8_lossy(arg.as_bytes_no_nul())));
         }
         Ok(())
     }
@@ -361,8 +361,8 @@ impl fmt::Show for ProcessExit {
     /// Format a ProcessExit enum, to nicely present the information.
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
-            ExitStatus(code) =>  write!(f.buf, "exit code: {}", code),
-            ExitSignal(code) =>  write!(f.buf, "signal: {}", code),
+            ExitStatus(code) =>  write!(f, "exit code: {}", code),
+            ExitSignal(code) =>  write!(f, "signal: {}", code),
         }
     }
 }
index 69ba0fb20ee1c28c2b8b5bb05e1b93939e348e4c..e6d416164d008831ad2af668ba3154916f7298d5 100644 (file)
@@ -36,7 +36,7 @@
 use option::{Option, Some, None};
 use owned::Box;
 use prelude::drop;
-use result::{Ok, Err, ResultUnwrap};
+use result::{Ok, Err};
 use rt;
 use rt::local::Local;
 use rt::rtio::{DontClose, IoFactory, LocalIo, RtioFileStream, RtioTTY};
@@ -276,13 +276,13 @@ pub fn println(s: &str) {
 /// Similar to `print`, but takes a `fmt::Arguments` structure to be compatible
 /// with the `format_args!` macro.
 pub fn print_args(fmt: &fmt::Arguments) {
-    with_task_stdout(|io| fmt::write(io, fmt))
+    with_task_stdout(|io| write!(io, "{}", fmt))
 }
 
 /// Similar to `println`, but takes a `fmt::Arguments` structure to be
 /// compatible with the `format_args!` macro.
 pub fn println_args(fmt: &fmt::Arguments) {
-    with_task_stdout(|io| fmt::writeln(io, fmt))
+    with_task_stdout(|io| writeln!(io, "{}", fmt))
 }
 
 /// Representation of a reader of a standard input stream
index 87c4ef1046f1a490405395c7431bf4789b244ec6..119cd9aa2ca8848159a7dade3b5c94187a9ed6fd 100644 (file)
 pub use core::ptr;
 pub use core::raw;
 pub use core::tuple;
+pub use core::result;
 
 // Run tests with libgreen instead of libnative.
 //
@@ -218,7 +219,6 @@ fn start(argc: int, argv: **u8) -> int {
 
 /* Common data structures */
 
-pub mod result;
 pub mod option;
 
 /* Tasks and communication */
index 3a0e78b39d13dc3682f2663858c67e4477d598f2..b260f685a34777fbe488d49044d18bff81f64a51 100644 (file)
@@ -251,10 +251,17 @@ macro_rules! format_strbuf(
 /// write!(&mut w, "formatted {}", "arguments");
 /// ```
 #[macro_export]
+#[cfg(not(stage0))]
 macro_rules! write(
     ($dst:expr, $($arg:tt)*) => ({
-        let dst: &mut ::std::io::Writer = $dst;
-        format_args!(|args| { ::std::fmt::write(dst, args) }, $($arg)*)
+        format_args_method!($dst, write_fmt, $($arg)*)
+    })
+)
+#[cfg(stage0)]
+#[macro_export]
+macro_rules! write(
+    ($dst:expr, $($arg:tt)*) => ({
+        format_args!(|args| { $dst.write_fmt(args) }, $($arg)*)
     })
 )
 
@@ -262,10 +269,9 @@ macro_rules! write(
 /// the message is written.
 #[macro_export]
 macro_rules! writeln(
-    ($dst:expr, $($arg:tt)*) => ({
-        let dst: &mut ::std::io::Writer = $dst;
-        format_args!(|args| { ::std::fmt::writeln(dst, args) }, $($arg)*)
-    })
+    ($dst:expr, $fmt:expr $($arg:tt)*) => (
+        write!($dst, concat!($fmt, "\n") $($arg)*)
+    )
 )
 
 /// Equivalent to the `println!` macro except that a newline is not printed at
index 29c206b32fc36a89617360bfbadda42993166071..e9ea0df2a7b9e4f02b3706a9f6b849827bc75f51 100644 (file)
@@ -18,9 +18,7 @@
 use from_str::FromStr;
 use intrinsics;
 use libc::c_int;
-use mem;
 use num::strconv;
-use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
 use num;
 
 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
@@ -69,82 +67,7 @@ mod cmath {
     }
 }
 
-impl Float for f32 {
-    #[inline]
-    fn nan() -> f32 { NAN }
-
-    #[inline]
-    fn infinity() -> f32 { INFINITY }
-
-    #[inline]
-    fn neg_infinity() -> f32 { NEG_INFINITY }
-
-    #[inline]
-    fn neg_zero() -> f32 { -0.0 }
-
-    /// Returns `true` if the number is NaN
-    #[inline]
-    fn is_nan(self) -> bool { self != self }
-
-    /// Returns `true` if the number is infinite
-    #[inline]
-    fn is_infinite(self) -> bool {
-        self == Float::infinity() || self == Float::neg_infinity()
-    }
-
-    /// Returns `true` if the number is neither infinite or NaN
-    #[inline]
-    fn is_finite(self) -> bool {
-        !(self.is_nan() || self.is_infinite())
-    }
-
-    /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
-    #[inline]
-    fn is_normal(self) -> bool {
-        self.classify() == FPNormal
-    }
-
-    /// Returns the floating point category of the number. If only one property
-    /// is going to be tested, it is generally faster to use the specific
-    /// predicate instead.
-    fn classify(self) -> FPCategory {
-        static EXP_MASK: u32 = 0x7f800000;
-        static MAN_MASK: u32 = 0x007fffff;
-
-        let bits: u32 = unsafe { mem::transmute(self) };
-        match (bits & MAN_MASK, bits & EXP_MASK) {
-            (0, 0)        => FPZero,
-            (_, 0)        => FPSubnormal,
-            (0, EXP_MASK) => FPInfinite,
-            (_, EXP_MASK) => FPNaN,
-            _             => FPNormal,
-        }
-    }
-
-    #[inline]
-    fn mantissa_digits(_: Option<f32>) -> uint { MANTISSA_DIGITS }
-
-    #[inline]
-    fn digits(_: Option<f32>) -> uint { DIGITS }
-
-    #[inline]
-    fn epsilon() -> f32 { EPSILON }
-
-    #[inline]
-    fn min_exp(_: Option<f32>) -> int { MIN_EXP }
-
-    #[inline]
-    fn max_exp(_: Option<f32>) -> int { MAX_EXP }
-
-    #[inline]
-    fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP }
-
-    #[inline]
-    fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
-
-    #[inline]
-    fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
-
+impl FloatMath for f32 {
     /// Constructs a floating point number by multiplying `x` by 2 raised to the
     /// power of `exp`
     #[inline]
@@ -166,21 +89,6 @@ fn frexp(self) -> (f32, int) {
         }
     }
 
-    /// Returns the mantissa, exponent and sign as integers.
-    fn integer_decode(self) -> (u64, i16, i8) {
-        let bits: u32 = unsafe { mem::transmute(self) };
-        let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
-        let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
-        let mantissa = if exponent == 0 {
-            (bits & 0x7fffff) << 1
-        } else {
-            (bits & 0x7fffff) | 0x800000
-        };
-        // Exponent bias + mantissa shift
-        exponent -= 127 + 23;
-        (mantissa as u64, exponent, sign)
-    }
-
     /// Returns the next representable floating-point value in the direction of
     /// `other`.
     #[inline]
@@ -188,39 +96,6 @@ fn next_after(self, other: f32) -> f32 {
         unsafe { cmath::nextafterf(self, other) }
     }
 
-    /// Round half-way cases toward `NEG_INFINITY`
-    #[inline]
-    fn floor(self) -> f32 {
-        unsafe { intrinsics::floorf32(self) }
-    }
-
-    /// Round half-way cases toward `INFINITY`
-    #[inline]
-    fn ceil(self) -> f32 {
-        unsafe { intrinsics::ceilf32(self) }
-    }
-
-    /// Round half-way cases away from `0.0`
-    #[inline]
-    fn round(self) -> f32 {
-        unsafe { intrinsics::roundf32(self) }
-    }
-
-    /// The integer part of the number (rounds towards `0.0`)
-    #[inline]
-    fn trunc(self) -> f32 {
-        unsafe { intrinsics::truncf32(self) }
-    }
-
-    /// The fractional part of the number, satisfying:
-    ///
-    /// ```rust
-    /// let x = 1.65f32;
-    /// assert!(x == x.trunc() + x.fract())
-    /// ```
-    #[inline]
-    fn fract(self) -> f32 { self - self.trunc() }
-
     #[inline]
     fn max(self, other: f32) -> f32 {
         unsafe { cmath::fmaxf(self, other) }
@@ -231,43 +106,6 @@ fn min(self, other: f32) -> f32 {
         unsafe { cmath::fminf(self, other) }
     }
 
-    /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
-    /// error. This produces a more accurate result with better performance than
-    /// a separate multiplication operation followed by an add.
-    #[inline]
-    fn mul_add(self, a: f32, b: f32) -> f32 {
-        unsafe { intrinsics::fmaf32(self, a, b) }
-    }
-
-    /// The reciprocal (multiplicative inverse) of the number
-    #[inline]
-    fn recip(self) -> f32 { 1.0 / self }
-
-    fn powi(self, n: i32) -> f32 {
-        unsafe { intrinsics::powif32(self, n) }
-    }
-
-    #[inline]
-    fn powf(self, n: f32) -> f32 {
-        unsafe { intrinsics::powf32(self, n) }
-    }
-
-    /// sqrt(2.0)
-    #[inline]
-    fn sqrt2() -> f32 { consts::SQRT2 }
-
-    /// 1.0 / sqrt(2.0)
-    #[inline]
-    fn frac_1_sqrt2() -> f32 { consts::FRAC_1_SQRT2 }
-
-    #[inline]
-    fn sqrt(self) -> f32 {
-        unsafe { intrinsics::sqrtf32(self) }
-    }
-
-    #[inline]
-    fn rsqrt(self) -> f32 { self.sqrt().recip() }
-
     #[inline]
     fn cbrt(self) -> f32 {
         unsafe { cmath::cbrtf(self) }
@@ -278,46 +116,6 @@ fn hypot(self, other: f32) -> f32 {
         unsafe { cmath::hypotf(self, other) }
     }
 
-    /// Archimedes' constant
-    #[inline]
-    fn pi() -> f32 { consts::PI }
-
-    /// 2.0 * pi
-    #[inline]
-    fn two_pi() -> f32 { consts::PI_2 }
-
-    /// pi / 2.0
-    #[inline]
-    fn frac_pi_2() -> f32 { consts::FRAC_PI_2 }
-
-    /// pi / 3.0
-    #[inline]
-    fn frac_pi_3() -> f32 { consts::FRAC_PI_3 }
-
-    /// pi / 4.0
-    #[inline]
-    fn frac_pi_4() -> f32 { consts::FRAC_PI_4 }
-
-    /// pi / 6.0
-    #[inline]
-    fn frac_pi_6() -> f32 { consts::FRAC_PI_6 }
-
-    /// pi / 8.0
-    #[inline]
-    fn frac_pi_8() -> f32 { consts::FRAC_PI_8 }
-
-    /// 1 .0/ pi
-    #[inline]
-    fn frac_1_pi() -> f32 { consts::FRAC_1_PI }
-
-    /// 2.0 / pi
-    #[inline]
-    fn frac_2_pi() -> f32 { consts::FRAC_2_PI }
-
-    /// 2.0 / sqrt(pi)
-    #[inline]
-    fn frac_2_sqrtpi() -> f32 { consts::FRAC_2_SQRTPI }
-
     #[inline]
     fn sin(self) -> f32 {
         unsafe { intrinsics::sinf32(self) }
@@ -359,38 +157,6 @@ fn sin_cos(self) -> (f32, f32) {
         (self.sin(), self.cos())
     }
 
-    /// Euler's number
-    #[inline]
-    fn e() -> f32 { consts::E }
-
-    /// log2(e)
-    #[inline]
-    fn log2_e() -> f32 { consts::LOG2_E }
-
-    /// log10(e)
-    #[inline]
-    fn log10_e() -> f32 { consts::LOG10_E }
-
-    /// ln(2.0)
-    #[inline]
-    fn ln_2() -> f32 { consts::LN_2 }
-
-    /// ln(10.0)
-    #[inline]
-    fn ln_10() -> f32 { consts::LN_10 }
-
-    /// Returns the exponential of the number
-    #[inline]
-    fn exp(self) -> f32 {
-        unsafe { intrinsics::expf32(self) }
-    }
-
-    /// Returns 2 raised to the power of the number
-    #[inline]
-    fn exp2(self) -> f32 {
-        unsafe { intrinsics::exp2f32(self) }
-    }
-
     /// Returns the exponential of the number, minus `1`, in a way that is
     /// accurate even if the number is close to zero
     #[inline]
@@ -398,28 +164,6 @@ fn exp_m1(self) -> f32 {
         unsafe { cmath::expm1f(self) }
     }
 
-    /// Returns the natural logarithm of the number
-    #[inline]
-    fn ln(self) -> f32 {
-        unsafe { intrinsics::logf32(self) }
-    }
-
-    /// Returns the logarithm of the number with respect to an arbitrary base
-    #[inline]
-    fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
-
-    /// Returns the base 2 logarithm of the number
-    #[inline]
-    fn log2(self) -> f32 {
-        unsafe { intrinsics::log2f32(self) }
-    }
-
-    /// Returns the base 10 logarithm of the number
-    #[inline]
-    fn log10(self) -> f32 {
-        unsafe { intrinsics::log10f32(self) }
-    }
-
     /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
     /// accurately than if the operations were performed separately
     #[inline]
@@ -486,17 +230,6 @@ fn acosh(self) -> f32 {
     fn atanh(self) -> f32 {
         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
     }
-
-    /// Converts to degrees, assuming the number is in radians
-    #[inline]
-    fn to_degrees(self) -> f32 { self * (180.0f32 / Float::pi()) }
-
-    /// Converts to radians, assuming the number is in degrees
-    #[inline]
-    fn to_radians(self) -> f32 {
-        let value: f32 = Float::pi();
-        self * (value / 180.0f32)
-    }
 }
 
 //
@@ -1000,18 +733,18 @@ fn test_ldexp() {
         // are supported in floating-point literals
         let f1: f32 = from_str_hex("1p-123").unwrap();
         let f2: f32 = from_str_hex("1p-111").unwrap();
-        assert_eq!(Float::ldexp(1f32, -123), f1);
-        assert_eq!(Float::ldexp(1f32, -111), f2);
+        assert_eq!(FloatMath::ldexp(1f32, -123), f1);
+        assert_eq!(FloatMath::ldexp(1f32, -111), f2);
 
-        assert_eq!(Float::ldexp(0f32, -123), 0f32);
-        assert_eq!(Float::ldexp(-0f32, -123), -0f32);
+        assert_eq!(FloatMath::ldexp(0f32, -123), 0f32);
+        assert_eq!(FloatMath::ldexp(-0f32, -123), -0f32);
 
         let inf: f32 = Float::infinity();
         let neg_inf: f32 = Float::neg_infinity();
         let nan: f32 = Float::nan();
-        assert_eq!(Float::ldexp(inf, -123), inf);
-        assert_eq!(Float::ldexp(neg_inf, -123), neg_inf);
-        assert!(Float::ldexp(nan, -123).is_nan());
+        assert_eq!(FloatMath::ldexp(inf, -123), inf);
+        assert_eq!(FloatMath::ldexp(neg_inf, -123), neg_inf);
+        assert!(FloatMath::ldexp(nan, -123).is_nan());
     }
 
     #[test]
@@ -1024,8 +757,8 @@ fn test_frexp() {
         let (x2, exp2) = f2.frexp();
         assert_eq!((x1, exp1), (0.5f32, -122));
         assert_eq!((x2, exp2), (0.5f32, -110));
-        assert_eq!(Float::ldexp(x1, exp1), f1);
-        assert_eq!(Float::ldexp(x2, exp2), f2);
+        assert_eq!(FloatMath::ldexp(x1, exp1), f1);
+        assert_eq!(FloatMath::ldexp(x2, exp2), f2);
 
         assert_eq!(0f32.frexp(), (0f32, 0));
         assert_eq!((-0f32).frexp(), (-0f32, 0));
index c18ea5caba6298c3a1ce5fcefbc691262495137d..869a275b1d4089b57eb983bc99b599b54dd3f17a 100644 (file)
 
 use from_str::FromStr;
 use intrinsics;
-use libc::{c_int};
-use mem;
-use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
-use num::{strconv};
+use libc::c_int;
+use num::strconv;
 use num;
 
 pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
@@ -77,82 +75,7 @@ mod cmath {
     }
 }
 
-impl Float for f64 {
-    #[inline]
-    fn nan() -> f64 { NAN }
-
-    #[inline]
-    fn infinity() -> f64 { INFINITY }
-
-    #[inline]
-    fn neg_infinity() -> f64 { NEG_INFINITY }
-
-    #[inline]
-    fn neg_zero() -> f64 { -0.0 }
-
-    /// Returns `true` if the number is NaN
-    #[inline]
-    fn is_nan(self) -> bool { self != self }
-
-    /// Returns `true` if the number is infinite
-    #[inline]
-    fn is_infinite(self) -> bool {
-        self == Float::infinity() || self == Float::neg_infinity()
-    }
-
-    /// Returns `true` if the number is neither infinite or NaN
-    #[inline]
-    fn is_finite(self) -> bool {
-        !(self.is_nan() || self.is_infinite())
-    }
-
-    /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
-    #[inline]
-    fn is_normal(self) -> bool {
-        self.classify() == FPNormal
-    }
-
-    /// Returns the floating point category of the number. If only one property
-    /// is going to be tested, it is generally faster to use the specific
-    /// predicate instead.
-    fn classify(self) -> FPCategory {
-        static EXP_MASK: u64 = 0x7ff0000000000000;
-        static MAN_MASK: u64 = 0x000fffffffffffff;
-
-        let bits: u64 = unsafe { mem::transmute(self) };
-        match (bits & MAN_MASK, bits & EXP_MASK) {
-            (0, 0)        => FPZero,
-            (_, 0)        => FPSubnormal,
-            (0, EXP_MASK) => FPInfinite,
-            (_, EXP_MASK) => FPNaN,
-            _             => FPNormal,
-        }
-    }
-
-    #[inline]
-    fn mantissa_digits(_: Option<f64>) -> uint { MANTISSA_DIGITS }
-
-    #[inline]
-    fn digits(_: Option<f64>) -> uint { DIGITS }
-
-    #[inline]
-    fn epsilon() -> f64 { EPSILON }
-
-    #[inline]
-    fn min_exp(_: Option<f64>) -> int { MIN_EXP }
-
-    #[inline]
-    fn max_exp(_: Option<f64>) -> int { MAX_EXP }
-
-    #[inline]
-    fn min_10_exp(_: Option<f64>) -> int { MIN_10_EXP }
-
-    #[inline]
-    fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP }
-
-    #[inline]
-    fn min_pos_value(_: Option<f64>) -> f64 { MIN_POS_VALUE }
-
+impl FloatMath for f64 {
     /// Constructs a floating point number by multiplying `x` by 2 raised to the
     /// power of `exp`
     #[inline]
@@ -174,21 +97,6 @@ fn frexp(self) -> (f64, int) {
         }
     }
 
-    /// Returns the mantissa, exponent and sign as integers.
-    fn integer_decode(self) -> (u64, i16, i8) {
-        let bits: u64 = unsafe { mem::transmute(self) };
-        let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
-        let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
-        let mantissa = if exponent == 0 {
-            (bits & 0xfffffffffffff) << 1
-        } else {
-            (bits & 0xfffffffffffff) | 0x10000000000000
-        };
-        // Exponent bias + mantissa shift
-        exponent -= 1023 + 52;
-        (mantissa, exponent, sign)
-    }
-
     /// Returns the next representable floating-point value in the direction of
     /// `other`.
     #[inline]
@@ -196,39 +104,6 @@ fn next_after(self, other: f64) -> f64 {
         unsafe { cmath::nextafter(self, other) }
     }
 
-    /// Round half-way cases toward `NEG_INFINITY`
-    #[inline]
-    fn floor(self) -> f64 {
-        unsafe { intrinsics::floorf64(self) }
-    }
-
-    /// Round half-way cases toward `INFINITY`
-    #[inline]
-    fn ceil(self) -> f64 {
-        unsafe { intrinsics::ceilf64(self) }
-    }
-
-    /// Round half-way cases away from `0.0`
-    #[inline]
-    fn round(self) -> f64 {
-        unsafe { intrinsics::roundf64(self) }
-    }
-
-    /// The integer part of the number (rounds towards `0.0`)
-    #[inline]
-    fn trunc(self) -> f64 {
-        unsafe { intrinsics::truncf64(self) }
-    }
-
-    /// The fractional part of the number, satisfying:
-    ///
-    /// ```rust
-    /// let x = 1.65f64;
-    /// assert!(x == x.trunc() + x.fract())
-    /// ```
-    #[inline]
-    fn fract(self) -> f64 { self - self.trunc() }
-
     #[inline]
     fn max(self, other: f64) -> f64 {
         unsafe { cmath::fmax(self, other) }
@@ -239,44 +114,6 @@ fn min(self, other: f64) -> f64 {
         unsafe { cmath::fmin(self, other) }
     }
 
-    /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
-    /// error. This produces a more accurate result with better performance than
-    /// a separate multiplication operation followed by an add.
-    #[inline]
-    fn mul_add(self, a: f64, b: f64) -> f64 {
-        unsafe { intrinsics::fmaf64(self, a, b) }
-    }
-
-    /// The reciprocal (multiplicative inverse) of the number
-    #[inline]
-    fn recip(self) -> f64 { 1.0 / self }
-
-    #[inline]
-    fn powf(self, n: f64) -> f64 {
-        unsafe { intrinsics::powf64(self, n) }
-    }
-
-    #[inline]
-    fn powi(self, n: i32) -> f64 {
-        unsafe { intrinsics::powif64(self, n) }
-    }
-
-    /// sqrt(2.0)
-    #[inline]
-    fn sqrt2() -> f64 { consts::SQRT2 }
-
-    /// 1.0 / sqrt(2.0)
-    #[inline]
-    fn frac_1_sqrt2() -> f64 { consts::FRAC_1_SQRT2 }
-
-    #[inline]
-    fn sqrt(self) -> f64 {
-        unsafe { intrinsics::sqrtf64(self) }
-    }
-
-    #[inline]
-    fn rsqrt(self) -> f64 { self.sqrt().recip() }
-
     #[inline]
     fn cbrt(self) -> f64 {
         unsafe { cmath::cbrt(self) }
@@ -287,46 +124,6 @@ fn hypot(self, other: f64) -> f64 {
         unsafe { cmath::hypot(self, other) }
     }
 
-    /// Archimedes' constant
-    #[inline]
-    fn pi() -> f64 { consts::PI }
-
-    /// 2.0 * pi
-    #[inline]
-    fn two_pi() -> f64 { consts::PI_2 }
-
-    /// pi / 2.0
-    #[inline]
-    fn frac_pi_2() -> f64 { consts::FRAC_PI_2 }
-
-    /// pi / 3.0
-    #[inline]
-    fn frac_pi_3() -> f64 { consts::FRAC_PI_3 }
-
-    /// pi / 4.0
-    #[inline]
-    fn frac_pi_4() -> f64 { consts::FRAC_PI_4 }
-
-    /// pi / 6.0
-    #[inline]
-    fn frac_pi_6() -> f64 { consts::FRAC_PI_6 }
-
-    /// pi / 8.0
-    #[inline]
-    fn frac_pi_8() -> f64 { consts::FRAC_PI_8 }
-
-    /// 1.0 / pi
-    #[inline]
-    fn frac_1_pi() -> f64 { consts::FRAC_1_PI }
-
-    /// 2.0 / pi
-    #[inline]
-    fn frac_2_pi() -> f64 { consts::FRAC_2_PI }
-
-    /// 2.0 / sqrt(pi)
-    #[inline]
-    fn frac_2_sqrtpi() -> f64 { consts::FRAC_2_SQRTPI }
-
     #[inline]
     fn sin(self) -> f64 {
         unsafe { intrinsics::sinf64(self) }
@@ -368,38 +165,6 @@ fn sin_cos(self) -> (f64, f64) {
         (self.sin(), self.cos())
     }
 
-    /// Euler's number
-    #[inline]
-    fn e() -> f64 { consts::E }
-
-    /// log2(e)
-    #[inline]
-    fn log2_e() -> f64 { consts::LOG2_E }
-
-    /// log10(e)
-    #[inline]
-    fn log10_e() -> f64 { consts::LOG10_E }
-
-    /// ln(2.0)
-    #[inline]
-    fn ln_2() -> f64 { consts::LN_2 }
-
-    /// ln(10.0)
-    #[inline]
-    fn ln_10() -> f64 { consts::LN_10 }
-
-    /// Returns the exponential of the number
-    #[inline]
-    fn exp(self) -> f64 {
-        unsafe { intrinsics::expf64(self) }
-    }
-
-    /// Returns 2 raised to the power of the number
-    #[inline]
-    fn exp2(self) -> f64 {
-        unsafe { intrinsics::exp2f64(self) }
-    }
-
     /// Returns the exponential of the number, minus `1`, in a way that is
     /// accurate even if the number is close to zero
     #[inline]
@@ -407,28 +172,6 @@ fn exp_m1(self) -> f64 {
         unsafe { cmath::expm1(self) }
     }
 
-    /// Returns the natural logarithm of the number
-    #[inline]
-    fn ln(self) -> f64 {
-        unsafe { intrinsics::logf64(self) }
-    }
-
-    /// Returns the logarithm of the number with respect to an arbitrary base
-    #[inline]
-    fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
-
-    /// Returns the base 2 logarithm of the number
-    #[inline]
-    fn log2(self) -> f64 {
-        unsafe { intrinsics::log2f64(self) }
-    }
-
-    /// Returns the base 10 logarithm of the number
-    #[inline]
-    fn log10(self) -> f64 {
-        unsafe { intrinsics::log10f64(self) }
-    }
-
     /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
     /// accurately than if the operations were performed separately
     #[inline]
@@ -495,17 +238,6 @@ fn acosh(self) -> f64 {
     fn atanh(self) -> f64 {
         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
     }
-
-    /// Converts to degrees, assuming the number is in radians
-    #[inline]
-    fn to_degrees(self) -> f64 { self * (180.0f64 / Float::pi()) }
-
-    /// Converts to radians, assuming the number is in degrees
-    #[inline]
-    fn to_radians(self) -> f64 {
-        let value: f64 = Float::pi();
-        self * (value / 180.0)
-    }
 }
 
 //
@@ -1003,18 +735,18 @@ fn test_ldexp() {
         // are supported in floating-point literals
         let f1: f64 = from_str_hex("1p-123").unwrap();
         let f2: f64 = from_str_hex("1p-111").unwrap();
-        assert_eq!(Float::ldexp(1f64, -123), f1);
-        assert_eq!(Float::ldexp(1f64, -111), f2);
+        assert_eq!(FloatMath::ldexp(1f64, -123), f1);
+        assert_eq!(FloatMath::ldexp(1f64, -111), f2);
 
-        assert_eq!(Float::ldexp(0f64, -123), 0f64);
-        assert_eq!(Float::ldexp(-0f64, -123), -0f64);
+        assert_eq!(FloatMath::ldexp(0f64, -123), 0f64);
+        assert_eq!(FloatMath::ldexp(-0f64, -123), -0f64);
 
         let inf: f64 = Float::infinity();
         let neg_inf: f64 = Float::neg_infinity();
         let nan: f64 = Float::nan();
-        assert_eq!(Float::ldexp(inf, -123), inf);
-        assert_eq!(Float::ldexp(neg_inf, -123), neg_inf);
-        assert!(Float::ldexp(nan, -123).is_nan());
+        assert_eq!(FloatMath::ldexp(inf, -123), inf);
+        assert_eq!(FloatMath::ldexp(neg_inf, -123), neg_inf);
+        assert!(FloatMath::ldexp(nan, -123).is_nan());
     }
 
     #[test]
@@ -1027,8 +759,8 @@ fn test_frexp() {
         let (x2, exp2) = f2.frexp();
         assert_eq!((x1, exp1), (0.5f64, -122));
         assert_eq!((x2, exp2), (0.5f64, -110));
-        assert_eq!(Float::ldexp(x1, exp1), f1);
-        assert_eq!(Float::ldexp(x2, exp2), f2);
+        assert_eq!(FloatMath::ldexp(x1, exp1), f1);
+        assert_eq!(FloatMath::ldexp(x2, exp2), f2);
 
         assert_eq!(0f64.frexp(), (0f64, 0));
         assert_eq!((-0f64).frexp(), (-0f64, 0));
index d8f1c108b742d48dd8040b50dba5b578319d7450..396037d0dbace10cb6d897ee0e80252462441f22 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::i16::{BITS, BYTES, MIN, MAX};
 
index 9cc8981fc13f422c10d231fd413362088f54723d..5640e82d077dc3a95eb465d39aebb5d15300bb61 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::i32::{BITS, BYTES, MIN, MAX};
 
index 4f7fe32cc7088c9b7d3f171f258840d9e4bad851..40245691e3456fc118f094e261433dc058572905 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::i64::{BITS, BYTES, MIN, MAX};
 
index bea315d86837392e42a3236b646b7d747abfd36e..7ddddd893e211989cc76ccdb76cba11f28f13402 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::i8::{BITS, BYTES, MIN, MAX};
 
index d6a7fd1660b42800eb61a8731d6eb86413ba815a..dc4d80601b7db6a0116eab77f409f584347e4946 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::int::{BITS, BYTES, MIN, MAX};
 
index fcdb63f5ad5eb06564fadbc26af502becd07bc3d..ddff42f68dba9fe870fb49fbfb9c89586d692eab 100644 (file)
@@ -62,31 +62,23 @@ fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
 /// ```
 #[inline]
 pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
+    use io::{Writer, Seek};
     // The radix can be as low as 2, so we need at least 64 characters for a
     // base 2 number, and then we need another for a possible '-' character.
     let mut buf = [0u8, ..65];
-    let mut cur = 0;
-    strconv::int_to_str_bytes_common(n, radix, strconv::SignNeg, |i| {
-        buf[cur] = i;
-        cur += 1;
-    });
-    f(buf.slice(0, cur))
+    let amt = {
+        let mut wr = ::io::BufWriter::new(buf);
+        (write!(&mut wr, "{}", ::fmt::radix(n, radix as u8))).unwrap();
+        wr.tell().unwrap() as uint
+    };
+    f(buf.slice(0, amt))
 }
 
 impl ToStrRadix for $T {
     /// Convert to a string in a given base.
     #[inline]
     fn to_str_radix(&self, radix: uint) -> ~str {
-        use slice::Vector;
-        use str::StrAllocating;
-
-        let mut buf = ::vec::Vec::new();
-        strconv::int_to_str_bytes_common(*self, radix, strconv::SignNeg, |i| {
-            buf.push(i);
-        });
-        // We know we generated valid utf-8, so we don't need to go through that
-        // check.
-        unsafe { str::raw::from_utf8(buf.as_slice()).to_owned() }
+        format!("{}", ::fmt::radix(*self, radix as u8))
     }
 }
 
index 1efd7cad300bfca9b0144c0ba89ee4bfa11a58fe..3178fcbd66fdb8958e423a352f41942b134f2e33 100644 (file)
 pub use core::num::{from_int, from_i8, from_i16, from_i32, from_i64};
 pub use core::num::{from_uint, from_u8, from_u16, from_u32, from_u64};
 pub use core::num::{from_f32, from_f64};
+pub use core::num::{FPCategory, FPNaN, FPInfinite, FPZero, FPSubnormal};
+pub use core::num::{FPNormal, Float};
 
 pub mod strconv;
 
-/// Used for representing the classification of floating point numbers
-#[deriving(Eq, Show)]
-pub enum FPCategory {
-    /// "Not a Number", often obtained by dividing by zero
-    FPNaN,
-    /// Positive or negative infinity
-    FPInfinite ,
-    /// Positive or negative zero
-    FPZero,
-    /// De-normalized floating point representation (less precise than `FPNormal`)
-    FPSubnormal,
-    /// A regular floating point number
-    FPNormal,
-}
-
-/// Operations on primitive floating point numbers.
-// FIXME(#5527): In a future version of Rust, many of these functions will
-//               become constants.
-//
-// FIXME(#8888): Several of these functions have a parameter named
-//               `unused_self`. Removing it requires #8888 to be fixed.
-pub trait Float: Signed + Primitive {
-    /// Returns the NaN value.
-    fn nan() -> Self;
-    /// Returns the infinite value.
-    fn infinity() -> Self;
-    /// Returns the negative infinite value.
-    fn neg_infinity() -> Self;
-    /// Returns -0.0.
-    fn neg_zero() -> Self;
-
-    /// Returns true if this value is NaN and false otherwise.
-    fn is_nan(self) -> bool;
-    /// Returns true if this value is positive infinity or negative infinity and
-    /// false otherwise.
-    fn is_infinite(self) -> bool;
-    /// Returns true if this number is neither infinite nor NaN.
-    fn is_finite(self) -> bool;
-    /// Returns true if this number is neither zero, infinite, denormal, or NaN.
-    fn is_normal(self) -> bool;
-    /// Returns the category that this number falls into.
-    fn classify(self) -> FPCategory;
-
-    // FIXME (#5527): These should be associated constants
-
-    /// Returns the number of binary digits of mantissa that this type supports.
-    fn mantissa_digits(unused_self: Option<Self>) -> uint;
-    /// Returns the number of base-10 digits of precision that this type supports.
-    fn digits(unused_self: Option<Self>) -> uint;
-    /// Returns the difference between 1.0 and the smallest representable number larger than 1.0.
-    fn epsilon() -> Self;
-    /// Returns the minimum binary exponent that this type can represent.
-    fn min_exp(unused_self: Option<Self>) -> int;
-    /// Returns the maximum binary exponent that this type can represent.
-    fn max_exp(unused_self: Option<Self>) -> int;
-    /// Returns the minimum base-10 exponent that this type can represent.
-    fn min_10_exp(unused_self: Option<Self>) -> int;
-    /// Returns the maximum base-10 exponent that this type can represent.
-    fn max_10_exp(unused_self: Option<Self>) -> int;
-    /// Returns the smallest normalized positive number that this type can represent.
-    fn min_pos_value(unused_self: Option<Self>) -> Self;
-
+/// Mathematical operations on primitive floating point numbers.
+pub trait FloatMath: Float {
     /// Constructs a floating point number created by multiplying `x` by 2
     /// raised to the power of `exp`.
     fn ldexp(x: Self, exp: int) -> Self;
@@ -105,82 +47,22 @@ pub trait Float: Signed + Primitive {
     ///
     ///  * `0.5 <= abs(x) < 1.0`
     fn frexp(self) -> (Self, int);
-    /// Returns the mantissa, exponent and sign as integers, respectively.
-    fn integer_decode(self) -> (u64, i16, i8);
 
     /// Returns the next representable floating-point value in the direction of
     /// `other`.
     fn next_after(self, other: Self) -> Self;
 
-    /// Return the largest integer less than or equal to a number.
-    fn floor(self) -> Self;
-    /// Return the smallest integer greater than or equal to a number.
-    fn ceil(self) -> Self;
-    /// Return the nearest integer to a number. Round half-way cases away from
-    /// `0.0`.
-    fn round(self) -> Self;
-    /// Return the integer part of a number.
-    fn trunc(self) -> Self;
-    /// Return the fractional part of a number.
-    fn fract(self) -> Self;
-
     /// Returns the maximum of the two numbers.
     fn max(self, other: Self) -> Self;
     /// Returns the minimum of the two numbers.
     fn min(self, other: Self) -> Self;
 
-    /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
-    /// error. This produces a more accurate result with better performance than
-    /// a separate multiplication operation followed by an add.
-    fn mul_add(self, a: Self, b: Self) -> Self;
-    /// Take the reciprocal (inverse) of a number, `1/x`.
-    fn recip(self) -> Self;
-
-    /// Raise a number to an integer power.
-    ///
-    /// Using this function is generally faster than using `powf`
-    fn powi(self, n: i32) -> Self;
-    /// Raise a number to a floating point power.
-    fn powf(self, n: Self) -> Self;
-
-    /// sqrt(2.0).
-    fn sqrt2() -> Self;
-    /// 1.0 / sqrt(2.0).
-    fn frac_1_sqrt2() -> Self;
-
-    /// Take the square root of a number.
-    fn sqrt(self) -> Self;
-    /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
-    fn rsqrt(self) -> Self;
     /// Take the cubic root of a number.
     fn cbrt(self) -> Self;
     /// Calculate the length of the hypotenuse of a right-angle triangle given
     /// legs of length `x` and `y`.
     fn hypot(self, other: Self) -> Self;
 
-    // FIXME (#5527): These should be associated constants
-
-    /// Archimedes' constant.
-    fn pi() -> Self;
-    /// 2.0 * pi.
-    fn two_pi() -> Self;
-    /// pi / 2.0.
-    fn frac_pi_2() -> Self;
-    /// pi / 3.0.
-    fn frac_pi_3() -> Self;
-    /// pi / 4.0.
-    fn frac_pi_4() -> Self;
-    /// pi / 6.0.
-    fn frac_pi_6() -> Self;
-    /// pi / 8.0.
-    fn frac_pi_8() -> Self;
-    /// 1.0 / pi.
-    fn frac_1_pi() -> Self;
-    /// 2.0 / pi.
-    fn frac_2_pi() -> Self;
-    /// 2.0 / sqrt(pi).
-    fn frac_2_sqrtpi() -> Self;
-
     /// Computes the sine of a number (in radians).
     fn sin(self) -> Self;
     /// Computes the cosine of a number (in radians).
@@ -206,32 +88,9 @@ pub trait Float: Signed + Primitive {
     /// `(sin(x), cos(x))`.
     fn sin_cos(self) -> (Self, Self);
 
-    /// Euler's number.
-    fn e() -> Self;
-    /// log2(e).
-    fn log2_e() -> Self;
-    /// log10(e).
-    fn log10_e() -> Self;
-    /// ln(2.0).
-    fn ln_2() -> Self;
-    /// ln(10.0).
-    fn ln_10() -> Self;
-
-    /// Returns `e^(self)`, (the exponential function).
-    fn exp(self) -> Self;
-    /// Returns 2 raised to the power of the number, `2^(self)`.
-    fn exp2(self) -> Self;
     /// Returns the exponential of the number, minus 1, in a way that is
     /// accurate even if the number is close to zero.
     fn exp_m1(self) -> Self;
-    /// Returns the natural logarithm of the number.
-    fn ln(self) -> Self;
-    /// Returns the logarithm of the number with respect to an arbitrary base.
-    fn log(self, base: Self) -> Self;
-    /// Returns the base 2 logarithm of the number.
-    fn log2(self) -> Self;
-    /// Returns the base 10 logarithm of the number.
-    fn log10(self) -> Self;
     /// Returns the natural logarithm of the number plus 1 (`ln(1+n)`) more
     /// accurately than if the operations were performed separately.
     fn ln_1p(self) -> Self;
@@ -248,11 +107,6 @@ pub trait Float: Signed + Primitive {
     fn acosh(self) -> Self;
     /// Inverse hyperbolic tangent function.
     fn atanh(self) -> Self;
-
-    /// Convert radians to degrees.
-    fn to_degrees(self) -> Self;
-    /// Convert degrees to radians.
-    fn to_radians(self) -> Self;
 }
 
 /// A generic trait for converting a value to a string with a radix (base)
index 63d6219ab8af5fce6bb44ad756fcc3bdbbe788d7..e58872b8395a6c388cc974eb83aa007c2bd3de6e 100644 (file)
@@ -19,7 +19,6 @@
 use num;
 use ops::{Add, Sub, Mul, Div, Rem, Neg};
 use option::{None, Option, Some};
-use result::ResultUnwrap;
 use slice::{CloneableVector, ImmutableVector, MutableVector};
 use std::cmp::{Ord, Eq};
 use str::{StrAllocating, StrSlice};
@@ -170,6 +169,7 @@ impl NumStrConv for $t {
  * # Failure
  * - Fails if `radix` < 2 or `radix` > 36.
  */
+#[deprecated = "format!() and friends should be favored instead"]
 pub fn int_to_str_bytes_common<T: Int>(num: T, radix: uint, sign: SignFormat, f: |u8|) {
     assert!(2 <= radix && radix <= 36);
 
@@ -258,6 +258,7 @@ pub fn int_to_str_bytes_common<T: Int>(num: T, radix: uint, sign: SignFormat, f:
  * - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict
  *   between digit and exponent sign `'p'`.
  */
+#[allow(deprecated)]
 pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+
                                   Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
         num: T, radix: uint, negative_zero: bool,
@@ -820,7 +821,6 @@ mod uint {
         use super::test::Bencher;
         use rand::{XorShiftRng, Rng};
         use num::ToStrRadix;
-        use realstd::result::ResultUnwrap;
 
         #[bench]
         fn to_str_bin(b: &mut Bencher) {
@@ -857,7 +857,6 @@ mod int {
         use super::test::Bencher;
         use rand::{XorShiftRng, Rng};
         use num::ToStrRadix;
-        use realstd::result::ResultUnwrap;
 
         #[bench]
         fn to_str_bin(b: &mut Bencher) {
@@ -894,7 +893,6 @@ mod f64 {
         use super::test::Bencher;
         use rand::{XorShiftRng, Rng};
         use f64;
-        use realstd::result::ResultUnwrap;
 
         #[bench]
         fn float_to_str(b: &mut Bencher) {
index 5c93ca6c36b79748b8fba3a704eb353d4dd0feaa..65ac46af5aa0c80bdbc780119f221a34f1ef142b 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::u16::{BITS, BYTES, MIN, MAX};
 
index 436eae7cd14d0c839acb9f0a0a617560acaeb478..d549e4d0d636253aee6d7398f4f940344d6b32be 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::u32::{BITS, BYTES, MIN, MAX};
 
index c654d6fbe3133df7e4e6cf36e0d4a58939a36741..3773e56f4d16df1f00d51025911947936fb8b9cf 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::u64::{BITS, BYTES, MIN, MAX};
 
index 7051b9191be8158ba668ec23d3b079068383b5ce..372e38d66521f1173e33e3e8e9e3ebb87db25839 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::u8::{BITS, BYTES, MIN, MAX};
 
index d1c3e96b2c987d7334b82dd6d3dc0b70d88141e1..c419276fa24518024d4494e495f70e34ead2c155 100644 (file)
@@ -15,7 +15,6 @@
 use num::strconv;
 use option::Option;
 use slice::ImmutableVector;
-use str;
 
 pub use core::uint::{BITS, BYTES, MIN, MAX};
 
index 0795238a49cff9578865f34f431f431575219533..7977c647606775f695f4a84480a8dce5dfe9df46 100644 (file)
@@ -63,31 +63,23 @@ fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
 /// ```
 #[inline]
 pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
+    use io::{Writer, Seek};
     // The radix can be as low as 2, so we need at least 64 characters for a
-    // base 2 number.
-    let mut buf = [0u8, ..64];
-    let mut cur = 0;
-    strconv::int_to_str_bytes_common(n, radix, strconv::SignNone, |i| {
-        buf[cur] = i;
-        cur += 1;
-    });
-    f(buf.slice(0, cur))
+    // base 2 number, and then we need another for a possible '-' character.
+    let mut buf = [0u8, ..65];
+    let amt = {
+        let mut wr = ::io::BufWriter::new(buf);
+        (write!(&mut wr, "{}", ::fmt::radix(n, radix as u8))).unwrap();
+        wr.tell().unwrap() as uint
+    };
+    f(buf.slice(0, amt))
 }
 
 impl ToStrRadix for $T {
     /// Convert to a string in a given base.
     #[inline]
     fn to_str_radix(&self, radix: uint) -> ~str {
-        use slice::Vector;
-        use str::StrAllocating;
-
-        let mut buf = ::vec::Vec::new();
-        strconv::int_to_str_bytes_common(*self, radix, strconv::SignNone, |i| {
-            buf.push(i);
-        });
-        // We know we generated valid utf-8, so we don't need to go through that
-        // check.
-        unsafe { str::raw::from_utf8(buf.as_slice()).to_owned() }
+        format!("{}", ::fmt::radix(*self, radix as u8))
     }
 }
 
index 88081d90b40013d55c8e37acb74e118e65659de7..a4705b78caab7419c9a53e3727c095a9dcfc2340 100644 (file)
@@ -1073,19 +1073,19 @@ fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
             ErrAlreadyExists => "File mapping for specified file already exists",
             ErrZeroLength => "Zero-length mapping not allowed",
             ErrUnknown(code) => {
-                return write!(out.buf, "Unknown error = {}", code)
+                return write!(out, "Unknown error = {}", code)
             },
             ErrVirtualAlloc(code) => {
-                return write!(out.buf, "VirtualAlloc failure = {}", code)
+                return write!(out, "VirtualAlloc failure = {}", code)
             },
             ErrCreateFileMappingW(code) => {
-                return write!(out.buf, "CreateFileMappingW failure = {}", code)
+                return write!(out, "CreateFileMappingW failure = {}", code)
             },
             ErrMapViewOfFile(code) => {
-                return write!(out.buf, "MapViewOfFile failure = {}", code)
+                return write!(out, "MapViewOfFile failure = {}", code)
             }
         };
-        write!(out.buf, "{}", str)
+        write!(out, "{}", str)
     }
 }
 
index 3af12c5154c29d5f08847f5ca209ff53f477781d..bd6684b390572b217e717aff2305146e30288239 100644 (file)
@@ -14,6 +14,7 @@
 use clone::Clone;
 use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
 use default::Default;
+use fmt;
 use intrinsics;
 use mem;
 use raw::TraitObject;
@@ -99,3 +100,16 @@ fn move<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
         }
     }
 }
+
+impl<T: fmt::Show> fmt::Show for Box<T> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        (**self).fmt(f)
+    }
+}
+
+#[cfg(not(stage0))]
+impl fmt::Show for Box<Any> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        f.pad("Box<Any>")
+    }
+}
index 6cd9e96496fe0ae85fb3557435cd3df7cffe3e91..e39d8d34447a465e4eb54e8aaa91e762fb50e026 100644 (file)
 pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
 pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
 pub use num::{Signed, Unsigned};
-pub use num::{Primitive, Int, Float, ToPrimitive, FromPrimitive};
+pub use num::{Primitive, Int, Float, FloatMath, ToPrimitive, FromPrimitive};
 pub use option::Expect;
 pub use owned::Box;
 pub use path::{GenericPath, Path, PosixPath, WindowsPath};
 pub use ptr::RawPtr;
 pub use io::{Buffer, Writer, Reader, Seek};
-pub use result::{ResultUnwrap, ResultUnwrapErr};
 pub use str::{Str, StrVector, StrSlice, OwnedStr, IntoMaybeOwned};
 pub use str::{StrAllocating};
 pub use to_str::{ToStr, IntoStr};
index 6029f504d10b62a5b426e935f628d852f011282f..35f32d0872802d3e3b8ad11b869d08022c5ba4d2 100644 (file)
@@ -25,7 +25,7 @@
 use ptr::RawPtr;
 use reflect;
 use reflect::{MovePtr, align};
-use result::{Ok, Err, ResultUnwrap};
+use result::{Ok, Err};
 use str::StrSlice;
 use to_str::ToStr;
 use slice::Vector;
diff --git a/src/libstd/result.rs b/src/libstd/result.rs
deleted file mode 100644 (file)
index ecbc164..0000000
+++ /dev/null
@@ -1,312 +0,0 @@
-// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! Error handling with the `Result` type
-//!
-//! `Result<T>` is the type used for returning and propagating
-//! errors. It is an enum with the variants, `Ok(T)`, representing
-//! success and containing a value, and `Err(E)`, representing error
-//! and containing an error value.
-//!
-//! ~~~
-//! enum Result<T, E> {
-//!    Ok(T),
-//!    Err(E)
-//! }
-//! ~~~
-//!
-//! Functions return `Result` whenever errors are expected and
-//! recoverable. In the `std` crate `Result` is most prominently used
-//! for [I/O](../io/index.html).
-//!
-//! A simple function returning `Result` might be
-//! defined and used like so:
-//!
-//! ~~~
-//! #[deriving(Show)]
-//! enum Version { Version1, Version2 }
-//!
-//! fn parse_version(header: &[u8]) -> Result<Version, &'static str> {
-//!     if header.len() < 1 {
-//!         return Err("invalid header length");
-//!     }
-//!     match header[0] {
-//!         1 => Ok(Version1),
-//!         2 => Ok(Version2),
-//!         _ => Err("invalid version")
-//!     }
-//! }
-//!
-//! let version = parse_version(&[1, 2, 3, 4]);
-//! match version {
-//!     Ok(v) => {
-//!         println!("working with version: {}", v);
-//!     }
-//!     Err(e) => {
-//!         println!("error parsing header: {}", e);
-//!     }
-//! }
-//! ~~~
-//!
-//! Pattern matching on `Result`s is clear and straightforward for
-//! simple cases, but `Result` comes with some convenience methods
-//! that make working it more succinct.
-//!
-//! ~~~
-//! let good_result: Result<int, int> = Ok(10);
-//! let bad_result: Result<int, int> = Err(10);
-//!
-//! // The `is_ok` and `is_err` methods do what they say.
-//! assert!(good_result.is_ok() && !good_result.is_err());
-//! assert!(bad_result.is_err() && !bad_result.is_ok());
-//!
-//! // `map` consumes the `Result` and produces another.
-//! let good_result: Result<int, int> = good_result.map(|i| i + 1);
-//! let bad_result: Result<int, int> = bad_result.map(|i| i - 1);
-//!
-//! // Use `and_then` to continue the computation.
-//! let good_result: Result<bool, int> = good_result.and_then(|i| Ok(i == 11));
-//!
-//! // Use `or_else` to handle the error.
-//! let bad_result: Result<int, int> = bad_result.or_else(|i| Ok(11));
-//!
-//! // Consume the result and return the contents with `unwrap`.
-//! let final_awesome_result = good_result.ok().unwrap();
-//! ~~~
-//!
-//! # Results must be used
-//!
-//! A common problem with using return values to indicate errors is
-//! that it is easy to ignore the return value, thus failing to handle
-//! the error. Result is annotated with the #[must_use] attribute,
-//! which will cause the compiler to issue a warning when a Result
-//! value is ignored. This makes `Result` especially useful with
-//! functions that may encounter errors but don't otherwise return a
-//! useful value.
-//!
-//! Consider the `write_line` method defined for I/O types
-//! by the [`Writer`](../io/trait.Writer.html) trait:
-//!
-//! ~~~
-//! use std::io::IoError;
-//!
-//! trait Writer {
-//!     fn write_line(&mut self, s: &str) -> Result<(), IoError>;
-//! }
-//! ~~~
-//!
-//! *Note: The actual definition of `Writer` uses `IoResult`, which
-//! is just a synonym for `Result<T, IoError>`.*
-//!
-//! This method doesn`t produce a value, but the write may
-//! fail. It's crucial to handle the error case, and *not* write
-//! something like this:
-//!
-//! ~~~ignore
-//! use std::io::{File, Open, Write};
-//!
-//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
-//! // If `write_line` errors, then we'll never know, because the return
-//! // value is ignored.
-//! file.write_line("important message");
-//! drop(file);
-//! ~~~
-//!
-//! If you *do* write that in Rust, the compiler will by give you a
-//! warning (by default, controlled by the `unused_must_use` lint).
-//!
-//! You might instead, if you don't want to handle the error, simply
-//! fail, by converting to an `Option` with `ok`, then asserting
-//! success with `expect`. This will fail if the write fails, proving
-//! a marginally useful message indicating why:
-//!
-//! ~~~no_run
-//! use std::io::{File, Open, Write};
-//!
-//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
-//! file.write_line("important message").ok().expect("failed to write message");
-//! drop(file);
-//! ~~~
-//!
-//! You might also simply assert success:
-//!
-//! ~~~no_run
-//! # use std::io::{File, Open, Write};
-//!
-//! # let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
-//! assert!(file.write_line("important message").is_ok());
-//! # drop(file);
-//! ~~~
-//!
-//! Or propagate the error up the call stack with `try!`:
-//!
-//! ~~~
-//! # use std::io::{File, Open, Write, IoError};
-//! fn write_message() -> Result<(), IoError> {
-//!     let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
-//!     try!(file.write_line("important message"));
-//!     drop(file);
-//!     return Ok(());
-//! }
-//! ~~~
-//!
-//! # The `try!` macro
-//!
-//! When writing code that calls many functions that return the
-//! `Result` type, the error handling can be tedious.  The `try!`
-//! macro hides some of the boilerplate of propagating errors up the
-//! call stack.
-//!
-//! It replaces this:
-//!
-//! ~~~
-//! use std::io::{File, Open, Write, IoError};
-//!
-//! struct Info { name: ~str, age: int, rating: int }
-//!
-//! fn write_info(info: &Info) -> Result<(), IoError> {
-//!     let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write);
-//!     // Early return on error
-//!     match file.write_line(format!("name: {}", info.name)) {
-//!         Ok(_) => (),
-//!         Err(e) => return Err(e)
-//!     }
-//!     match file.write_line(format!("age: {}", info.age)) {
-//!         Ok(_) => (),
-//!         Err(e) => return Err(e)
-//!     }
-//!     return file.write_line(format!("rating: {}", info.rating));
-//! }
-//! ~~~
-//!
-//! With this:
-//!
-//! ~~~
-//! use std::io::{File, Open, Write, IoError};
-//!
-//! struct Info { name: ~str, age: int, rating: int }
-//!
-//! fn write_info(info: &Info) -> Result<(), IoError> {
-//!     let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write);
-//!     // Early return on error
-//!     try!(file.write_line(format!("name: {}", info.name)));
-//!     try!(file.write_line(format!("age: {}", info.age)));
-//!     try!(file.write_line(format!("rating: {}", info.rating)));
-//!     return Ok(());
-//! }
-//! ~~~
-//!
-//! *It's much nicer!*
-//!
-//! Wrapping an expression in `try!` will result in the unwrapped
-//! success (`Ok`) value, unless the result is `Err`, in which case
-//! `Err` is returned early from the enclosing function. Its simple definition
-//! makes it clear:
-//!
-//! ~~~
-//! # #![feature(macro_rules)]
-//! macro_rules! try(
-//!     ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
-//! )
-//! # fn main() { }
-//! ~~~
-//!
-//! `try!` is imported by the prelude, and is available everywhere.
-//!
-//! # `Result` and `Option`
-//!
-//! The `Result` and [`Option`](../option/index.html) types are
-//! similar and complementary: they are often employed to indicate a
-//! lack of a return value; and they are trivially converted between
-//! each other, so `Result`s are often handled by first converting to
-//! `Option` with the [`ok`](../../core/result/enum.Result.html#method.ok) and
-//! [`err`](../../core/result/enum.Result.html#method.ok) methods.
-//!
-//! Whereas `Option` only indicates the lack of a value, `Result` is
-//! specifically for error reporting, and carries with it an error
-//! value.  Sometimes `Option` is used for indicating errors, but this
-//! is only for simple cases and is generally discouraged. Even when
-//! there is no useful error value to return, prefer `Result<T, ()>`.
-//!
-//! Converting to an `Option` with `ok()` to handle an error:
-//!
-//! ~~~
-//! use std::io::Timer;
-//! let mut t = Timer::new().ok().expect("failed to create timer!");
-//! ~~~
-//!
-//! # `Result` vs. `fail!`
-//!
-//! `Result` is for recoverable errors; `fail!` is for unrecoverable
-//! errors. Callers should always be able to avoid failure if they
-//! take the proper precautions, for example, calling `is_some()`
-//! on an `Option` type before calling `unwrap`.
-//!
-//! The suitability of `fail!` as an error handling mechanism is
-//! limited by Rust's lack of any way to "catch" and resume execution
-//! from a thrown exception. Therefore using failure for error
-//! handling requires encapsulating fallable code in a task. Calling
-//! the `fail!` macro, or invoking `fail!` indirectly should be
-//! avoided as an error reporting strategy. Failure is only for
-//! unrecoverable errors and a failing task is typically the sign of
-//! a bug.
-//!
-//! A module that instead returns `Results` is alerting the caller
-//! that failure is possible, and providing precise control over how
-//! it is handled.
-//!
-//! Furthermore, failure may not be recoverable at all, depending on
-//! the context. The caller of `fail!` should assume that execution
-//! will not resume after failure, that failure is catastrophic.
-
-use fmt::Show;
-
-pub use core::result::{Result, Ok, Err, collect, fold, fold_};
-
-// FIXME: These traits should not exist. Once std::fmt is moved to libcore,
-//        these can once again become inherent methods on Result.
-
-/// Temporary trait for unwrapping a result
-pub trait ResultUnwrap<T, E> {
-    /// Unwraps a result, yielding the content of an `Ok`.
-    ///
-    /// Fails if the value is an `Err`.
-    fn unwrap(self) -> T;
-}
-
-/// Temporary trait for unwrapping the error of a result
-pub trait ResultUnwrapErr<T, E> {
-    /// Unwraps a result, yielding the content of an `Err`.
-    ///
-    /// Fails if the value is an `Ok`.
-    fn unwrap_err(self) -> E;
-}
-
-impl<T, E: Show> ResultUnwrap<T, E> for Result<T, E> {
-    #[inline]
-    fn unwrap(self) -> T {
-        match self {
-            Ok(t) => t,
-            Err(e) =>
-                fail!("called `Result::unwrap()` on an `Err` value: {}", e)
-        }
-    }
-}
-
-impl<T: Show, E> ResultUnwrapErr<T, E> for Result<T, E> {
-    #[inline]
-    fn unwrap_err(self) -> E {
-        match self {
-            Ok(t) =>
-                fail!("called `Result::unwrap_err()` on an `Ok` value: {}", t),
-            Err(e) => e
-        }
-    }
-}
index e10e0716f67f2bea6c58168c7289280deb30f9f8..1cc513825a707e50831692cd7af452d8b70cb442 100644 (file)
@@ -295,24 +295,12 @@ pub extern "C" fn rust_eh_personality_catch(
     }
 }
 
-#[cold]
-#[no_mangle]
-#[cfg(not(test))]
-pub extern fn rust_fail_bounds_check(file: *u8, line: uint,
-                                     index: uint, len: uint) -> ! {
-    use str::raw::c_str_to_static_slice;
-
-    let msg = format!("index out of bounds: the len is {} but the index is {}",
-                      len as uint, index as uint);
-    begin_unwind(msg, unsafe { c_str_to_static_slice(file as *i8) }, line)
-}
-
 // Entry point of failure from the libcore crate
 #[no_mangle]
 #[cfg(not(test))]
-pub extern fn rust_begin_unwind(msg: &str, file: &'static str, line: uint) -> ! {
-    use str::StrAllocating;
-    begin_unwind(msg.to_owned(), file, line)
+pub extern fn rust_begin_unwind(msg: &fmt::Arguments,
+                                file: &'static str, line: uint) -> ! {
+    begin_unwind_fmt(msg, file, line)
 }
 
 /// The entry point for unwinding with a formatted message.
@@ -402,9 +390,9 @@ fn begin_unwind_inner(msg: Box<Any:Send>,
                 Some(mut stderr) => {
                     Local::put(task);
                     // FIXME: what to do when the task printing fails?
-                    let _err = format_args!(|args| ::fmt::writeln(stderr, args),
-                                            "task '{}' failed at '{}', {}:{}",
-                                            n, msg_s, file, line);
+                    let _err = write!(stderr,
+                                      "task '{}' failed at '{}', {}:{}\n",
+                                      n, msg_s, file, line);
                     if backtrace::log_enabled() {
                         let _err = backtrace::write(stderr);
                     }
index e8b1acb10241f331d970058695aeeb6810e0e437..5f9ea14a64711467c432ea1474a4d09b7c5e493a 100644 (file)
@@ -110,8 +110,9 @@ fn write(&mut self, data: &[u8]) -> IoResult<()> {
 }
 
 pub fn dumb_println(args: &fmt::Arguments) {
+    use io::Writer;
     let mut w = Stderr;
-    let _ = fmt::writeln(&mut w as &mut io::Writer, args);
+    let _ = writeln!(&mut w, "{}", args);
 }
 
 pub fn abort(msg: &str) -> ! {
index 7fb61c29112dee8b26ecaaa5b587677240c4a5d4..5c875b4a2ad088acd92035d2c85f498c12e982cd 100644 (file)
@@ -49,7 +49,6 @@
 
 #[cfg(test)] use any::AnyRefExt;
 #[cfg(test)] use owned::AnyOwnExt;
-#[cfg(test)] use realstd::result::ResultUnwrap;
 #[cfg(test)] use result;
 #[cfg(test)] use str::StrAllocating;
 
index 17251d31351ab303b72be6c41091c02391280fba..bc53d2bec8d6e3ca2b7004af7c940b2fd9f735cf 100644 (file)
@@ -155,7 +155,7 @@ fn bit(&self) -> u32 {
 
 impl fmt::Show for Abi {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "\"{}\"", self.name())
+        write!(f, "\"{}\"", self.name())
     }
 }
 
index e5ef31a95a38b5bc3c79cecdbbf9e314cc77c35d..edcb8c32ecc40dc2b3dec63d81fa495efb9d8779 100644 (file)
@@ -711,7 +711,7 @@ pub enum IntTy {
 
 impl fmt::Show for IntTy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}",
+        write!(f, "{}",
                ast_util::int_ty_to_str(*self, None, ast_util::AutoSuffix))
     }
 }
@@ -727,7 +727,7 @@ pub enum UintTy {
 
 impl fmt::Show for UintTy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}",
+        write!(f, "{}",
                ast_util::uint_ty_to_str(*self, None, ast_util::AutoSuffix))
     }
 }
@@ -741,7 +741,7 @@ pub enum FloatTy {
 
 impl fmt::Show for FloatTy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", ast_util::float_ty_to_str(*self))
+        write!(f, "{}", ast_util::float_ty_to_str(*self))
     }
 }
 
index 6a7b913dce406222ffd0674888170f6520290051..f1561ea31f91bcae60fe5f511e09c5c58a332f91 100644 (file)
@@ -41,7 +41,7 @@ pub fn name(&self) -> Name {
 impl fmt::Show for PathElem {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let slot = token::get_name(self.name());
-        write!(f.buf, "{}", slot)
+        write!(f, "{}", slot)
     }
 }
 
index 84ef7941b2ed7546d65a671b5bd7306e3c04fec2..b7700cf396d48dba6d2e3266ea70bcac685d5e53 100644 (file)
@@ -33,16 +33,16 @@ pub struct CrateId {
 
 impl fmt::Show for CrateId {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, "{}", self.path));
+        try!(write!(f, "{}", self.path));
         let version = match self.version {
             None => "0.0",
             Some(ref version) => version.as_slice(),
         };
         if self.path == self.name ||
                 self.path.as_slice().ends_with(format!("/{}", self.name)) {
-            write!(f.buf, "\\#{}", version)
+            write!(f, "\\#{}", version)
         } else {
-            write!(f.buf, "\\#{}:{}", self.name, version)
+            write!(f, "\\#{}:{}", self.name, version)
         }
     }
 }
index f4330960acacb3e20d113b47916195231d865681..06b56bbe472a250e804a429ff29596d4371884d9 100644 (file)
@@ -281,7 +281,10 @@ fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
                                 ext::fmt::expand_syntax_ext));
     syntax_expanders.insert(intern("format_args"),
                             builtin_normal_expander(
-                                ext::format::expand_args));
+                                ext::format::expand_format_args));
+    syntax_expanders.insert(intern("format_args_method"),
+                            builtin_normal_expander(
+                                ext::format::expand_format_args_method));
     syntax_expanders.insert(intern("env"),
                             builtin_normal_expander(
                                     ext::env::expand_env));
index aeaf53a193904b7e1f5330db0b73b5fd47c0d33a..343100d3a8ef6194b0100edca159c0fdb2628dd6 100644 (file)
@@ -120,23 +120,18 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
     // AST construction!
     // we're basically calling
     //
-    // format_arg!(|__args| ::std::fmt::write(fmt.buf, __args), "<format_string>", exprs...)
+    // format_arg_method!(fmt, write_fmt, "<format_string>", exprs...)
     //
     // but doing it directly via ext::format.
     let formatter = substr.nonself_args[0];
-    let buf = cx.expr_field_access(span, formatter, cx.ident_of("buf"));
-
-    let std_write = vec!(cx.ident_of("std"), cx.ident_of("fmt"), cx.ident_of("write"));
-    let args = cx.ident_of("__args");
-    let write_call = cx.expr_call_global(span, std_write, vec!(buf, cx.expr_ident(span, args)));
-    let format_closure = cx.lambda_expr(span, vec!(args), write_call);
 
+    let meth = cx.ident_of("write_fmt");
     let s = token::intern_and_get_ident(format_string.as_slice());
     let format_string = cx.expr_str(span, s);
 
     // phew, not our responsibility any more!
     format::expand_preparsed_format_args(cx, span,
-                                         format_closure,
+                                         format::MethodCall(formatter, meth),
                                          format_string, exprs, Vec::new(),
                                          HashMap::new())
 }
index c03d174365ed5fb94cd301d8bd99e95c577a596b..e92ce139d007036fa8d533c6727b6976709ee256 100644 (file)
@@ -59,6 +59,11 @@ struct Context<'a, 'b> {
     next_arg: uint,
 }
 
+pub enum Invocation {
+    Call(@ast::Expr),
+    MethodCall(@ast::Expr, ast::Ident),
+}
+
 /// Parses the arguments from the given list of tokens, returning None
 /// if there's a parse error so we can continue parsing other format!
 /// expressions.
@@ -67,8 +72,9 @@ struct Context<'a, 'b> {
 ///
 ///     Some((fmtstr, unnamed arguments, ordering of named arguments,
 ///           named arguments))
-fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-    -> (@ast::Expr, Option<(@ast::Expr, Vec<@ast::Expr>, Vec<StrBuf>,
+fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool,
+              tts: &[ast::TokenTree])
+    -> (Invocation, Option<(@ast::Expr, Vec<@ast::Expr>, Vec<StrBuf>,
                             HashMap<StrBuf, @ast::Expr>)>) {
     let mut args = Vec::new();
     let mut names = HashMap::<StrBuf, @ast::Expr>::new();
@@ -80,22 +86,31 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
                                                 .map(|x| (*x).clone())
                                                 .collect());
     // Parse the leading function expression (maybe a block, maybe a path)
-    let extra = p.parse_expr();
+    let invocation = if allow_method {
+        let e = p.parse_expr();
+        if !p.eat(&token::COMMA) {
+            ecx.span_err(sp, "expected token: `,`");
+            return (Call(e), None);
+        }
+        MethodCall(e, p.parse_ident())
+    } else {
+        Call(p.parse_expr())
+    };
     if !p.eat(&token::COMMA) {
         ecx.span_err(sp, "expected token: `,`");
-        return (extra, None);
+        return (invocation, None);
     }
 
     if p.token == token::EOF {
         ecx.span_err(sp, "requires at least a format string argument");
-        return (extra, None);
+        return (invocation, None);
     }
     let fmtstr = p.parse_expr();
     let mut named = false;
     while p.token != token::EOF {
         if !p.eat(&token::COMMA) {
             ecx.span_err(sp, "expected token: `,`");
-            return (extra, None);
+            return (invocation, None);
         }
         if p.token == token::EOF { break } // accept trailing commas
         if named || (token::is_ident(&p.token) &&
@@ -110,13 +125,13 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
                     ecx.span_err(p.span,
                                  "expected ident, positional arguments \
                                  cannot follow named arguments");
-                    return (extra, None);
+                    return (invocation, None);
                 }
                 _ => {
                     ecx.span_err(p.span,
                                  format!("expected ident for named argument, but found `{}`",
                                          p.this_token_to_str()));
-                    return (extra, None);
+                    return (invocation, None);
                 }
             };
             let interned_name = token::get_ident(ident);
@@ -137,7 +152,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
             args.push(p.parse_expr());
         }
     }
-    return (extra, Some((fmtstr, args, order, names)));
+    return (invocation, Some((fmtstr, args, order, names)));
 }
 
 impl<'a, 'b> Context<'a, 'b> {
@@ -595,7 +610,7 @@ fn trans_piece(&mut self, piece: &parse::Piece) -> @ast::Expr {
 
     /// Actually builds the expression which the iformat! block will be expanded
     /// to
-    fn to_expr(&self, extra: @ast::Expr) -> @ast::Expr {
+    fn to_expr(&self, invocation: Invocation) -> @ast::Expr {
         let mut lets = Vec::new();
         let mut locals = Vec::new();
         let mut names = Vec::from_fn(self.name_positions.len(), |_| None);
@@ -699,8 +714,16 @@ fn to_expr(&self, extra: @ast::Expr) -> @ast::Expr {
         let resname = self.ecx.ident_of("__args");
         lets.push(self.ecx.stmt_let(self.fmtsp, false, resname, result));
         let res = self.ecx.expr_ident(self.fmtsp, resname);
-        let result = self.ecx.expr_call(extra.span, extra, vec!(
-                            self.ecx.expr_addr_of(extra.span, res)));
+        let result = match invocation {
+            Call(e) => {
+                self.ecx.expr_call(e.span, e,
+                                   vec!(self.ecx.expr_addr_of(e.span, res)))
+            }
+            MethodCall(e, m) => {
+                self.ecx.expr_method_call(e.span, e, m,
+                                          vec!(self.ecx.expr_addr_of(e.span, res)))
+            }
+        };
         let body = self.ecx.expr_block(self.ecx.block(self.fmtsp, lets,
                                                       Some(result)));
 
@@ -794,13 +817,25 @@ fn format_arg(&self, sp: Span, argno: Position, arg: @ast::Expr)
     }
 }
 
-pub fn expand_args(ecx: &mut ExtCtxt, sp: Span,
-                   tts: &[ast::TokenTree]) -> Box<base::MacResult> {
+pub fn expand_format_args(ecx: &mut ExtCtxt, sp: Span,
+                          tts: &[ast::TokenTree]) -> Box<base::MacResult> {
+
+    match parse_args(ecx, sp, false, tts) {
+        (invocation, Some((efmt, args, order, names))) => {
+            MacExpr::new(expand_preparsed_format_args(ecx, sp, invocation, efmt,
+                                                      args, order, names))
+        }
+        (_, None) => MacExpr::new(ecx.expr_uint(sp, 2))
+    }
+}
+
+pub fn expand_format_args_method(ecx: &mut ExtCtxt, sp: Span,
+                                 tts: &[ast::TokenTree]) -> Box<base::MacResult> {
 
-    match parse_args(ecx, sp, tts) {
-        (extra, Some((efmt, args, order, names))) => {
-            MacExpr::new(expand_preparsed_format_args(ecx, sp, extra, efmt, args,
-                                                order, names))
+    match parse_args(ecx, sp, true, tts) {
+        (invocation, Some((efmt, args, order, names))) => {
+            MacExpr::new(expand_preparsed_format_args(ecx, sp, invocation, efmt,
+                                                      args, order, names))
         }
         (_, None) => MacExpr::new(ecx.expr_uint(sp, 2))
     }
@@ -810,7 +845,7 @@ pub fn expand_args(ecx: &mut ExtCtxt, sp: Span,
 /// name=names...)` and construct the appropriate formatting
 /// expression.
 pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
-                                    extra: @ast::Expr,
+                                    invocation: Invocation,
                                     efmt: @ast::Expr, args: Vec<@ast::Expr>,
                                     name_ordering: Vec<StrBuf>,
                                     names: HashMap<StrBuf, @ast::Expr>) -> @ast::Expr {
@@ -869,5 +904,5 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
         }
     }
 
-    cx.to_expr(extra)
+    cx.to_expr(invocation)
 }
index 68ce8cb2bc123104c9435fa7c103e170d7460c9b..5dfd18392a9c0da2a8c1b53eeaeb76c2e221af9f 100644 (file)
@@ -594,7 +594,7 @@ fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
 
 impl fmt::Show for InternedString {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.string.as_slice())
+        write!(f, "{}", self.string.as_slice())
     }
 }
 
index bf40a2d601fcca67d75aaeeead94ffc270b0ef2e..b3c768a519924c05f1cc63a10785374d8c8335c7 100644 (file)
@@ -38,7 +38,7 @@ fn local_sort<T: Float>(v: &mut [T]) {
 }
 
 /// Trait that provides simple descriptive statistics on a univariate set of numeric samples.
-pub trait Stats <T: Float + FromPrimitive>{
+pub trait Stats <T: FloatMath + FromPrimitive>{
 
     /// Sum of the samples.
     ///
@@ -143,7 +143,7 @@ pub struct Summary<T> {
     pub iqr: T,
 }
 
-impl<T: Float + FromPrimitive> Summary<T> {
+impl<T: FloatMath + FromPrimitive> Summary<T> {
 
     /// Construct a new summary of a sample set.
     pub fn new(samples: &[T]) -> Summary<T> {
@@ -164,7 +164,7 @@ pub fn new(samples: &[T]) -> Summary<T> {
     }
 }
 
-impl<'a,T: Float + FromPrimitive> Stats<T> for &'a [T] {
+impl<'a,T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
 
     // FIXME #11059 handle NaN, inf and overflow
     #[allow(deprecated_owned_vector)]
index a2e75e0bf9b1eb0c90a615461bac300c38280f4e..5fc567f06d38d8fbed9fee6d5b4b7275bf6e9d2f 100644 (file)
@@ -427,8 +427,8 @@ fn split_char_first(s: &str, c: char) -> (StrBuf, StrBuf) {
 impl fmt::Show for UserInfo {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self.pass {
-            Some(ref pass) => write!(f.buf, "{}:{}@", self.user, *pass),
-            None => write!(f.buf, "{}@", self.user),
+            Some(ref pass) => write!(f, "{}:{}@", self.user, *pass),
+            None => write!(f, "{}@", self.user),
         }
     }
 }
@@ -824,30 +824,30 @@ impl fmt::Show for Url {
      * result in just "http://somehost.com".
      */
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, "{}:", self.scheme));
+        try!(write!(f, "{}:", self.scheme));
 
         if !self.host.is_empty() {
-            try!(write!(f.buf, "//"));
+            try!(write!(f, "//"));
             match self.user {
-                Some(ref user) => try!(write!(f.buf, "{}", *user)),
+                Some(ref user) => try!(write!(f, "{}", *user)),
                 None => {}
             }
             match self.port {
-                Some(ref port) => try!(write!(f.buf, "{}:{}", self.host,
+                Some(ref port) => try!(write!(f, "{}:{}", self.host,
                                                 *port)),
-                None => try!(write!(f.buf, "{}", self.host)),
+                None => try!(write!(f, "{}", self.host)),
             }
         }
 
-        try!(write!(f.buf, "{}", self.path));
+        try!(write!(f, "{}", self.path));
 
         if !self.query.is_empty() {
-            try!(write!(f.buf, "?{}", query_to_str(&self.query)));
+            try!(write!(f, "?{}", query_to_str(&self.query)));
         }
 
         match self.fragment {
             Some(ref fragment) => {
-                write!(f.buf, "\\#{}", encode_component(fragment.as_slice()))
+                write!(f, "\\#{}", encode_component(fragment.as_slice()))
             }
             None => Ok(()),
         }
@@ -856,14 +856,14 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 
 impl fmt::Show for Path {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(f.buf, "{}", self.path));
+        try!(write!(f, "{}", self.path));
         if !self.query.is_empty() {
-            try!(write!(f.buf, "?{}", self.query))
+            try!(write!(f, "?{}", self.query))
         }
 
         match self.fragment {
             Some(ref fragment) => {
-                write!(f.buf, "\\#{}", encode_component(fragment.as_slice()))
+                write!(f, "\\#{}", encode_component(fragment.as_slice()))
             }
             None => Ok(())
         }
index d75f967a229b1990f43e9e9b1599f0226cf1966c..94f1239cc08465cfebe60f7fd3ecb98957a98d39 100644 (file)
@@ -154,17 +154,17 @@ impl fmt::Show for ParseError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             ErrorInvalidLength(found) =>
-                write!(f.buf, "Invalid length; expecting 32, 36 or 45 chars, \
-                               found {}", found),
+                write!(f, "Invalid length; expecting 32, 36 or 45 chars, \
+                           found {}", found),
             ErrorInvalidCharacter(found, pos) =>
-                write!(f.buf, "Invalid character; found `{}` (0x{:02x}) at \
-                               offset {}", found, found as uint, pos),
+                write!(f, "Invalid character; found `{}` (0x{:02x}) at \
+                           offset {}", found, found as uint, pos),
             ErrorInvalidGroups(found) =>
-                write!(f.buf, "Malformed; wrong number of groups: expected 1 \
-                               or 5, found {}", found),
+                write!(f, "Malformed; wrong number of groups: expected 1 \
+                           or 5, found {}", found),
             ErrorInvalidGroupLength(group, found, expecting) =>
-                write!(f.buf, "Malformed; length of group {} was {}, \
-                               expecting {}", group, found, expecting),
+                write!(f, "Malformed; length of group {} was {}, \
+                           expecting {}", group, found, expecting),
         }
     }
 }
@@ -474,7 +474,7 @@ fn from_str(us: &str) -> Option<Uuid> {
 /// Convert the UUID to a hexadecimal-based string representation
 impl fmt::Show for Uuid {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.to_simple_str())
+        write!(f, "{}", self.to_simple_str())
     }
 }
 
index 0abacf9ecdd23f8017a71cf2ef691019dee5e72d..c4c2f407423f5031c04f0bc529e804d8ae9e2e16 100644 (file)
@@ -19,7 +19,7 @@ pub struct cat {
 
     impl fmt::Show for cat {
         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-            write!(f.buf, "{}", self.name)
+            write!(f, "{}", self.name)
         }
     }
 
index 9db068e56c29af2ff4ee65103e5bec7d807279d2..f0bc0204fd26d17a53262f6581ba2619246396ea 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// no-pretty-expanded
+
 #![feature(phase)]
 #[phase(syntax)] extern crate green;
 
@@ -33,7 +35,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
             Yellow => "yellow",
             Blue => "blue",
         };
-        f.buf.write(str.as_bytes())
+        write!(f, "{}", str)
     }
 }
 
@@ -82,7 +84,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         }
 
         for s in out.iter().rev() {
-            try!(f.buf.write(s.as_bytes()));
+            try!(write!(f, "{}", s))
         }
         Ok(())
     }
diff --git a/src/test/compile-fail-fulldeps/gated-phase.rs b/src/test/compile-fail-fulldeps/gated-phase.rs
new file mode 100644 (file)
index 0000000..c876389
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:macro_crate_test.rs
+// ignore-stage1
+
+#[phase(syntax)]
+//~^ ERROR compile time crate loading is experimental and possibly buggy
+extern crate macro_crate_test;
+
+fn main() {}
diff --git a/src/test/compile-fail-fulldeps/macro-crate-unexported-macro.rs b/src/test/compile-fail-fulldeps/macro-crate-unexported-macro.rs
new file mode 100644 (file)
index 0000000..f8eb986
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:macro_crate_test.rs
+// ignore-stage1
+// ignore-android
+
+#![feature(phase)]
+
+#[phase(syntax)]
+extern crate macro_crate_test;
+
+fn main() {
+    assert_eq!(3, unexported_macro!()); //~ ERROR macro undefined: 'unexported_macro'
+}
diff --git a/src/test/compile-fail-fulldeps/macro-crate-unknown-crate.rs b/src/test/compile-fail-fulldeps/macro-crate-unknown-crate.rs
new file mode 100644 (file)
index 0000000..84c915f
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(phase)]
+
+#[phase(syntax)]
+extern crate doesnt_exist; //~ ERROR can't find crate
+
+fn main() {}
diff --git a/src/test/compile-fail-fulldeps/phase-syntax-doesnt-resolve.rs b/src/test/compile-fail-fulldeps/phase-syntax-doesnt-resolve.rs
new file mode 100644 (file)
index 0000000..2053f81
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:macro_crate_test.rs
+// ignore-stage1
+// ignore-android
+
+#![feature(phase)]
+
+#[phase(syntax)]
+extern crate macro_crate_test;
+
+fn main() {
+    macro_crate_test::foo();
+    //~^ ERROR unresolved name
+    //~^^ ERROR use of undeclared module `macro_crate_test`
+    //~^^^ ERROR unresolved name `macro_crate_test::foo`.
+}
diff --git a/src/test/compile-fail/gated-phase.rs b/src/test/compile-fail/gated-phase.rs
deleted file mode 100644 (file)
index c876389..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// aux-build:macro_crate_test.rs
-// ignore-stage1
-
-#[phase(syntax)]
-//~^ ERROR compile time crate loading is experimental and possibly buggy
-extern crate macro_crate_test;
-
-fn main() {}
index 830b041bbc7156c365629386042e245a24815346..897717971bc1735e1e863d854b6e5ae0fa5c9438 100644 (file)
@@ -10,5 +10,5 @@
 
 fn main() {
     format!("{:d}", "3");
-    //~^ ERROR: failed to find an implementation of trait std::fmt::Signed
+    //~^ ERROR: failed to find an implementation of trait core::fmt::Signed
 }
diff --git a/src/test/compile-fail/macro-crate-unexported-macro.rs b/src/test/compile-fail/macro-crate-unexported-macro.rs
deleted file mode 100644 (file)
index f8eb986..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// aux-build:macro_crate_test.rs
-// ignore-stage1
-// ignore-android
-
-#![feature(phase)]
-
-#[phase(syntax)]
-extern crate macro_crate_test;
-
-fn main() {
-    assert_eq!(3, unexported_macro!()); //~ ERROR macro undefined: 'unexported_macro'
-}
diff --git a/src/test/compile-fail/macro-crate-unknown-crate.rs b/src/test/compile-fail/macro-crate-unknown-crate.rs
deleted file mode 100644 (file)
index 84c915f..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-#![feature(phase)]
-
-#[phase(syntax)]
-extern crate doesnt_exist; //~ ERROR can't find crate
-
-fn main() {}
diff --git a/src/test/compile-fail/phase-syntax-doesnt-resolve.rs b/src/test/compile-fail/phase-syntax-doesnt-resolve.rs
deleted file mode 100644 (file)
index 2053f81..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// aux-build:macro_crate_test.rs
-// ignore-stage1
-// ignore-android
-
-#![feature(phase)]
-
-#[phase(syntax)]
-extern crate macro_crate_test;
-
-fn main() {
-    macro_crate_test::foo();
-    //~^ ERROR unresolved name
-    //~^^ ERROR use of undeclared module `macro_crate_test`
-    //~^^^ ERROR unresolved name `macro_crate_test::foo`.
-}
index e95ab71e5aaeae895c28ebaf5eb7b7a628b430b5..753c91d1dc9580ffde569ad6e219f1294ca0ea94 100644 (file)
@@ -18,7 +18,7 @@ struct Number {
 
 impl fmt::Show for Number {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.n)
+        write!(f, "{}", self.n)
     }
 }
 
index e0e60289f9dbf805dfd32a3d89ba37146a954454..bb101140ec39351650e880b8e5b59794499e34d4 100644 (file)
@@ -26,7 +26,7 @@
 impl Logger for MyWriter {
     fn log(&mut self, record: &LogRecord) {
         let MyWriter(ref mut inner) = *self;
-        fmt::writeln(inner as &mut Writer, record.args);
+        write!(inner, "{}", record.args);
     }
 }
 
@@ -45,5 +45,7 @@ fn main() {
         debug!("debug");
         info!("info");
     });
-    assert_eq!(r.read_to_str().unwrap(), "info\n".to_owned());
+    let s = r.read_to_str().unwrap();
+    assert!(s.contains("info"));
+    assert!(!s.contains("debug"));
 }
index 874cf1233b8bb61ad5a74f8327a4f08f9e4d0f9e..fdd44740d0531021b8ec20403ae95130f850064f 100644 (file)
@@ -53,7 +53,7 @@ fn cat(in_x : uint, in_y : int, in_name: StrBuf) -> cat {
 
 impl fmt::Show for cat {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.name)
+        write!(f, "{}", self.name)
     }
 }
 
index 802417da2c2e86495b27099d0cad949760901bca..14c2a5ae6c8b6a153700957d50945f37dbedcca3 100644 (file)
@@ -8,7 +8,10 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// no-pretty-expanded
+
 #![allow(unused_must_use, dead_code)]
+#![feature(macro_rules)]
 
 use std::io::MemWriter;
 
@@ -21,8 +24,9 @@ fn borrowing_writer_from_struct_and_formatting_struct_field(foo: Foo) {
     write!(foo.writer, "{}", foo.other);
 }
 
-pub fn main() {
+fn main() {
     let mut w = MemWriter::new();
     write!(&mut w as &mut Writer, "");
     write!(&mut w, ""); // should coerce
+    println!("ok");
 }
index 59ab75ddaaf196d841caae2c25967542c1577cb9..41650b680512977c99f7c45dfaf1608558e67628 100644 (file)
@@ -36,7 +36,7 @@ struct I { a: int, b: int }
 struct Custom;
 impl fmt::Show for Custom {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "yay")
+        write!(f, "yay")
     }
 }
 
index 56d265233baf1b7bc5c904094e9b27d445e32966..ee142aa8e6dbcdd0240dd60c5ae71c970b1af0d5 100644 (file)
 
 impl fmt::Signed for A {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        f.buf.write("aloha".as_bytes())
+        f.write("aloha".as_bytes())
     }
 }
 impl fmt::Signed for B {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        f.buf.write("adios".as_bytes())
+        f.write("adios".as_bytes())
     }
 }
 
@@ -195,9 +195,9 @@ fn test_format_args() {
     let mut buf = MemWriter::new();
     {
         let w = &mut buf as &mut io::Writer;
-        format_args!(|args| { fmt::write(w, args); }, "{}", 1);
-        format_args!(|args| { fmt::write(w, args); }, "test");
-        format_args!(|args| { fmt::write(w, args); }, "{test}", test=3);
+        format_args!(|args| { write!(w, "{}", args); }, "{}", 1);
+        format_args!(|args| { write!(w, "{}", args); }, "test");
+        format_args!(|args| { write!(w, "{}", args); }, "{test}", test=3);
     }
     let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_owned();
     t!(s, "1test3");
index 4626c0f0c787686424f4daed349740606ed10ff7..2ce3cb931e5d9772b12ab7f6ed13dedc36465bee 100644 (file)
@@ -29,7 +29,7 @@ enum square {
 
 impl fmt::Show for square {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", match *self {
+        write!(f, "{}", match *self {
           bot => { "R".to_owned() }
           wall => { "#".to_owned() }
           rock => { "*".to_owned() }
index ac2ce615fee405a06baae8ce328de30ec113f269..cdc07c026772e3bcff717ea5b00572717010c227 100644 (file)
@@ -106,7 +106,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                               .collect::<Vec<StrBuf>>();
 
         // Concatenate the lines together using a new-line.
-        write!(f.buf, "{}", lines.connect("\n"))
+        write!(f, "{}", lines.connect("\n"))
     }
 }
 
index 30200d4cb1807cf82d60f92d0ab73e14a19a691d..7431340e413a9f78f8fd1bd5d263df722ca18ba0 100644 (file)
@@ -17,7 +17,7 @@ struct Thingy {
 
 impl fmt::Show for Thingy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "\\{ x: {}, y: {} \\}", self.x, self.y)
+        write!(f, "\\{ x: {}, y: {} \\}", self.x, self.y)
     }
 }
 
@@ -27,7 +27,7 @@ struct PolymorphicThingy<T> {
 
 impl<T:fmt::Show> fmt::Show for PolymorphicThingy<T> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", self.x)
+        write!(f, "{}", self.x)
     }
 }