Skip to content

Commit d82a7ea

Browse files
committed
Move ToString to collections::string
This also impls `FormatWriter` for `Vec<u8>`
1 parent 59abf75 commit d82a7ea

File tree

11 files changed

+51
-72
lines changed

11 files changed

+51
-72
lines changed

src/libcollections/string.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,20 @@ pub trait IntoString {
808808
fn into_string(self) -> String;
809809
}
810810

811+
/// A generic trait for converting a value to a string
812+
pub trait ToString {
813+
/// Converts the value of `self` to an owned string
814+
fn to_string(&self) -> String;
815+
}
816+
817+
impl<T: fmt::Show> ToString for T {
818+
fn to_string(&self) -> String {
819+
let mut buf = Vec::<u8>::new();
820+
let _ = format_args!(|args| fmt::write(&mut buf, args), "{}", self);
821+
String::from_utf8(buf).unwrap()
822+
}
823+
}
824+
811825
/// Unsafe operations
812826
#[unstable = "waiting on raw module conventions"]
813827
pub mod raw {
@@ -873,7 +887,7 @@ mod tests {
873887

874888
use str;
875889
use str::{Str, StrPrelude, Owned};
876-
use super::{as_string, String};
890+
use super::{as_string, String, ToString};
877891
use vec::Vec;
878892
use slice::CloneSliceAllocPrelude;
879893

@@ -1177,6 +1191,28 @@ mod tests {
11771191
assert_eq!("oob", s[1..4]);
11781192
}
11791193

1194+
#[test]
1195+
fn test_simple_types() {
1196+
assert_eq!(1i.to_string(), "1".to_string());
1197+
assert_eq!((-1i).to_string(), "-1".to_string());
1198+
assert_eq!(200u.to_string(), "200".to_string());
1199+
assert_eq!(2u8.to_string(), "2".to_string());
1200+
assert_eq!(true.to_string(), "true".to_string());
1201+
assert_eq!(false.to_string(), "false".to_string());
1202+
assert_eq!(().to_string(), "()".to_string());
1203+
assert_eq!(("hi".to_string()).to_string(), "hi".to_string());
1204+
}
1205+
1206+
#[test]
1207+
fn test_vectors() {
1208+
let x: Vec<int> = vec![];
1209+
assert_eq!(x.to_string(), "[]".to_string());
1210+
assert_eq!((vec![1i]).to_string(), "[1]".to_string());
1211+
assert_eq!((vec![1i, 2, 3]).to_string(), "[1, 2, 3]".to_string());
1212+
assert!((vec![vec![], vec![1i], vec![1i, 1]]).to_string() ==
1213+
"[[], [1], [1, 1]]".to_string());
1214+
}
1215+
11801216
#[bench]
11811217
fn bench_with_capacity(b: &mut Bencher) {
11821218
b.iter(|| {

src/libcollections/vec.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,13 @@ impl<T> Vec<T> {
16531653
}
16541654
}
16551655

1656+
impl<'a> fmt::FormatWriter for Vec<u8> {
1657+
fn write(&mut self, buf: &[u8]) -> fmt::Result {
1658+
self.push_all(buf);
1659+
Ok(())
1660+
}
1661+
}
1662+
16561663
#[cfg(test)]
16571664
mod tests {
16581665
extern crate test;

src/libstd/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ pub mod time;
220220

221221
pub mod error;
222222
pub mod num;
223-
pub mod to_string;
224223

225224
/* Common data structures */
226225

src/libstd/num/strconv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
423423

424424
#[cfg(test)]
425425
mod tests {
426-
use to_string::ToString;
426+
use string::ToString;
427427

428428
#[test]
429429
fn test_int_to_str_overflow() {

src/libstd/os.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ use result::{Err, Ok, Result};
5151
use slice::{AsSlice, SlicePrelude, PartialEqSlicePrelude};
5252
use slice::CloneSliceAllocPrelude;
5353
use str::{Str, StrPrelude, StrAllocating};
54-
use string::String;
55-
use to_string::ToString;
54+
use string::{String, ToString};
5655
use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
5756
use vec::Vec;
5857

src/libstd/prelude.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,13 @@
7676
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek};
7777
#[doc(no_inline)] pub use str::{Str, StrVector, StrPrelude};
7878
#[doc(no_inline)] pub use str::{IntoMaybeOwned, StrAllocating, UnicodeStrPrelude};
79-
#[doc(no_inline)] pub use to_string::ToString;
8079
#[doc(no_inline)] pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4};
8180
#[doc(no_inline)] pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8};
8281
#[doc(no_inline)] pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12};
8382
#[doc(no_inline)] pub use slice::{SlicePrelude, AsSlice, CloneSlicePrelude};
8483
#[doc(no_inline)] pub use slice::{VectorVector, PartialEqSlicePrelude, OrdSlicePrelude};
8584
#[doc(no_inline)] pub use slice::{CloneSliceAllocPrelude, OrdSliceAllocPrelude, SliceAllocPrelude};
86-
#[doc(no_inline)] pub use string::{IntoString, String};
85+
#[doc(no_inline)] pub use string::{IntoString, String, ToString};
8786
#[doc(no_inline)] pub use vec::Vec;
8887

8988
// Reexported runtime types

src/libstd/task.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ use rt::local::Local;
105105
use rt::task;
106106
use rt::task::Task;
107107
use str::{Str, SendStr, IntoMaybeOwned};
108-
use string::String;
108+
use string::{String, ToString};
109109
use sync::Future;
110-
use to_string::ToString;
111110

112111
/// A means of spawning a task
113112
pub trait Spawner {

src/libstd/time/duration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ mod tests {
388388
use super::{Duration, MIN, MAX};
389389
use {i32, i64};
390390
use option::{Some, None};
391-
use to_string::ToString;
391+
use string::ToString;
392392

393393
#[test]
394394
fn test_duration() {

src/libstd/to_string.rs

-60
This file was deleted.

src/test/run-pass/class-cast-to-trait-cross-crate-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// aux-build:cci_class_cast.rs
1212
extern crate cci_class_cast;
1313

14-
use std::to_string::ToString;
14+
use std::string::ToString;
1515
use cci_class_cast::kitty::cat;
1616

1717
fn print_out(thing: Box<ToString>, expected: String) {

src/test/run-pass/send_str_treemap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
extern crate collections;
1212

1313
use std::str::{SendStr, Owned, Slice};
14-
use std::to_string::ToString;
14+
use std::string::ToString;
1515
use self::collections::TreeMap;
1616
use std::option::Some;
1717

0 commit comments

Comments
 (0)