Merge pull request #1325 from matthiasbeyer/libimagrt/locked-io
libimagrt: locked io
This commit is contained in:
commit
8c1a9ffe67
2 changed files with 62 additions and 12 deletions
|
@ -31,24 +31,24 @@ use std::io::Write;
|
||||||
/// a "sink" which does not write to either.
|
/// a "sink" which does not write to either.
|
||||||
///
|
///
|
||||||
pub enum OutputProxy {
|
pub enum OutputProxy {
|
||||||
Out,
|
Out(::std::io::Stdout),
|
||||||
Err,
|
Err(::std::io::Stderr),
|
||||||
Sink,
|
Sink,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Write for OutputProxy {
|
impl Write for OutputProxy {
|
||||||
fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
|
||||||
match *self {
|
match *self {
|
||||||
OutputProxy::Out => ::std::io::stdout().write(buf),
|
OutputProxy::Out(ref mut r) => r.write(buf),
|
||||||
OutputProxy::Err => ::std::io::stderr().write(buf),
|
OutputProxy::Err(ref mut r) => r.write(buf),
|
||||||
OutputProxy::Sink => Ok(0),
|
OutputProxy::Sink => Ok(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> ::std::io::Result<()> {
|
fn flush(&mut self) -> ::std::io::Result<()> {
|
||||||
match *self {
|
match *self {
|
||||||
OutputProxy::Out => ::std::io::stdout().flush(),
|
OutputProxy::Out(ref mut r) => r.flush(),
|
||||||
OutputProxy::Err => ::std::io::stderr().flush(),
|
OutputProxy::Err(ref mut r) => r.flush(),
|
||||||
OutputProxy::Sink => Ok(()),
|
OutputProxy::Sink => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,9 +58,59 @@ impl Write for OutputProxy {
|
||||||
impl Debug for OutputProxy {
|
impl Debug for OutputProxy {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
OutputProxy::Out => write!(f, "OutputProxy(Stdout)"),
|
OutputProxy::Out(..) => write!(f, "OutputProxy(Stdout)"),
|
||||||
OutputProxy::Err => write!(f, "OutputProxy(Stderr)"),
|
OutputProxy::Err(..) => write!(f, "OutputProxy(Stderr)"),
|
||||||
OutputProxy::Sink => write!(f, "OutputProxy(Sink)"),
|
OutputProxy::Sink => write!(f, "OutputProxy(Sink)"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OutputProxy {
|
||||||
|
pub fn lock(&self) -> LockedOutputProxy {
|
||||||
|
match *self {
|
||||||
|
OutputProxy::Out(ref r) => {
|
||||||
|
LockedOutputProxy::Out(r.lock())
|
||||||
|
},
|
||||||
|
OutputProxy::Err(ref r) => {
|
||||||
|
LockedOutputProxy::Err(r.lock())
|
||||||
|
},
|
||||||
|
OutputProxy::Sink => LockedOutputProxy::Sink,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum LockedOutputProxy<'a> {
|
||||||
|
Out(::std::io::StdoutLock<'a>),
|
||||||
|
Err(::std::io::StderrLock<'a>),
|
||||||
|
Sink,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Write for LockedOutputProxy<'a> {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
|
||||||
|
match *self {
|
||||||
|
LockedOutputProxy::Out(ref mut r) => r.write(buf),
|
||||||
|
LockedOutputProxy::Err(ref mut r) => r.write(buf),
|
||||||
|
LockedOutputProxy::Sink => Ok(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> ::std::io::Result<()> {
|
||||||
|
match *self {
|
||||||
|
LockedOutputProxy::Out(ref mut r) => r.flush(),
|
||||||
|
LockedOutputProxy::Err(ref mut r) => r.flush(),
|
||||||
|
LockedOutputProxy::Sink => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Debug for LockedOutputProxy<'a> {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
||||||
|
match *self {
|
||||||
|
LockedOutputProxy::Out(..) => write!(f, "LockedOutputProxy(Stdout)"),
|
||||||
|
LockedOutputProxy::Err(..) => write!(f, "LockedOutputProxy(Stderr)"),
|
||||||
|
LockedOutputProxy::Sink => write!(f, "LockedOutputProxy(Sink)"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -447,14 +447,14 @@ impl<'a> Runtime<'a> {
|
||||||
|
|
||||||
pub fn stdout(&self) -> OutputProxy {
|
pub fn stdout(&self) -> OutputProxy {
|
||||||
if self.stdout_is_tty {
|
if self.stdout_is_tty {
|
||||||
OutputProxy::Out
|
OutputProxy::Out(::std::io::stdout())
|
||||||
} else {
|
} else {
|
||||||
OutputProxy::Err
|
OutputProxy::Err(::std::io::stderr())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stderr(&self) -> OutputProxy {
|
pub fn stderr(&self) -> OutputProxy {
|
||||||
OutputProxy::Err
|
OutputProxy::Err(::std::io::stderr())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stdin(&self) -> Option<Stdin> {
|
pub fn stdin(&self) -> Option<Stdin> {
|
||||||
|
|
Loading…
Reference in a new issue