Writeable

Trait Writeable 

Source
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§

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 Writeable for char

Source§

impl Writeable for i8

Source§

impl Writeable for i16

Source§

impl Writeable for i32

Source§

impl Writeable for i64

Source§

impl Writeable for i128

Source§

impl Writeable for isize

Source§

impl Writeable for str

Source§

impl Writeable for u8

Source§

impl Writeable for u16

Source§

impl Writeable for u32

Source§

impl Writeable for u64

Source§

impl Writeable for u128

Source§

impl Writeable for usize

Source§

impl Writeable for String

Available on crate feature alloc only.
Source§

impl<'a, T: ?Sized + Writeable + ToOwned> Writeable for Cow<'a, T>

Source§

impl<'a, T: ?Sized + Writeable> Writeable for Box<T>

Source§

impl<'a, T: ?Sized + Writeable> Writeable for Rc<T>

Source§

impl<'a, T: ?Sized + Writeable> Writeable for Arc<T>

Source§

impl<T: Writeable + ?Sized> Writeable for &T

Source§

impl<W0, W1> Writeable for Either<W0, W1>
where W0: Writeable, W1: Writeable,

A Writeable impl that delegates to one type or another type.

Implementors§