
use crate::sbi::console_put_char;
use core::fmt::{self, Write};

struct Stdout;

impl Write for Stdout{
    fn write_str(&mut self, s: &str) -> fmt::Result {
        for c in s.chars() {
            console_put_char(c as usize);
        }
        Ok(())
    }
}

// 用函数包装一下, 表示传进来的参数满足Arguments trait, 然后供宏调用
pub fn print(args: fmt::Arguments) {
    Stdout.write_fmt(args).unwrap();
}

#[macro_export]  // 导入这个文件即可使用这些宏
macro_rules! print {
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!($fmt $(, $($arg)+)?));
    }
}

#[macro_export]
macro_rules! println {
    ($fmt: literal $(, $($arg: tt)+)?) => {
        $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
    }
}
