From 9851b4fbbf327bb1baab3182ce92970d4db22c6c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 7 Jan 2015 14:58:31 -0800 Subject: std: Tweak String implementations This commit performs a pass over the implementations of the new `String` trait in the formatting module. Some implementations were removed as a conservative move pending an upcoming convention about `String` implementations, and some were added in order to retain consistency across the libraries. Specifically: * All "smart pointers" implement `String` now, adding missing implementations for `Arc` and `Rc`. * The `Vec` and `[T]` types no longer implement `String`. * The `*const T` and `*mut T` type no longer implement `String`. * The `()` type no longer implements `String`. * The `Path` type's `Show` implementation does not surround itself with `Path {}` (a minor tweak). All implementations of `String` in this PR were also marked `#[stable]` to indicate that the types will continue to implement the `String` trait regardless of what it looks like. --- src/libcore/borrow.rs | 1 + src/libcore/fmt/mod.rs | 61 +++++--------------------------------------------- src/libcore/option.rs | 12 +++++----- 3 files changed, 13 insertions(+), 61 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/borrow.rs b/src/libcore/borrow.rs index 31631355422..11f5e593462 100644 --- a/src/libcore/borrow.rs +++ b/src/libcore/borrow.rs @@ -248,6 +248,7 @@ impl<'a, T, B: ?Sized> PartialOrd for Cow<'a, T, B> where B: PartialOrd + ToOwne } } +#[stable] impl<'a, T, B: ?Sized> fmt::String for Cow<'a, T, B> where B: fmt::String + ToOwned, T: fmt::String, diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index f9027f19068..8cceb37b9a4 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -219,6 +219,7 @@ impl<'a> Show for Arguments<'a> { } } +#[stable] impl<'a> String for Arguments<'a> { fn fmt(&self, fmt: &mut Formatter) -> Result { write(fmt.buf, *self) @@ -627,6 +628,7 @@ impl Show for bool { } } +#[stable] impl String for bool { fn fmt(&self, f: &mut Formatter) -> Result { String::fmt(if *self { "true" } else { "false" }, f) @@ -653,6 +655,7 @@ impl Show for str { } } +#[stable] impl String for str { fn fmt(&self, f: &mut Formatter) -> Result { f.pad(self) @@ -680,6 +683,7 @@ impl Show for char { } } +#[stable] impl String for char { fn fmt(&self, f: &mut Formatter) -> Result { let mut utf8 = [0u8; 4]; @@ -725,6 +729,7 @@ macro_rules! floating { ($ty:ident) => { } } + #[stable] impl String for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { use num::Float; @@ -796,15 +801,9 @@ floating! { f64 } impl Show for *const T { fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) } } -impl String for *const T { - fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) } -} impl Show for *mut T { fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) } } -impl String for *mut T { - fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) } -} macro_rules! peel { ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* }) @@ -863,61 +862,12 @@ impl Show for [T] { } } -#[cfg(stage0)] -impl String for [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(()) - } -} -#[cfg(not(stage0))] -impl String for [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 Show for () { fn fmt(&self, f: &mut Formatter) -> Result { f.pad("()") } } -impl String for () { - fn fmt(&self, f: &mut Formatter) -> Result { - f.pad("()") - } -} - impl Show for Cell { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "Cell {{ value: {:?} }}", self.get()) @@ -946,6 +896,7 @@ impl<'b, T: Show> Show for RefMut<'b, T> { } } +#[stable] impl String for Utf8Error { fn fmt(&self, f: &mut Formatter) -> Result { match *self { diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 272570a0d5b..56e27e801af 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -145,17 +145,18 @@ use self::Option::*; +use clone::Clone; use cmp::{Eq, Ord}; use default::Default; -use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator}; +use fmt; use iter::{ExactSizeIterator}; +use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator}; use mem; -use result::Result; +use ops::{Deref, FnOnce}; use result::Result::{Ok, Err}; -use slice; +use result::Result; use slice::AsSlice; -use clone::Clone; -use ops::{Deref, FnOnce}; +use slice; // Note that this is not a lang item per se, but it has a hidden dependency on // `Iterator`, which is one. The compiler assumes that the `next` method of @@ -762,7 +763,6 @@ impl AsSlice for Option { #[stable] impl Default for Option { - #[stable] #[inline] #[stable] fn default() -> Option { None } -- cgit 1.4.1-3-g733a5