Trait writeable::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> { ... }
    fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering { ... }
}
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()
}
source

fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering

Compares the contents of this Writeable to the given bytes without allocating a String to hold the Writeable contents.

This returns a lexicographical comparison, the same as if the Writeable were first converted to a String and then compared with Ord. For a locale-sensitive string ordering, use an ICU4X Collator.

§Examples
use core::cmp::Ordering;
use core::fmt;
use writeable::Writeable;

struct WelcomeMessage<'s> {
    pub name: &'s str,
}

impl<'s> Writeable for WelcomeMessage<'s> {
    // see impl in Writeable docs
}

let message = WelcomeMessage { name: "Alice" };
let message_str = message.write_to_string();

assert_eq!(Ordering::Equal, message.writeable_cmp_bytes(b"Hello, Alice!"));

assert_eq!(Ordering::Greater, message.writeable_cmp_bytes(b"Alice!"));
assert_eq!(Ordering::Greater, (*message_str).cmp("Alice!"));

assert_eq!(Ordering::Less, message.writeable_cmp_bytes(b"Hello, Bob!"));
assert_eq!(Ordering::Less, (*message_str).cmp("Hello, Bob!"));

Object Safety§

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§

fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering

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§