Skip to content

Commit 031c512

Browse files
committed
make ToString completely use format! internally
1 parent def019b commit 031c512

File tree

7 files changed

+48
-50
lines changed

7 files changed

+48
-50
lines changed

src/libcollections/macros.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@ macro_rules! vec {
2222
);
2323
($($x:expr,)*) => (vec![$($x),*])
2424
}
25+
26+
/// Use the syntax described in `std::fmt` to create a value of type `String`.
27+
/// See `std::fmt` for more information.
28+
///
29+
/// # Example
30+
///
31+
/// ```
32+
/// format!("test");
33+
/// format!("hello {}", "world!");
34+
/// format!("x = {}, y = {y}", 10i, y = 30i);
35+
/// ```
36+
#[macro_export]
37+
#[stable]
38+
macro_rules! format {
39+
($($arg:tt)*) => ($crate::string::format(format_args!($($arg)*)))
40+
}

src/libcollections/string.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,32 @@ impl String {
672672
}
673673
}
674674

675+
/// The format function takes a precompiled format string and a list of
676+
/// arguments, to return the resulting formatted string.
677+
///
678+
/// # Arguments
679+
///
680+
/// * args - a structure of arguments generated via the `format_args!` macro.
681+
///
682+
/// # Example
683+
///
684+
/// ```rust
685+
/// use std::string;
686+
///
687+
/// let s = string::format(format_args!("Hello, {}!", "world"));
688+
/// assert_eq!(s, "Hello, world!".to_string());
689+
/// ```
690+
#[unstable = "this is an implementation detail of format! and should not \
691+
be called directly"]
692+
pub fn format(args: fmt::Arguments) -> String {
693+
use core::fmt::Writer;
694+
let hint = fmt::Display::size_hint(&args);
695+
let mut output = String::with_capacity(hint.min);
696+
let _ = write!(&mut output, "{}", args);
697+
output.shrink_to_fit();
698+
output
699+
}
700+
675701
impl FromUtf8Error {
676702
/// Consume this error, returning the bytes that were attempted to make a
677703
/// `String` with.
@@ -942,11 +968,7 @@ pub trait ToString {
942968
impl<T: fmt::Display + ?Sized> ToString for T {
943969
#[inline]
944970
fn to_string(&self) -> String {
945-
use core::fmt::Writer;
946-
let mut buf = String::new();
947-
let _ = buf.write_fmt(format_args!("{}", self));
948-
buf.shrink_to_fit();
949-
buf
971+
format!("{}", self)
950972
}
951973
}
952974

src/libstd/fmt.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,7 @@
405405
406406
#![unstable]
407407

408-
use string;
409-
410-
pub use core::fmt::{Formatter, Result, Writer, rt};
408+
pub use core::fmt::{Formatter, Result, Writer, rt, SizeHint};
411409
pub use core::fmt::{Show, String, Octal, Binary};
412410
pub use core::fmt::{Display, Debug};
413411
pub use core::fmt::{LowerHex, UpperHex, Pointer};
@@ -417,28 +415,3 @@ pub use core::fmt::{Argument, Arguments, write, radix, Radix, RadixFmt};
417415

418416
#[doc(hidden)]
419417
pub use core::fmt::{argument, argumentuint};
420-
421-
/// The format function takes a precompiled format string and a list of
422-
/// arguments, to return the resulting formatted string.
423-
///
424-
/// # Arguments
425-
///
426-
/// * args - a structure of arguments generated via the `format_args!` macro.
427-
///
428-
/// # Example
429-
///
430-
/// ```rust
431-
/// use std::fmt;
432-
///
433-
/// let s = fmt::format(format_args!("Hello, {}!", "world"));
434-
/// assert_eq!(s, "Hello, world!".to_string());
435-
/// ```
436-
#[unstable = "this is an implementation detail of format! and should not \
437-
be called directly"]
438-
pub fn format(args: Arguments) -> string::String {
439-
let min = String::size_hint(args);
440-
let mut output = string::String::with_capacity(min);
441-
let _ = write!(&mut output, "{}", args);
442-
output.shrink_to_fit();
443-
output
444-
}

src/libstd/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ extern crate log;
130130
extern crate core;
131131

132132
#[macro_use]
133-
#[macro_reexport(vec)]
133+
#[macro_reexport(format, vec)]
134134
extern crate "collections" as core_collections;
135135

136136
extern crate "rand" as core_rand;

src/libstd/macros.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,6 @@ macro_rules! panic {
6060
});
6161
}
6262

63-
/// Use the syntax described in `std::fmt` to create a value of type `String`.
64-
/// See `std::fmt` for more information.
65-
///
66-
/// # Example
67-
///
68-
/// ```
69-
/// format!("test");
70-
/// format!("hello {}", "world!");
71-
/// format!("x = {}, y = {y}", 10i, y = 30i);
72-
/// ```
73-
#[macro_export]
74-
#[stable]
75-
macro_rules! format {
76-
($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
77-
}
7863

7964
/// Equivalent to the `println!` macro except that a newline is not printed at
8065
/// the end of the message.

src/test/run-pass/deriving-show.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ macro_rules! t {
3030
}
3131
}
3232

33+
3334
pub fn main() {
3435
t!(Unit, "Unit");
3536
t!(Tuple(1, 2), "Tuple(1, 2)");

src/test/run-pass/ifmt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ fn test_print() {
196196
// can do with them just yet (to test the output)
197197
fn test_format_args() {
198198
use std::fmt::Writer;
199+
use std::string;
199200
let mut buf = String::new();
200201
{
201202
let w = &mut buf;
@@ -206,7 +207,7 @@ fn test_format_args() {
206207
let s = buf;
207208
t!(s, "1test3");
208209

209-
let s = fmt::format(format_args!("hello {}", "world"));
210+
let s = string::format(format_args!("hello {}", "world"));
210211
t!(s, "hello world");
211212
let s = format!("{}: {}", "args were", format_args!("hello {}", "world"));
212213
t!(s, "args were: hello world");

0 commit comments

Comments
 (0)