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