From 3cb7372b670db43f415b09e03fce22c04c090540 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 4 Mar 2018 15:42:00 +0100 Subject: [PATCH] Provide LockedOutputProxy which holds locked variants of Stdout/Stderr --- lib/core/libimagrt/src/io.rs | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/lib/core/libimagrt/src/io.rs b/lib/core/libimagrt/src/io.rs index 2c5dcca9..8107db48 100644 --- a/lib/core/libimagrt/src/io.rs +++ b/lib/core/libimagrt/src/io.rs @@ -64,3 +64,53 @@ impl Debug for OutputProxy { } } } + +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 { + 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)"), + } + } +} +