embedded_io_adapters/
fmt.rs

1//! Adapters to the `core::fmt::Write`.
2
3/// Adapter to the `core::fmt::Write` trait.
4#[derive(Clone, Default, PartialEq, Debug)]
5pub struct ToFmt<T: ?Sized> {
6    inner: T,
7}
8
9impl<T> ToFmt<T> {
10    /// Create a new adapter.
11    pub fn new(inner: T) -> Self {
12        Self { inner }
13    }
14
15    /// Consume the adapter, returning the inner object.
16    pub fn into_inner(self) -> T {
17        self.inner
18    }
19}
20
21impl<T: ?Sized> ToFmt<T> {
22    /// Borrow the inner object.
23    pub fn inner(&self) -> &T {
24        &self.inner
25    }
26
27    /// Mutably borrow the inner object.
28    pub fn inner_mut(&mut self) -> &mut T {
29        &mut self.inner
30    }
31}
32
33impl<T: embedded_io::Write + ?Sized> core::fmt::Write for ToFmt<T> {
34    fn write_str(&mut self, s: &str) -> core::fmt::Result {
35        self.inner.write_all(s.as_bytes()).or(Err(core::fmt::Error))
36    }
37
38    // Use fmt::Write default impls for
39    // * write_fmt(): better here than e-io::Write::write_fmt
40    //   since we don't need to bother with saving the Error
41    // * write_char(): would be the same
42}