Skip to content

Commit 9851b4f

Browse files
committed
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<T>` 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.
1 parent 9f1ead8 commit 9851b4f

22 files changed

+75
-108
lines changed

src/liballoc/arc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,13 @@ impl<T: fmt::Show> fmt::Show for Arc<T> {
585585
}
586586
}
587587

588+
#[stable]
589+
impl<T: fmt::String> fmt::String for Arc<T> {
590+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
591+
fmt::String::fmt(&**self, f)
592+
}
593+
}
594+
588595
#[stable]
589596
impl<T: Default + Sync + Send> Default for Arc<T> {
590597
#[stable]

src/liballoc/boxed.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ impl<T: ?Sized + fmt::Show> fmt::Show for Box<T> {
149149
}
150150
}
151151

152+
#[stable]
152153
impl<T: ?Sized + fmt::String> fmt::String for Box<T> {
153154
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
154155
fmt::String::fmt(&**self, f)

src/liballoc/rc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ impl<T: fmt::Show> fmt::Show for Rc<T> {
611611
}
612612
}
613613

614+
#[stable]
615+
impl<T: fmt::String> fmt::String for Rc<T> {
616+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
617+
fmt::String::fmt(&**self, f)
618+
}
619+
}
620+
614621
/// A weak version of `Rc<T>`.
615622
///
616623
/// Weak references do not count when determining if the inner value should be dropped.

src/libcollections/string.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ impl fmt::Show for FromUtf8Error {
681681
}
682682
}
683683

684+
#[stable]
684685
impl fmt::String for FromUtf8Error {
685686
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
686687
fmt::String::fmt(&self.error, f)
@@ -693,6 +694,7 @@ impl fmt::Show for FromUtf16Error {
693694
}
694695
}
695696

697+
#[stable]
696698
impl fmt::String for FromUtf16Error {
697699
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
698700
fmt::String::fmt("invalid utf-16: lone surrogate found", f)
@@ -805,7 +807,7 @@ impl Default for String {
805807
}
806808
}
807809

808-
#[experimental = "waiting on fmt stabilization"]
810+
#[stable]
809811
impl fmt::String for String {
810812
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
811813
fmt::String::fmt(&**self, f)
@@ -1277,18 +1279,17 @@ mod tests {
12771279
assert_eq!(2u8.to_string(), "2");
12781280
assert_eq!(true.to_string(), "true");
12791281
assert_eq!(false.to_string(), "false");
1280-
assert_eq!(().to_string(), "()");
12811282
assert_eq!(("hi".to_string()).to_string(), "hi");
12821283
}
12831284

12841285
#[test]
12851286
fn test_vectors() {
12861287
let x: Vec<int> = vec![];
1287-
assert_eq!(x.to_string(), "[]");
1288-
assert_eq!((vec![1i]).to_string(), "[1]");
1289-
assert_eq!((vec![1i, 2, 3]).to_string(), "[1, 2, 3]");
1290-
assert!((vec![vec![], vec![1i], vec![1i, 1]]).to_string() ==
1291-
"[[], [1], [1, 1]]");
1288+
assert_eq!(format!("{:?}", x), "[]");
1289+
assert_eq!(format!("{:?}", vec![1i]), "[1i]");
1290+
assert_eq!(format!("{:?}", vec![1i, 2, 3]), "[1i, 2i, 3i]");
1291+
assert!(format!("{:?}", vec![vec![], vec![1i], vec![1i, 1]]) ==
1292+
"[[], [1i], [1i, 1i]]");
12921293
}
12931294

12941295
#[test]

src/libcollections/vec.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,22 +1454,6 @@ impl<T: fmt::Show> fmt::Show for Vec<T> {
14541454
}
14551455
}
14561456

1457-
#[cfg(stage0)]
1458-
#[experimental = "waiting on Show stability"]
1459-
impl<T: fmt::Show> fmt::String for Vec<T> {
1460-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1461-
fmt::String::fmt(self.as_slice(), f)
1462-
}
1463-
}
1464-
1465-
#[cfg(not(stage0))]
1466-
#[experimental = "waiting on Show stability"]
1467-
impl<T: fmt::String> fmt::String for Vec<T> {
1468-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1469-
fmt::String::fmt(self.as_slice(), f)
1470-
}
1471-
}
1472-
14731457
impl<'a> fmt::Writer for Vec<u8> {
14741458
fn write_str(&mut self, s: &str) -> fmt::Result {
14751459
self.push_all(s.as_bytes());

src/libcore/borrow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl<'a, T, B: ?Sized> PartialOrd for Cow<'a, T, B> where B: PartialOrd + ToOwne
248248
}
249249
}
250250

251+
#[stable]
251252
impl<'a, T, B: ?Sized> fmt::String for Cow<'a, T, B> where
252253
B: fmt::String + ToOwned<T>,
253254
T: fmt::String,

src/libcore/fmt/mod.rs

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl<'a> Show for Arguments<'a> {
219219
}
220220
}
221221

222+
#[stable]
222223
impl<'a> String for Arguments<'a> {
223224
fn fmt(&self, fmt: &mut Formatter) -> Result {
224225
write(fmt.buf, *self)
@@ -627,6 +628,7 @@ impl Show for bool {
627628
}
628629
}
629630

631+
#[stable]
630632
impl String for bool {
631633
fn fmt(&self, f: &mut Formatter) -> Result {
632634
String::fmt(if *self { "true" } else { "false" }, f)
@@ -653,6 +655,7 @@ impl Show for str {
653655
}
654656
}
655657

658+
#[stable]
656659
impl String for str {
657660
fn fmt(&self, f: &mut Formatter) -> Result {
658661
f.pad(self)
@@ -680,6 +683,7 @@ impl Show for char {
680683
}
681684
}
682685

686+
#[stable]
683687
impl String for char {
684688
fn fmt(&self, f: &mut Formatter) -> Result {
685689
let mut utf8 = [0u8; 4];
@@ -725,6 +729,7 @@ macro_rules! floating { ($ty:ident) => {
725729
}
726730
}
727731

732+
#[stable]
728733
impl String for $ty {
729734
fn fmt(&self, fmt: &mut Formatter) -> Result {
730735
use num::Float;
@@ -796,15 +801,9 @@ floating! { f64 }
796801
impl<T> Show for *const T {
797802
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
798803
}
799-
impl<T> String for *const T {
800-
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
801-
}
802804
impl<T> Show for *mut T {
803805
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
804806
}
805-
impl<T> String for *mut T {
806-
fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
807-
}
808807

809808
macro_rules! peel {
810809
($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
@@ -863,61 +862,12 @@ impl<T: Show> Show for [T] {
863862
}
864863
}
865864

866-
#[cfg(stage0)]
867-
impl<T: Show> String for [T] {
868-
fn fmt(&self, f: &mut Formatter) -> Result {
869-
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
870-
try!(write!(f, "["));
871-
}
872-
let mut is_first = true;
873-
for x in self.iter() {
874-
if is_first {
875-
is_first = false;
876-
} else {
877-
try!(write!(f, ", "));
878-
}
879-
try!(write!(f, "{}", *x))
880-
}
881-
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
882-
try!(write!(f, "]"));
883-
}
884-
Ok(())
885-
}
886-
}
887-
#[cfg(not(stage0))]
888-
impl<T: String> String for [T] {
889-
fn fmt(&self, f: &mut Formatter) -> Result {
890-
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
891-
try!(write!(f, "["));
892-
}
893-
let mut is_first = true;
894-
for x in self.iter() {
895-
if is_first {
896-
is_first = false;
897-
} else {
898-
try!(write!(f, ", "));
899-
}
900-
try!(write!(f, "{}", *x))
901-
}
902-
if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
903-
try!(write!(f, "]"));
904-
}
905-
Ok(())
906-
}
907-
}
908-
909865
impl Show for () {
910866
fn fmt(&self, f: &mut Formatter) -> Result {
911867
f.pad("()")
912868
}
913869
}
914870

915-
impl String for () {
916-
fn fmt(&self, f: &mut Formatter) -> Result {
917-
f.pad("()")
918-
}
919-
}
920-
921871
impl<T: Copy + Show> Show for Cell<T> {
922872
fn fmt(&self, f: &mut Formatter) -> Result {
923873
write!(f, "Cell {{ value: {:?} }}", self.get())
@@ -946,6 +896,7 @@ impl<'b, T: Show> Show for RefMut<'b, T> {
946896
}
947897
}
948898

899+
#[stable]
949900
impl String for Utf8Error {
950901
fn fmt(&self, f: &mut Formatter) -> Result {
951902
match *self {

src/libcore/option.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,18 @@
145145

146146
use self::Option::*;
147147

148+
use clone::Clone;
148149
use cmp::{Eq, Ord};
149150
use default::Default;
150-
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator};
151+
use fmt;
151152
use iter::{ExactSizeIterator};
153+
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator};
152154
use mem;
153-
use result::Result;
155+
use ops::{Deref, FnOnce};
154156
use result::Result::{Ok, Err};
155-
use slice;
157+
use result::Result;
156158
use slice::AsSlice;
157-
use clone::Clone;
158-
use ops::{Deref, FnOnce};
159+
use slice;
159160

160161
// Note that this is not a lang item per se, but it has a hidden dependency on
161162
// `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -762,7 +763,6 @@ impl<T> AsSlice<T> for Option<T> {
762763

763764
#[stable]
764765
impl<T> Default for Option<T> {
765-
#[stable]
766766
#[inline]
767767
#[stable]
768768
fn default() -> Option<T> { None }

src/librustdoc/html/format.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub struct ConciseStability<'a>(pub &'a Option<clean::Stability>);
5151
pub struct WhereClause<'a>(pub &'a clean::Generics);
5252
/// Wrapper struct for emitting type parameter bounds.
5353
pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
54+
/// Wrapper struct for emitting a comma-separated list of items
55+
pub struct CommaSep<'a, T: 'a>(pub &'a [T]);
5456

5557
impl VisSpace {
5658
pub fn get(&self) -> Option<ast::Visibility> {
@@ -64,6 +66,16 @@ impl UnsafetySpace {
6466
}
6567
}
6668

69+
impl<'a, T: fmt::String> fmt::String for CommaSep<'a, T> {
70+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71+
for (i, item) in self.0.iter().enumerate() {
72+
if i != 0 { try!(write!(f, ", ")); }
73+
try!(write!(f, "{}", item));
74+
}
75+
Ok(())
76+
}
77+
}
78+
6779
//NOTE(stage0): remove impl after snapshot
6880
#[cfg(stage0)]
6981
impl<'a> fmt::Show for TyParamBounds<'a> {
@@ -530,7 +542,8 @@ impl fmt::String for clean::Type {
530542
lifetimes = if decl.lifetimes.len() == 0 {
531543
"".to_string()
532544
} else {
533-
format!("for &lt;{:#}&gt;", decl.lifetimes)
545+
format!("for &lt;{}&gt;",
546+
CommaSep(decl.lifetimes.as_slice()))
534547
},
535548
args = decl.decl.inputs,
536549
arrow = decl.decl.output,
@@ -562,7 +575,8 @@ impl fmt::String for clean::Type {
562575
lifetimes = if decl.lifetimes.len() == 0 {
563576
"".to_string()
564577
} else {
565-
format!("for &lt;{:#}&gt;", decl.lifetimes)
578+
format!("for &lt;{}&gt;",
579+
CommaSep(decl.lifetimes.as_slice()))
566580
},
567581
args = decl.decl.inputs,
568582
bounds = if decl.bounds.len() == 0 {
@@ -592,7 +606,8 @@ impl fmt::String for clean::Type {
592606
primitive_link(f, clean::PrimitiveTuple,
593607
match typs.as_slice() {
594608
[ref one] => format!("({},)", one),
595-
many => format!("({:#})", many)
609+
many => format!("({})",
610+
CommaSep(many.as_slice()))
596611
}.as_slice())
597612
}
598613
clean::Vector(ref t) => {

src/libstd/path/posix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn is_sep(c: char) -> bool {
6060

6161
impl fmt::Show for Path {
6262
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63-
write!(f, "Path {{ {} }}", self.display())
63+
fmt::Show::fmt(&self.display(), f)
6464
}
6565
}
6666

src/libstd/path/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub struct Path {
8787

8888
impl fmt::Show for Path {
8989
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90-
write!(f, "Path {{ {} }}", self.display())
90+
fmt::Show::fmt(&self.display(), f)
9191
}
9292
}
9393

src/test/compile-fail/borrowck-borrowed-uniq-rvalue-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct defer<'a> {
1818
impl<'a> Drop for defer<'a> {
1919
fn drop(&mut self) {
2020
unsafe {
21-
println!("{}", self.x);
21+
println!("{:?}", self.x);
2222
}
2323
}
2424
}

src/test/compile-fail/drop-with-active-borrows-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ fn read_lines_borrowed<'a>() -> Vec<&'a str> {
1515
}
1616

1717
fn main() {
18-
println!("{}", read_lines_borrowed());
18+
println!("{:?}", read_lines_borrowed());
1919
}

src/test/compile-fail/no-capture-arc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ fn main() {
2323

2424
assert_eq!((*arc_v)[2], 3);
2525

26-
println!("{}", *arc_v);
26+
println!("{:?}", *arc_v);
2727
}

src/test/compile-fail/no-reuse-move-arc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ fn main() {
2121

2222
assert_eq!((*arc_v)[2], 3); //~ ERROR use of moved value: `arc_v`
2323

24-
println!("{}", *arc_v); //~ ERROR use of moved value: `arc_v`
24+
println!("{:?}", *arc_v); //~ ERROR use of moved value: `arc_v`
2525
}

src/test/compile-fail/packed-struct-generic-transmute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ fn main() {
3434
let foo = Foo { bar: [1u8, 2, 3, 4, 5], baz: 10i32 };
3535
unsafe {
3636
let oof: Oof<[u8; 5], i32> = mem::transmute(foo);
37-
println!("{} {}", &oof.rab[], oof.zab);
37+
println!("{:?} {:?}", &oof.rab[], oof.zab);
3838
}
3939
}

0 commit comments

Comments
 (0)