ufmt_write/
lib.rs

1//! `μfmt`'s `uWrite` trait
2
3#![cfg_attr(not(feature = "std"), no_std)]
4#![deny(missing_docs)]
5#![deny(rust_2018_compatibility)]
6#![deny(rust_2018_idioms)]
7#![deny(warnings)]
8
9#[cfg(feature = "std")]
10use core::convert::Infallible;
11
12#[allow(deprecated)]
13unsafe fn uninitialized<T>() -> T {
14    core::mem::uninitialized()
15}
16
17/// A collection of methods that are required / used to format a message into a stream.
18#[allow(non_camel_case_types)]
19pub trait uWrite {
20    /// The error associated to this writer
21    type Error;
22
23    /// Writes a string slice into this writer, returning whether the write succeeded.
24    ///
25    /// This method can only succeed if the entire string slice was successfully written, and this
26    /// method will not return until all data has been written or an error occurs.
27    fn write_str(&mut self, s: &str) -> Result<(), Self::Error>;
28
29    /// Writes a [`char`] into this writer, returning whether the write succeeded.
30    ///
31    /// A single [`char`] may be encoded as more than one byte. This method can only succeed if the
32    /// entire byte sequence was successfully written, and this method will not return until all
33    /// data has been written or an error occurs.
34    fn write_char(&mut self, c: char) -> Result<(), Self::Error> {
35        let mut buf: [u8; 4] = unsafe { uninitialized() };
36        self.write_str(c.encode_utf8(&mut buf))
37    }
38}
39
40#[cfg(feature = "std")]
41impl uWrite for String {
42    type Error = Infallible;
43
44    fn write_str(&mut self, s: &str) -> Result<(), Infallible> {
45        self.push_str(s);
46        Ok(())
47    }
48}