]> git.lizzy.rs Git - rust.git/commitdiff
Don't allocate a string when calling println
authorAlex Crichton <alex@alexcrichton.com>
Sun, 20 Oct 2013 22:42:24 +0000 (15:42 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sun, 20 Oct 2013 22:42:24 +0000 (15:42 -0700)
Instead use format_args! to pass around a struct to pass along into std::fmt

src/libstd/rt/io/stdio.rs
src/libsyntax/ext/expand.rs

index e3ca148862fbe9fe88d3cf96a68c0af3853f2248..e6dd9a480998ebf39391187ae6bd62ae64268404 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use fmt;
 use libc;
 use option::{Option, Some, None};
 use result::{Ok, Err};
@@ -56,7 +57,9 @@ pub fn stderr() -> StdWriter {
 pub fn print(s: &str) {
     // XXX: need to see if not caching stdin() is the cause of performance
     //      issues, it should be possible to cache a stdout handle in each Task
-    //      and then re-use that across calls to print/println
+    //      and then re-use that across calls to print/println. Note that the
+    //      resolution of this comment will affect all of the prints below as
+    //      well.
     stdout().write(s.as_bytes());
 }
 
@@ -68,6 +71,20 @@ pub fn println(s: &str) {
     out.write(['\n' as u8]);
 }
 
+/// Similar to `print`, but takes a `fmt::Arguments` structure to be compatible
+/// with the `format_args!` macro.
+pub fn print_args(fmt: &fmt::Arguments) {
+    let mut out = stdout();
+    fmt::write(&mut out as &mut Writer, fmt);
+}
+
+/// Similar to `println`, but takes a `fmt::Arguments` structure to be
+/// compatible with the `format_args!` macro.
+pub fn println_args(fmt: &fmt::Arguments) {
+    let mut out = stdout();
+    fmt::writeln(&mut out as &mut Writer, fmt);
+}
+
 /// Representation of a reader of a standard input stream
 pub struct StdReader {
     priv inner: ~RtioFileStream
index bc91283b47bb30422e90dc825c05b0030226eae7..620594a0a171e47da73912507e52a368fee079d8 100644 (file)
@@ -999,16 +999,11 @@ macro_rules! write(($dst:expr, $($arg:tt)*) => (
     macro_rules! writeln(($dst:expr, $($arg:tt)*) => (
         format_args!(|args| { ::std::fmt::writeln($dst, args) }, $($arg)*)
     ))
-    // FIXME(#6846) once stdio is redesigned, this shouldn't perform an
-    //              allocation but should rather delegate to an invocation of
-    //              write! instead of format!
     macro_rules! print (
-        ($($arg:tt)*) => (::std::io::print(format!($($arg)*)))
+        ($($arg:tt)*) => (format_args!(::std::rt::io::stdio::print_args, $($arg)*))
     )
-    // FIXME(#6846) once stdio is redesigned, this shouldn't perform an
-    //              allocation but should rather delegate to an io::Writer
     macro_rules! println (
-        ($($arg:tt)*) => (::std::io::println(format!($($arg)*)))
+        ($($arg:tt)*) => (format_args!(::std::rt::io::stdio::println_args, $($arg)*))
     )
 
     macro_rules! local_data_key (