From 5ce82f274e17c7b98d4eeee3eaa8f5d33dbc0620 Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Wed, 17 May 2023 11:25:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0user=5Fconsole=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=80=81=E7=9A=84print=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3,=E4=BD=86=E6=98=AF=E5=B9=B6=E6=9C=AA=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=B3=BB=E7=BB=9F=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2/user/src/bin/00hello_world.rs | 4 +-- ch2/user/src/lib.rs | 3 +- ch2/user/src/user_lang_items.rs | 1 + ch2/user/src/user_lang_items/user_console.rs | 31 ++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 ch2/user/src/user_lang_items/user_console.rs diff --git a/ch2/user/src/bin/00hello_world.rs b/ch2/user/src/bin/00hello_world.rs index fbf31f5..965827b 100644 --- a/ch2/user/src/bin/00hello_world.rs +++ b/ch2/user/src/bin/00hello_world.rs @@ -1,9 +1,9 @@ #![no_std] #![no_main] -use user_lib; +use user_lib::*; #[no_mangle] fn main() { - + println!("123"); } \ No newline at end of file diff --git a/ch2/user/src/lib.rs b/ch2/user/src/lib.rs index 7c21dcb..081d219 100644 --- a/ch2/user/src/lib.rs +++ b/ch2/user/src/lib.rs @@ -2,7 +2,8 @@ #![feature(linkage)] // 开启弱链接特性 #![feature(panic_info_message)] -mod user_lang_items; +pub mod user_lang_items; +pub use user_lang_items::*; #[linkage = "weak"] // 设置我们默认的main函数, 弱链接 #[no_mangle] diff --git a/ch2/user/src/user_lang_items.rs b/ch2/user/src/user_lang_items.rs index 59f155e..fb50fe9 100644 --- a/ch2/user/src/user_lang_items.rs +++ b/ch2/user/src/user_lang_items.rs @@ -1 +1,2 @@ pub mod user_panic; +pub mod user_console; \ No newline at end of file diff --git a/ch2/user/src/user_lang_items/user_console.rs b/ch2/user/src/user_lang_items/user_console.rs new file mode 100644 index 0000000..d1ca998 --- /dev/null +++ b/ch2/user/src/user_lang_items/user_console.rs @@ -0,0 +1,31 @@ +use core::fmt::{Arguments, Write, Result}; + +struct Stdout; + +const STDOUT: usize = 1; + +impl Write for Stdout { + fn write_str(&mut self, s: &str) -> Result { + // ... todo 实现系统调用 + Ok(()) + } +} + +pub fn print(args: Arguments) { + Stdout.write_fmt(args).unwrap(); +} + + +#[macro_export] +macro_rules! print { + ($fmt: literal $(, $($arg: tt)+)?) => { + $crate::user_console::print(format_args!($fmt $(, $($arg)+)?)); + } +} + +#[macro_export] +macro_rules! println { + ($fmt: literal $(, $($arg: tt)+)?) => { + $crate::user_console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); + } +} \ No newline at end of file