Skip to content

Commit 6e92f05

Browse files
committed
Use new io in print and println macroses
1 parent 3e4be02 commit 6e92f05

File tree

8 files changed

+62
-57
lines changed

8 files changed

+62
-57
lines changed

src/librustc_driver/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#![feature(staged_api)]
3838
#![feature(exit_status)]
3939
#![feature(io)]
40-
#![feature(set_panic)]
40+
#![feature(set_stdio)]
4141

4242
extern crate arena;
4343
extern crate flate;

src/librustdoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#![feature(core)]
2727
#![feature(exit_status)]
2828
#![feature(int_uint)]
29-
#![feature(set_panic)]
29+
#![feature(set_stdio)]
3030
#![feature(libc)]
3131
#![feature(old_path)]
3232
#![feature(rustc_private)]

src/libstd/io/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ pub use self::buffered::IntoInnerError;
3737
pub use self::cursor::Cursor;
3838
pub use self::error::{Result, Error, ErrorKind};
3939
pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat};
40-
pub use self::stdio::{stdin, stdout, stderr, Stdin, Stdout, Stderr};
40+
pub use self::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr};
4141
pub use self::stdio::{StdoutLock, StderrLock, StdinLock};
4242
#[doc(no_inline, hidden)]
43-
pub use self::stdio::set_panic;
43+
pub use self::stdio::{set_panic, set_print};
4444

4545
#[macro_use] mod lazy;
4646

src/libstd/io/stdio.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@
1111
use prelude::v1::*;
1212
use io::prelude::*;
1313

14+
use cell::RefCell;
1415
use cmp;
1516
use fmt;
1617
use io::lazy::Lazy;
1718
use io::{self, BufReader, LineWriter};
1819
use sync::{Arc, Mutex, MutexGuard};
1920
use sys::stdio;
2021

22+
/// Stdout used by print! and println! macroses
23+
thread_local! {
24+
static LOCAL_STDOUT: RefCell<Option<Box<Write + Send>>> = {
25+
RefCell::new(None)
26+
}
27+
}
28+
2129
/// A handle to a raw instance of the standard input stream of this process.
2230
///
2331
/// This handle is not synchronized or buffered in any fashion. Constructed via
@@ -347,15 +355,15 @@ impl<'a> Write for StderrLock<'a> {
347355
fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
348356
}
349357

350-
/// Resets the task-local stdout handle to the specified writer
358+
/// Resets the task-local stderr handle to the specified writer
351359
///
352-
/// This will replace the current task's stdout handle, returning the old
353-
/// handle. All future calls to `print` and friends will emit their output to
360+
/// This will replace the current task's stderr handle, returning the old
361+
/// handle. All future calls to `panic!` and friends will emit their output to
354362
/// this specified handle.
355363
///
356364
/// Note that this does not need to be called for all new tasks; the default
357-
/// output handle is to the process's stdout stream.
358-
#[unstable(feature = "set_panic",
365+
/// output handle is to the process's stderr stream.
366+
#[unstable(feature = "set_stdio",
359367
reason = "this function may disappear completely or be replaced \
360368
with a more general mechanism")]
361369
#[doc(hidden)]
@@ -369,3 +377,37 @@ pub fn set_panic(sink: Box<Write + Send>) -> Option<Box<Write + Send>> {
369377
Some(s)
370378
})
371379
}
380+
381+
/// Resets the task-local stdout handle to the specified writer
382+
///
383+
/// This will replace the current task's stdout handle, returning the old
384+
/// handle. All future calls to `print!` and friends will emit their output to
385+
/// this specified handle.
386+
///
387+
/// Note that this does not need to be called for all new tasks; the default
388+
/// output handle is to the process's stdout stream.
389+
#[unstable(feature = "set_stdio",
390+
reason = "this function may disappear completely or be replaced \
391+
with a more general mechanism")]
392+
#[doc(hidden)]
393+
pub fn set_print(sink: Box<Write + Send>) -> Option<Box<Write + Send>> {
394+
use mem;
395+
LOCAL_STDOUT.with(move |slot| {
396+
mem::replace(&mut *slot.borrow_mut(), Some(sink))
397+
}).and_then(|mut s| {
398+
let _ = s.flush();
399+
Some(s)
400+
})
401+
}
402+
403+
#[unstable(feature = "print",
404+
reason = "implementation detail which may disappear or be replaced at any time")]
405+
#[doc(hidden)]
406+
pub fn _print(args: fmt::Arguments) {
407+
if let Err(e) = LOCAL_STDOUT.with(|s| match s.borrow_mut().as_mut() {
408+
Some(w) => w.write_fmt(args),
409+
None => stdout().write_fmt(args)
410+
}) {
411+
panic!("failed printing to stdout: {}", e);
412+
}
413+
}

src/libstd/macros.rs

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

63+
/// Macro for printing to the standard output.
64+
///
6365
/// Equivalent to the `println!` macro except that a newline is not printed at
6466
/// the end of the message.
6567
#[macro_export]
6668
#[stable(feature = "rust1", since = "1.0.0")]
69+
#[allow_internal_unstable]
6770
macro_rules! print {
68-
($($arg:tt)*) => ($crate::old_io::stdio::print_args(format_args!($($arg)*)))
71+
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
6972
}
7073

71-
/// Macro for printing to a task's stdout handle.
74+
/// Macro for printing to the standard output.
7275
///
73-
/// Each task can override its stdout handle via `std::old_io::stdio::set_stdout`.
74-
/// The syntax of this macro is the same as that used for `format!`. For more
75-
/// information, see `std::fmt` and `std::old_io::stdio`.
76+
/// Use the `format!` syntax to write data to the standard output.
77+
/// See `std::fmt` for more information.
7678
///
7779
/// # Examples
7880
///
@@ -83,7 +85,8 @@ macro_rules! print {
8385
#[macro_export]
8486
#[stable(feature = "rust1", since = "1.0.0")]
8587
macro_rules! println {
86-
($($arg:tt)*) => ($crate::old_io::stdio::println_args(format_args!($($arg)*)))
88+
($fmt:expr) => (print!(concat!($fmt, "\n")));
89+
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
8790
}
8891

8992
/// Helper macro for unwrapping `Result` values while returning early with an

src/libstd/old_io/stdio.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -535,18 +535,4 @@ mod tests {
535535
stdout();
536536
stderr();
537537
}
538-
539-
#[test]
540-
fn capture_stdout() {
541-
use old_io::{ChanReader, ChanWriter};
542-
543-
let (tx, rx) = channel();
544-
let (mut r, w) = (ChanReader::new(rx), ChanWriter::new(tx));
545-
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
546-
let _t = thread::spawn(move|| {
547-
set_stdout(Box::new(w));
548-
println!("hello!");
549-
});
550-
assert_eq!(r.read_to_string().unwrap(), "hello!\n");
551-
}
552538
}

src/libtest/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@
3939
#![feature(collections)]
4040
#![feature(core)]
4141
#![feature(int_uint)]
42-
#![feature(old_io)]
4342
#![feature(rustc_private)]
4443
#![feature(staged_api)]
4544
#![feature(std_misc)]
4645
#![feature(io)]
4746
#![feature(libc)]
48-
#![feature(set_panic)]
47+
#![feature(set_stdio)]
4948

5049
extern crate getopts;
5150
extern crate serialize;
@@ -909,7 +908,6 @@ pub fn run_test(opts: &TestOpts,
909908
return;
910909
}
911910

912-
#[allow(deprecated)] // set_stdout
913911
fn run_test_inner(desc: TestDesc,
914912
monitor_ch: Sender<MonitorMsg>,
915913
nocapture: bool,
@@ -921,11 +919,6 @@ pub fn run_test(opts: &TestOpts,
921919
}
922920
fn flush(&mut self) -> io::Result<()> { Ok(()) }
923921
}
924-
impl Writer for Sink {
925-
fn write_all(&mut self, data: &[u8]) -> std::old_io::IoResult<()> {
926-
Writer::write_all(&mut *self.0.lock().unwrap(), data)
927-
}
928-
}
929922

930923
thread::spawn(move || {
931924
let data = Arc::new(Mutex::new(Vec::new()));
@@ -937,7 +930,7 @@ pub fn run_test(opts: &TestOpts,
937930

938931
let result_guard = cfg.spawn(move || {
939932
if !nocapture {
940-
std::old_io::stdio::set_stdout(box Sink(data2.clone()));
933+
io::set_print(box Sink(data2.clone()));
941934
io::set_panic(box Sink(data2));
942935
}
943936
testfn.invoke(())

src/libtest/stats.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#![allow(missing_docs)]
1212

1313
use std::cmp::Ordering::{self, Less, Greater, Equal};
14-
use std::collections::hash_map::Entry::{Occupied, Vacant};
15-
use std::collections::hash_map;
16-
use std::hash::Hash;
1714
use std::mem;
1815
use std::num::{Float, FromPrimitive};
1916

@@ -330,22 +327,6 @@ pub fn winsorize<T: Float + FromPrimitive>(samples: &mut [T], pct: T) {
330327
}
331328
}
332329

333-
/// Returns a HashMap with the number of occurrences of every element in the
334-
/// sequence that the iterator exposes.
335-
#[cfg(not(stage0))]
336-
pub fn freq_count<T, U>(iter: T) -> hash_map::HashMap<U, uint>
337-
where T: Iterator<Item=U>, U: Eq + Clone + Hash
338-
{
339-
let mut map: hash_map::HashMap<U,uint> = hash_map::HashMap::new();
340-
for elem in iter {
341-
match map.entry(elem) {
342-
Occupied(mut entry) => { *entry.get_mut() += 1; },
343-
Vacant(entry) => { entry.insert(1); },
344-
}
345-
}
346-
map
347-
}
348-
349330
// Test vectors generated from R, using the script src/etc/stat-test-vectors.r.
350331

351332
#[cfg(test)]

0 commit comments

Comments
 (0)