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 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 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()
}

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§

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(_)));
Source§

fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result

Source§

fn writeable_length_hint(&self) -> LengthHint

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

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§

Source§

impl<T> Writeable for TryWriteableInfallibleAsWriteable<T>
where T: TryWriteable<Error = Infallible>,

Source§

impl<T: TryWriteable> Writeable for LossyWrap<T>

Source§

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

impl Writeable for BiesString<'_>

impl Writeable for CompactDecimal

impl Writeable for FixedInteger

impl Writeable for ScientificDecimal

impl Writeable for UnsignedDecimal

impl Writeable for Decimal

impl Writeable for DateTimePattern

impl Writeable for GenericPattern<'_>

impl Writeable for Pattern<'_>

impl Writeable for FormattedDateTime<'_>

impl Writeable for FormattedDecimal<'_>

impl Writeable for FormattedCompactCurrency<'_>

impl Writeable for FormattedCurrency<'_>

impl Writeable for FormattedLongCompactCurrency<'_>

impl Writeable for LongFormattedCurrency<'_>

impl Writeable for FormattedPercent<'_>

impl Writeable for FormattedUnit<'_>

impl Writeable for FormattedDuration<'_>

impl Writeable for FormattedRelativeTime<'_>

impl<'a, W: Writeable + 'a, I: Iterator<Item = W> + Clone + 'a> Writeable for FormattedList<'a, W, I>

impl Writeable for Other

impl Writeable for Private

impl Writeable for Subtag

impl Writeable for Extensions

impl Writeable for Fields

impl Writeable for Key

impl Writeable for Transform

impl Writeable for Value

impl Writeable for Attribute

impl Writeable for Attributes

impl Writeable for Key

impl Writeable for Keywords

impl Writeable for SubdivisionId

impl Writeable for SubdivisionSuffix

impl Writeable for Unicode

impl Writeable for Value

impl Writeable for DataLocale

impl Writeable for LanguageIdentifier

impl Writeable for Locale

impl Writeable for Language

impl Writeable for Region

impl Writeable for Script

impl Writeable for Subtag

impl Writeable for Variant

impl Writeable for Variants

impl Writeable for MissingNamedPlaceholderError<'_>

impl Writeable for FormattedHelloWorld<'_>

impl Writeable for DataLocaleFamily