Trait TryWriteable
trait TryWriteable
A writeable object that can fail while writing.
The default Writeable trait returns a fmt::Error, which originates from the sink.
In contrast, this trait allows the writeable itself to trigger an error as well.
Implementations are expected to always make a best attempt at writing to the sink
and should write replacement values in the error state. Therefore, the returned Result
can be safely ignored to emulate a "lossy" mode.
Any error substrings should be annotated with Part::ERROR.
Implementer Notes
This trait requires that implementers make a best attempt at writing to the sink, even in the error state, such as with a placeholder or fallback string.
In [TryWriteable::try_write_to_parts()], error substrings should be annotated with
Part::ERROR. Because of this, writing to parts is not default-implemented like
it is on Writeable.
The trait is implemented on [Result<T, E>] where T and E both implement Writeable;
In the Ok case, T is written, and in the Err case, E is written as a fallback value.
This impl, which writes Part::ERROR, can be used as a basis for more advanced impls.
Examples
Implementing on a custom type:
use fmt;
use LengthHint;
use PartsWrite;
use TryWriteable;
// Success case:
assert_try_writeable_eq!;
// Failure case, including the ERROR part:
assert_try_writeable_parts_eq!;
Associated Types
type Error
Required Methods
fn try_write_to_parts<S: PartsWrite + ?Sized>(self: &Self, sink: &mut S) -> Result<Result<(), <Self as >::Error>, Error>Writes the content of this writeable to a sink with parts (annotations).
For more information, see:
- [
TryWriteable::try_write_to()] for the general behavior. TryWriteablefor an example with parts.Partfor more about parts.
- [
Provided Methods
fn try_write_to<W: fmt::Write + ?Sized>(self: &Self, sink: &mut W) -> Result<Result<(), <Self as >::Error>, Error>Writes the content of this writeable to a sink.
If the sink hits an error, writing immediately ends,
Err(fmt::Error)is returned, and the sink does not contain valid output.If the writeable hits an error, writing is continued with a replacement value,
Ok(Err(TryWriteable::Error))is returned, and the caller may continue using the sink.Lossy Mode
The
fmt::Errorshould always be handled, but theTryWriteable::Errorcan be ignored if a fallback string is desired instead of an error.To handle the sink error, but not the writeable error, write:
# use TryWriteable; # let my_writeable: = Ok; # let mut sink = Stringnew; let _ = my_writeable.try_write_to?; # Ok::Examples
The following examples use
Result<&str, usize>, which implementsTryWriteablebecause both&strandusizedo.Success case:
use TryWriteable; let w: = Ok; let mut sink = Stringnew; let result = w.try_write_to; assert_eq!; assert_eq!;Failure case:
use TryWriteable; let w: = Err; let mut sink = Stringnew; let result = w.try_write_to; assert_eq!; assert_eq!;fn writeable_length_hint(self: &Self) -> LengthHintReturns a hint for the number of UTF-8 bytes that will be written to the sink.
This function returns the length of the "lossy mode" string; for more information, see [
TryWriteable::try_write_to()].fn try_write_to_string(self: &Self) -> Result<Cow<'_, str>, (<Self as >::Error, Cow<'_, str>)>Writes the content of this writeable to a string.
In the failure case, this function returns the error and the best-effort string ("lossy mode").
Examples
# use Cow; # use TryWriteable; // use the best-effort string let r1: = Ok:: .try_write_to_string .unwrap_or_else; // propagate the error let r2: = Ok:: .try_write_to_string .map_err;
Implementors
impl<T, E> TryWriteable for Result<T, E>impl<T> TryWriteable for WriteableAsTryWriteableInfallible<T>