# stdout.rs - source
Source of the Rust file `/home/runner/.cargo/git/checkouts/k210-hal-1069b16e3180bc3a/795ac3f/src/stdout.rs`.
Keywords: rust, rustlang, rust-lang

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31

    //! Stdout
    pub use core::fmt::Write;
    use nb::block;

    /// Stdout implements the core::fmt::Write trait for hal::serial::Write
    /// implementations.
    pub struct Stdout<'p, T>(pub &'p mut T);

    impl<'p, T> Write for Stdout<'p, T>
        where
        T: embedded_hal::serial::Write<u8>,
    {
        fn write_str(&mut self, s: &str) -> core::fmt::Result {
            for byte in s.as_bytes() {
                if *byte == b'\n' {
                    let res = block!(self.0.try_write(b'\r'));

                    if res.is_err() {
                        return Err(core::fmt::Error);
                    }
                }

                let res = block!(self.0.try_write(*byte));

                if res.is_err() {
                    return Err(core::fmt::Error);
                }
            }
            Ok(())
        }
    }