pub trait Writeable {
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 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
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.
fn 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.
fn 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.
fn write_to_string(&self) -> Cow<'_, str>
fn write_to_string(&self) -> Cow<'_, str>
Creates a new String
with the data from this Writeable
. Like ToString
,
but smaller and faster.
The default impl allocates an owned String
. However, if it is possible to return a
borrowed string, overwrite this method to return a Cow::Borrowed
.
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()
}
Implementations on Foreign Types
sourceimpl Writeable for u8
impl Writeable for u8
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for i8
impl Writeable for i8
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for u16
impl Writeable for u16
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for i16
impl Writeable for i16
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for u32
impl Writeable for u32
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for i32
impl Writeable for i32
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for u64
impl Writeable for u64
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for i64
impl Writeable for i64
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for u128
impl Writeable for u128
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for i128
impl Writeable for i128
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for usize
impl Writeable for usize
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for isize
impl Writeable for isize
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn writeable_length_hint(&self) -> LengthHint
sourceimpl Writeable for str
impl Writeable for str
sourcefn write_to_string(&self) -> Cow<'_, str>
fn write_to_string(&self) -> Cow<'_, str>
Returns a borrowed str
.
Examples
use std::borrow::Cow;
use writeable::Writeable;
let cow = "foo".write_to_string();
assert!(matches!(cow, Cow::Borrowed(_)));