Write

Trait Write 

Source
pub trait Write {
    type Error;

    // Required methods
    fn write_char(&mut self, val: char) -> Result<(), Self::Error>;
    fn size_hint(&mut self, bytes: usize);

    // Provided methods
    fn write_str(&mut self, val: &str) -> Result<(), Self::Error> { ... }
    fn uses_size_hint(&self) -> bool { ... }
    fn transform<T: Transform>(self, transformation: T) -> Transformer<T, Self>
       where Self: Sized { ... }
}
Expand description

Represents types to which characters may be written.

The difference between this trait and byte writers (such as std::io::Write or genio::Write) is that this guarantees valid UTF-8 encoding - it allows implementing this trait on String.

Required Associated Types§

Source

type Error

Type of error returned if write fails.

Required Methods§

Source

fn write_char(&mut self, val: char) -> Result<(), Self::Error>

Writes single char.

If this operation fails the state of underlying writer is unspecified. Re-trying is therefore impossible.

Source

fn size_hint(&mut self, bytes: usize)

Hints that implementor should allocate enough space so that string containing bytes UTF-8 bytes can be stored in it.

Warning: the number of bytes (chars) actually written might differ from this number! The writer must NOT fail if it does!

Provided Methods§

Source

fn write_str(&mut self, val: &str) -> Result<(), Self::Error>

Writes whole string.

By default, this just iterates and writes all characters. The implementors are encouraged to override this and provide faster implementation if possible.

If this operation fails the state of underlying writer is unspecified. Re-trying is therefore impossible.

Source

fn uses_size_hint(&self) -> bool

Tells the user whether the size hint is actually used. This allows the user to skip calculation of the hint.

By default this returns false but size_hint method is still mandatory to prevent people implementing this trait from forgetting about the size hint.

Source

fn transform<T: Transform>(self, transformation: T) -> Transformer<T, Self>
where Self: Sized,

Combinator for creating transformed writer.

Implementations on Foreign Types§

Source§

impl Write for String

Source§

type Error = Void

Source§

fn write_char(&mut self, val: char) -> Result<(), Self::Error>

Source§

fn write_str(&mut self, val: &str) -> Result<(), Self::Error>

Source§

fn size_hint(&mut self, bytes: usize)

Source§

fn uses_size_hint(&self) -> bool

Source§

impl<'a> Write for &'a mut [u8]

Source§

type Error = BufferOverflow

Source§

fn write_char(&mut self, val: char) -> Result<(), Self::Error>

Source§

fn size_hint(&mut self, _bytes: usize)

Source§

impl<'a, W: Write> Write for &'a mut W

Source§

type Error = <W as Write>::Error

Source§

fn write_char(&mut self, val: char) -> Result<(), Self::Error>

Source§

fn write_str(&mut self, val: &str) -> Result<(), Self::Error>

Source§

fn size_hint(&mut self, bytes: usize)

Source§

fn uses_size_hint(&self) -> bool

Implementors§

Source§

impl<T: Transform, W: Write> Write for Transformer<T, W>

Source§

type Error = <W as Write>::Error