Trait frame_support::dispatch::fmt::Write

1.0.0 · source ·
pub trait Write {
    // Required method
    fn write_str(&mut self, s: &str) -> Result<(), Error>;

    // Provided methods
    fn write_char(&mut self, c: char) -> Result<(), Error> { ... }
    fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error> { ... }
}
Expand description

A trait for writing or formatting into Unicode-accepting buffers or streams.

This trait only accepts UTF-8–encoded data and is not flushable. If you only want to accept Unicode and you don’t need flushing, you should implement this trait; otherwise you should implement std::io::Write.

Required Methods§

source

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

Writes a string slice into this writer, returning whether the write succeeded.

This method can only succeed if the entire string slice was successfully written, and this method will not return until all data has been written or an error occurs.

Errors

This function will return an instance of Error on error.

The purpose of std::fmt::Error is to abort the formatting operation when the underlying destination encounters some error preventing it from accepting more text; it should generally be propagated rather than handled, at least when implementing formatting traits.

Examples
use std::fmt::{Error, Write};

fn writer<W: Write>(f: &mut W, s: &str) -> Result<(), Error> {
    f.write_str(s)
}

let mut buf = String::new();
writer(&mut buf, "hola").unwrap();
assert_eq!(&buf, "hola");

Provided Methods§

1.1.0 · source

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

Writes a char into this writer, returning whether the write succeeded.

A single char may be encoded as more than one byte. This method can only succeed if the entire byte sequence was successfully written, and this method will not return until all data has been written or an error occurs.

Errors

This function will return an instance of Error on error.

Examples
use std::fmt::{Error, Write};

fn writer<W: Write>(f: &mut W, c: char) -> Result<(), Error> {
    f.write_char(c)
}

let mut buf = String::new();
writer(&mut buf, 'a').unwrap();
writer(&mut buf, 'b').unwrap();
assert_eq!(&buf, "ab");
source

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait.

This method should generally not be invoked manually, but rather through the write! macro itself.

Errors

This function will return an instance of Error on error. Please see write_str for details.

Examples
use std::fmt::{Error, Write};

fn writer<W: Write>(f: &mut W, s: &str) -> Result<(), Error> {
    f.write_fmt(format_args!("{s}"))
}

let mut buf = String::new();
writer(&mut buf, "world").unwrap();
assert_eq!(&buf, "world");

Implementors§

source§

impl Write for BytesMut

source§

impl Write for Writer

source§

impl Write for String

1.64.0 · source§

impl Write for OsString

1.2.0 · source§

impl Write for Formatter<'_>

source§

impl<'a, S, A> Write for Matcher<'a, S, A>where S: StateID, A: DFA<ID = S>,

source§

impl<A> Write for arrayvec::array_string::ArrayString<A>where A: Array<Item = u8> + Copy,

Write appends written data to the end of the string.

1.4.0 · source§

impl<W> Write for &mut Wwhere W: Write + ?Sized,

source§

impl<const CAP: usize> Write for arrayvec::array_string::ArrayString<CAP>

Write appends written data to the end of the string.