diff options
| author | bors <bors@rust-lang.org> | 2014-02-01 22:31:26 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-02-01 22:31:26 -0800 |
| commit | dce61c980e0f5e18972caa22e0b78995f0501f51 (patch) | |
| tree | 0ae33048d5cf665a58759f4353f8679b9553bb74 /src/libstd | |
| parent | 16f1a72f0ab6ebf9e3279e3dd4873b6dc13220ec (diff) | |
| parent | c19a7b68577f28547bdfb357798d05163ccb6824 (diff) | |
auto merge of #11948 : huonw/rust/show, r=alexcrichton
- renames `Default` to `Show` - introduces some hidden `std::fmt::secret_...` functions, designed to work-around the lack of UFCS (with UFCS they can be replaced by referencing the trait methods directly) because I'm going to convert the traits to have methods rather than static functions, since `#[deriving]` works much better with true methods. I'm blocked on a snapshot after this. (I could probably do a large number of `#[cfg]`s, but I can work on other things in the meantime.)
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/fmt/mod.rs | 70 | ||||
| -rw-r--r-- | src/libstd/io/process.rs | 2 | ||||
| -rw-r--r-- | src/libstd/option.rs | 2 | ||||
| -rw-r--r-- | src/libstd/os.rs | 2 | ||||
| -rw-r--r-- | src/libstd/path/mod.rs | 6 | ||||
| -rw-r--r-- | src/libstd/result.rs | 2 |
6 files changed, 69 insertions, 15 deletions
diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index 13e6d808095..a17a030f4f7 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -149,13 +149,13 @@ The current mapping of types to traits is: * `f` ⇒ `Float` * `e` ⇒ `LowerExp` * `E` ⇒ `UpperExp` -* *nothing* ⇒ `Default` +* *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 `Default` trait. This is one of the more commonly +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 time, you will have to implement a @@ -186,7 +186,7 @@ struct Vector2D { y: int, } -impl fmt::Default for Vector2D { +impl fmt::Show for Vector2D { fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) { // The `f.buf` value is of the type `&mut io::Writer`, which is what th // write! macro is expecting. Note that this formatting ignores the @@ -468,6 +468,7 @@ will look like `"\\{"`. */ +#[cfg(not(stage0))] use prelude::*; use cast; @@ -479,6 +480,24 @@ use repr; use util; use vec; +// NOTE this is just because the `prelude::*` import above includes +// default::Default, so the reexport doesn't work. +#[cfg(stage0)] +pub use Default = fmt::Show; // export required for `format!()` etc. + +#[cfg(stage0)] +use container::Container; +#[cfg(stage0)] +use iter::{Iterator, range}; +#[cfg(stage0)] +use option::{Option,Some,None}; +#[cfg(stage0)] +use vec::ImmutableVector; +#[cfg(stage0)] +use str::StrSlice; +#[cfg(stage0)] +use num::Signed; + pub mod parse; pub mod rt; @@ -542,7 +561,7 @@ pub struct Arguments<'a> { /// 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. #[allow(missing_doc)] -pub trait Default { fn fmt(&Self, &mut Formatter); } +pub trait Show { fn fmt(&Self, &mut Formatter); } /// Format trait for the `b` character #[allow(missing_doc)] @@ -587,6 +606,41 @@ pub trait LowerExp { fn fmt(&Self, &mut Formatter); } #[allow(missing_doc)] pub trait UpperExp { fn fmt(&Self, &mut Formatter); } +// FIXME #11938 - UFCS would make us able call the above methods +// directly Show::show(x, fmt). + +// FIXME(huonw's WIP): this is a intermediate state waiting for a +// snapshot (at the time of writing we're at 2014-01-20 b6400f9), to +// be able to make the `fmt` functions into normal methods and have +// `format!()` still work. +macro_rules! uniform_fn_call_workaround { + ($( $name: ident, $trait_: ident; )*) => { + $( + #[doc(hidden)] + pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) { + $trait_::fmt(x, 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. @@ -1148,10 +1202,10 @@ impl<T> Pointer for *mut T { fn fmt(t: &*mut T, f: &mut Formatter) { Pointer::fmt(&(*t as *T), f) } } -// Implementation of Default for various core types +// Implementation of Show for various core types macro_rules! delegate(($ty:ty to $other:ident) => { - impl<'a> Default for $ty { + impl<'a> Show for $ty { fn fmt(me: &$ty, f: &mut Formatter) { $other::fmt(me, f) } @@ -1174,10 +1228,10 @@ delegate!(char to Char) delegate!(f32 to Float) delegate!(f64 to Float) -impl<T> Default for *T { +impl<T> Show for *T { fn fmt(me: &*T, f: &mut Formatter) { Pointer::fmt(me, f) } } -impl<T> Default for *mut T { +impl<T> Show for *mut T { fn fmt(me: &*mut T, f: &mut Formatter) { Pointer::fmt(me, f) } } diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs index c3fb3e97edf..6a10f24916f 100644 --- a/src/libstd/io/process.rs +++ b/src/libstd/io/process.rs @@ -91,7 +91,7 @@ pub enum ProcessExit { ExitSignal(int), } -impl fmt::Default for ProcessExit { +impl fmt::Show for ProcessExit { /// Format a ProcessExit enum, to nicely present the information. fn fmt(obj: &ProcessExit, f: &mut fmt::Formatter) { match *obj { diff --git a/src/libstd/option.rs b/src/libstd/option.rs index fd5f3a233e6..83cedd92a3f 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -380,7 +380,7 @@ impl<T: Default> Option<T> { // Trait implementations ///////////////////////////////////////////////////////////////////////////// -impl<T: fmt::Default> fmt::Default for Option<T> { +impl<T: fmt::Show> fmt::Show for Option<T> { #[inline] fn fmt(s: &Option<T>, f: &mut fmt::Formatter) { match *s { diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 457d58ae464..0ea1c7510a1 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -928,7 +928,7 @@ pub enum MapError { ErrMapViewOfFile(uint) } -impl fmt::Default for MapError { +impl fmt::Show for MapError { fn fmt(val: &MapError, out: &mut fmt::Formatter) { let str = match *val { ErrFdNotAvail => "fd not available for reading or writing", diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index c5482811a94..86f96a1075b 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -198,14 +198,14 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// Converts the Path into an owned byte vector fn into_vec(self) -> ~[u8]; - /// Returns an object that implements `fmt::Default` for printing paths + /// Returns an object that implements `Show` for printing paths /// /// This will print the equivalent of `to_display_str()` when used with a {} format parameter. fn display<'a>(&'a self) -> Display<'a, Self> { Display{ path: self, filename: false } } - /// Returns an object that implements `fmt::Default` for printing filenames + /// Returns an object that implements `Show` for printing filenames /// /// This will print the equivalent of `to_filename_display_str()` when used with a {} /// format parameter. If there is no filename, nothing will be printed. @@ -532,7 +532,7 @@ pub struct Display<'a, P> { priv filename: bool } -impl<'a, P: GenericPath> fmt::Default for Display<'a, P> { +impl<'a, P: GenericPath> fmt::Show for Display<'a, P> { fn fmt(d: &Display<P>, f: &mut fmt::Formatter) { d.with_str(|s| f.pad(s)) } diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 4783c983d00..cc8fdeaccfe 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -206,7 +206,7 @@ impl<T, E> Result<T, E> { // Trait implementations ///////////////////////////////////////////////////////////////////////////// -impl<T: fmt::Default, E: fmt::Default> fmt::Default for Result<T, E> { +impl<T: fmt::Show, E: fmt::Show> fmt::Show for Result<T, E> { #[inline] fn fmt(s: &Result<T, E>, f: &mut fmt::Formatter) { match *s { |
