From 0b593d6635928a7f73c5f14c23091ab88cafb006 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 4 Mar 2018 15:41:32 +0100 Subject: [PATCH] Let the OutputProxy hold Stdout/Stderr objects, so we do not have to aquire them each write!() --- lib/core/libimagrt/src/io.rs | 18 +++++++++--------- lib/core/libimagrt/src/runtime.rs | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/core/libimagrt/src/io.rs b/lib/core/libimagrt/src/io.rs index 5f6bc9d8..2c5dcca9 100644 --- a/lib/core/libimagrt/src/io.rs +++ b/lib/core/libimagrt/src/io.rs @@ -31,24 +31,24 @@ use std::io::Write; /// a "sink" which does not write to either. /// pub enum OutputProxy { - Out, - Err, + Out(::std::io::Stdout), + Err(::std::io::Stderr), Sink, } impl Write for OutputProxy { fn write(&mut self, buf: &[u8]) -> ::std::io::Result { match *self { - OutputProxy::Out => ::std::io::stdout().write(buf), - OutputProxy::Err => ::std::io::stderr().write(buf), + OutputProxy::Out(ref mut r) => r.write(buf), + OutputProxy::Err(ref mut r) => r.write(buf), OutputProxy::Sink => Ok(0), } } fn flush(&mut self) -> ::std::io::Result<()> { match *self { - OutputProxy::Out => ::std::io::stdout().flush(), - OutputProxy::Err => ::std::io::stderr().flush(), + OutputProxy::Out(ref mut r) => r.flush(), + OutputProxy::Err(ref mut r) => r.flush(), OutputProxy::Sink => Ok(()), } } @@ -58,9 +58,9 @@ impl Write for OutputProxy { impl Debug for OutputProxy { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { match *self { - OutputProxy::Out => write!(f, "OutputProxy(Stdout)"), - OutputProxy::Err => write!(f, "OutputProxy(Stderr)"), - OutputProxy::Sink => write!(f, "OutputProxy(Sink)"), + OutputProxy::Out(..) => write!(f, "OutputProxy(Stdout)"), + OutputProxy::Err(..) => write!(f, "OutputProxy(Stderr)"), + OutputProxy::Sink => write!(f, "OutputProxy(Sink)"), } } } diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index e76618c7..aec26192 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -447,14 +447,14 @@ impl<'a> Runtime<'a> { pub fn stdout(&self) -> OutputProxy { if self.stdout_is_tty { - OutputProxy::Out + OutputProxy::Out(::std::io::stdout()) } else { - OutputProxy::Err + OutputProxy::Err(::std::io::stderr()) } } pub fn stderr(&self) -> OutputProxy { - OutputProxy::Err + OutputProxy::Err(::std::io::stderr()) } pub fn stdin(&self) -> Option {