pub enum Destination<'a> {
Terminal(StandardStream),
Buffered(BufferWriter, Buffer),
UnColoredRaw(&'a mut (dyn Write + Send)),
ColoredRaw(Ansi<&'a mut (dyn Write + Send)>),
}
Expand description
Emit destinations provide four ways to emit.
Destination::Terminal
: Emit byStandardStream
Destination::Buffered
: Emit byBufferWriter
, you can save content inBuffer
first, and then emit theBuffer
toBufferWriter
on flush.Destination::UnColoredRaw
: Emit by a custom writer that does not support colors.Destination::ColoredRaw
: Emit by a custom writer that supports colors.
Note: All custom writers must implement two traits Write
and Send
.
§Examples
- If you want to use writer stdout or stderr, you can use the method
from_stderr
andfrom_stdout
.
use compiler_base_error::Destination;
use termcolor::ColorChoice;
// stdout
let dest_stdout = Destination::from_stdout(ColorChoice::Never);
// stderr
let dest_stderr = Destination::from_stderr(ColorChoice::Never);
- If you want to use custom writer
use compiler_base_error::Destination;
use termcolor::Ansi;
use std::io::Write;
use std::io;
// 1. Define a custom writer.
struct MyWriter {
content: String,
}
// 2. Implement trait `Write`.
impl Write for MyWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if let Ok(s) = std::str::from_utf8(buf) {
self.content.push_str(s)
} else {
self.content = "Nothing".to_string();
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
// 3. Implement trait `Send`.
unsafe impl Send for MyWriter {}
// 4. Define a destiation.
let mut my_writer = MyWriter{ content: String::new() };
Destination::UnColoredRaw(&mut my_writer);
// 5. If your custom writer supports color.
Destination::ColoredRaw(Ansi::new(&mut my_writer));
Variants§
Terminal(StandardStream)
Emit to stderr/stdout by stream.
Buffered(BufferWriter, Buffer)
Save by the ‘Buffer’, and then Emit to stderr/stdout by the ‘Buffer’ through the ‘BufferWriter’.
UnColoredRaw(&'a mut (dyn Write + Send))
Emit to a destiation without color.
ColoredRaw(Ansi<&'a mut (dyn Write + Send)>)
Emit to a customize destiation with color.
Implementations§
Source§impl<'a> Destination<'a>
impl<'a> Destination<'a>
Sourcepub fn from_stderr(choice: ColorChoice) -> Self
pub fn from_stderr(choice: ColorChoice) -> Self
New a stderr destination.
ColorChoice
is used to determine whether the output content has been colored.
Sourcepub fn from_stdout(choice: ColorChoice) -> Self
pub fn from_stdout(choice: ColorChoice) -> Self
New a stdout destination.
ColorChoice
is used to determine whether the output content has been colored.
Sourcepub fn supports_color(&self) -> bool
pub fn supports_color(&self) -> bool
Returns true if and only if the underlying Destination
supports colors.
Sourcepub fn set_color(&mut self, color: &ColorSpec) -> Result<()>
pub fn set_color(&mut self, color: &ColorSpec) -> Result<()>
Set color for the Destination
by ColorSpec
.
Subsequent writes to this writer will use these settings until either reset()
is called or new color settings are set.
If there was a problem resetting the color settings, then an error is returned.
Sourcepub fn reset(&mut self) -> Result<()>
pub fn reset(&mut self) -> Result<()>
Reset the current color settings for Destination
to their original settings.
If there was a problem resetting the color settings, then an error is returned.
Trait Implementations§
Source§impl<'a> Drop for Destination<'a>
impl<'a> Drop for Destination<'a>
Source§impl<'a> Write for Destination<'a>
impl<'a> Write for Destination<'a>
Source§fn write(&mut self, bytes: &[u8]) -> Result<usize>
fn write(&mut self, bytes: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Auto Trait Implementations§
impl<'a> !Freeze for Destination<'a>
impl<'a> !RefUnwindSafe for Destination<'a>
impl<'a> Send for Destination<'a>
impl<'a> !Sync for Destination<'a>
impl<'a> Unpin for Destination<'a>
impl<'a> !UnwindSafe for Destination<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more