pub trait Writeable {
// Provided methods
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result { ... }
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result { ... }
fn writeable_length_hint(&self) -> LengthHint { ... }
fn writeable_borrow(&self) -> Option<&str> { ... }
fn write_to_string(&self) -> Cow<'_, str> { ... }
}Expand description
Writeable is an alternative to std::fmt::Display with the addition of a length function.
Provided Methods§
Sourcefn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
Writes a string to the given sink. Errors from the sink are bubbled up.
The default implementation delegates to write_to_parts, and discards any
Part annotations.
Sourcefn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result
Write bytes and Part annotations to the given sink. Errors from the
sink are bubbled up. The default implementation delegates to write_to,
and doesn’t produce any Part annotations.
Sourcefn writeable_length_hint(&self) -> LengthHint
fn writeable_length_hint(&self) -> LengthHint
Returns a hint for the number of UTF-8 bytes that will be written to the sink.
Override this method if it can be computed quickly.
Sourcefn writeable_borrow(&self) -> Option<&str>
fn writeable_borrow(&self) -> Option<&str>
Returns a &str that matches the output of write_to, if possible.
This method is used to avoid materializing a String in write_to_string.
Sourcefn write_to_string(&self) -> Cow<'_, str>
fn write_to_string(&self) -> Cow<'_, str>
Creates a new string with the data from this Writeable.
Unlike to_string, this does not pull in core::fmt
code, and borrows the string if possible.
To remove the Cow wrapper, call .into_owned() or .as_str() as appropriate.
§Examples
Inspect a Writeable before writing it to the sink:
use core::fmt::{Result, Write};
use writeable::Writeable;
fn write_if_ascii<W, S>(w: &W, sink: &mut S) -> Result
where
W: Writeable + ?Sized,
S: Write + ?Sized,
{
let s = w.write_to_string();
if s.is_ascii() {
sink.write_str(&s)
} else {
Ok(())
}
}Convert the Writeable into a fully owned String:
use writeable::Writeable;
fn make_string(w: &impl Writeable) -> String {
w.write_to_string().into_owned()
}§Note to implementors
This method has a default implementation in terms of writeable_borrow,
writeable_length_hint, and write_to. The only case
where this should be implemented is if the computation of writeable_borrow
requires a full invocation of write_to. In this case, implement this
using to_string_or_borrow.
§alloc Cargo feature
Calling or implementing this method requires the alloc Cargo feature.
However, as all the methods required by the default implementation do
not require the alloc Cargo feature, a caller that uses the feature
can still call this on types from crates that don’t use the alloc
Cargo feature.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<'a, T: ?Sized + Writeable + ToOwned> Writeable for Cow<'a, T>
impl<'a, T: ?Sized + Writeable + ToOwned> Writeable for Cow<'a, T>
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
fn writeable_borrow(&self) -> Option<&str>
fn write_to_string(&self) -> Cow<'_, str>
Source§impl<'a, T: ?Sized + Writeable> Writeable for Box<T>
impl<'a, T: ?Sized + Writeable> Writeable for Box<T>
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
fn writeable_borrow(&self) -> Option<&str>
fn write_to_string(&self) -> Cow<'_, str>
Source§impl<'a, T: ?Sized + Writeable> Writeable for Rc<T>
impl<'a, T: ?Sized + Writeable> Writeable for Rc<T>
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
fn writeable_borrow(&self) -> Option<&str>
fn write_to_string(&self) -> Cow<'_, str>
Source§impl<'a, T: ?Sized + Writeable> Writeable for Arc<T>
impl<'a, T: ?Sized + Writeable> Writeable for Arc<T>
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
fn writeable_borrow(&self) -> Option<&str>
fn write_to_string(&self) -> Cow<'_, str>
Source§impl<T: Writeable + ?Sized> Writeable for &T
impl<T: Writeable + ?Sized> Writeable for &T
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
fn writeable_borrow(&self) -> Option<&str>
fn write_to_string(&self) -> Cow<'_, str>
Source§impl<W0, W1> Writeable for Either<W0, W1>
A Writeable impl that delegates to one type or another type.
impl<W0, W1> Writeable for Either<W0, W1>
A Writeable impl that delegates to one type or another type.