6 releases (3 breaking)

new 0.4.1 Aug 4, 2022
0.4.0 May 10, 2022
0.3.0 Jan 31, 2022
0.2.1 Nov 1, 2021
0.1.0 Nov 12, 2020

#8 in #cldr

Download history 164/week @ 2022-04-21 266/week @ 2022-04-28 263/week @ 2022-05-05 278/week @ 2022-05-12 1250/week @ 2022-05-19 672/week @ 2022-05-26 997/week @ 2022-06-02 1323/week @ 2022-06-09 2592/week @ 2022-06-16 3061/week @ 2022-06-23 3592/week @ 2022-06-30 3847/week @ 2022-07-07 3914/week @ 2022-07-14 3322/week @ 2022-07-21 5836/week @ 2022-07-28 3344/week @ 2022-08-04

16,995 downloads per month
Used in 34 crates (14 directly)

Unicode-DFS-2016

31KB
552 lines

writeable crates.io

writeable is a utility crate of the ICU4X project.

It includes [Writeable], a core trait representing an object that can be written to a sink implementing std::fmt::Write. It is an alternative to std::fmt::Display with the addition of a function indicating the number of bytes to be written.

Writeable improves upon std::fmt::Display in two ways:

  1. More efficient, since the sink can pre-allocate bytes.
  2. Smaller code, since the format machinery can be short-circuited.

Types implementing Writeable have a defaulted write_to_string function. If desired, types implementing Writeable can manually implement ToString to wrap write_to_string.

Examples

use std::fmt;
use writeable::assert_writeable_eq;
use writeable::LengthHint;
use writeable::Writeable;

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

impl<'s> Writeable for WelcomeMessage<'s> {
    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
        sink.write_str("Hello, ")?;
        sink.write_str(self.name)?;
        sink.write_char('!')?;
        Ok(())
    }

    fn write_len(&self) -> LengthHint {
        // "Hello, " + '!' + length of name
        LengthHint::exact(8 + self.name.len())
    }
}

let message = WelcomeMessage { name: "Alice" };
assert_writeable_eq!(&message, "Hello, Alice!");

More Information

For more information on development, authorship, contributing etc. please visit ICU4X home page.

No runtime deps