From 5e0ccd5aa52b34d32fcec2d0983458214d036da6 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 12 Apr 2017 19:28:15 +0200 Subject: [PATCH] Implement the fmt::Write trait and print something with the `write!` macro --- src/vga_buffer.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index 522f535..20d620d 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -1,3 +1,4 @@ +use core::fmt; use core::ptr::Unique; use volatile::Volatile; @@ -87,12 +88,26 @@ impl Writer { fn new_line(&mut self) {/* TODO */} } +impl fmt::Write for Writer { + fn write_str(&mut self, s: &str) -> fmt::Result { + for byte in s.bytes() { + self.write_byte(byte) + } + Ok(()) + } +} + + pub fn print_something() { + use core::fmt::Write; + let mut writer = Writer { column_position: 0, color_code: ColorCode::new(Color::LightGreen, Color::Black), buffer: unsafe { Unique::new(0xb8000 as *mut _) }, }; - writer.write_str("Hello!"); + writer.write_byte(b'H'); + writer.write_str("ello! "); + write!(writer, "The numbers are {} and {}", 42, 1.0/3.0); }