aboutsummaryrefslogtreecommitdiff
path: root/src/fmt/writer/termcolor/shim_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/fmt/writer/termcolor/shim_impl.rs')
-rw-r--r--src/fmt/writer/termcolor/shim_impl.rs32
1 files changed, 21 insertions, 11 deletions
diff --git a/src/fmt/writer/termcolor/shim_impl.rs b/src/fmt/writer/termcolor/shim_impl.rs
index 563f8ad..bfc31d0 100644
--- a/src/fmt/writer/termcolor/shim_impl.rs
+++ b/src/fmt/writer/termcolor/shim_impl.rs
@@ -1,11 +1,11 @@
-use std::io;
+use std::{io, sync::Mutex};
-use crate::fmt::{Target, WriteStyle};
+use crate::fmt::{WritableTarget, WriteStyle};
pub(in crate::fmt::writer) mod glob {}
pub(in crate::fmt::writer) struct BufferWriter {
- target: Target,
+ target: WritableTarget,
}
pub(in crate::fmt) struct Buffer(Vec<u8>);
@@ -13,13 +13,23 @@ pub(in crate::fmt) struct Buffer(Vec<u8>);
impl BufferWriter {
pub(in crate::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
BufferWriter {
- target: Target::Stderr,
+ target: WritableTarget::Stderr,
}
}
pub(in crate::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self {
BufferWriter {
- target: Target::Stdout,
+ target: WritableTarget::Stdout,
+ }
+ }
+
+ pub(in crate::fmt::writer) fn pipe(
+ _is_test: bool,
+ _write_style: WriteStyle,
+ pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
+ ) -> Self {
+ BufferWriter {
+ target: WritableTarget::Pipe(pipe),
}
}
@@ -30,12 +40,12 @@ impl BufferWriter {
pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
// This impl uses the `eprint` and `print` macros
// instead of using the streams directly.
- // This is so their output can be captured by `cargo test`
- let log = String::from_utf8_lossy(&buf.0);
-
- match self.target {
- Target::Stderr => eprint!("{}", log),
- Target::Stdout => print!("{}", log),
+ // This is so their output can be captured by `cargo test`.
+ match &self.target {
+ // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty.
+ WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(&buf.0)?,
+ WritableTarget::Stdout => print!("{}", String::from_utf8_lossy(&buf.0)),
+ WritableTarget::Stderr => eprint!("{}", String::from_utf8_lossy(&buf.0)),
}
Ok(())