更多 trait

已经派生了 Debug trait。如果再实现更多 trait,会大有帮助。

use core::fmt::{self, Write};

impl Write for Uart {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        for c in s.as_bytes() {
            self.write_byte(*c);
        }
        Ok(())
    }
}

// Safe because it just contains a pointer to device memory, which can be
// accessed from any context.
unsafe impl Send for Uart {}
  • 通过实现 Write,我们可以将 write!writeln! 宏与 Uart 类型搭配使用。
  • 在 QEMU 中,使用 src/bare-metal/aps/examples 目录下的 make qemu_minimal 运行该示例。