Skip to content

Commit

Permalink
misc: change macro import method
Browse files Browse the repository at this point in the history
* See <rust-lang/rust#35896 (comment)>.
* This means #[macro_use] and #[macro_export(local_inner_macros)] can be obsolete.
* See also <https://stackoverflow.com/a/67140319>
  • Loading branch information
Tiger3018 committed May 30, 2023
1 parent b60406b commit 8efb454
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
7 changes: 7 additions & 0 deletions hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
// #![warn(missing_docs)]


use os::printk::{
print,
println,
debug_print as dprint,
debug_println as dprintln,
};
mod os;
mod arch;

// use crate::task::{Signals, SignalStack};
// use riscv::register::sstatus::{self, set_spp, Sstatus, SPP};

Expand Down
3 changes: 2 additions & 1 deletion os/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[macro_use]
// #[macro_use]
pub mod printk;

pub mod panic;
4 changes: 2 additions & 2 deletions os/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::arch::riscv_sbi::shutdown::shutdown;
#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
if let Some(location) = _panic.location() {
println!(
crate::println!(
"Panicked at {}:{} {}",
location.file(),
location.line(),
_panic.message().unwrap()
);
} else {
println!("Panicked: {}", _panic.message().unwrap());
crate::println!("Panicked: {}", _panic.message().unwrap());
}
shutdown();
// loop {};
Expand Down
34 changes: 28 additions & 6 deletions os/printk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::arch::riscv_sbi::shutdown::console_putchar;
use crate::arch::riscv_sbi::console_putchar;
use core::fmt::{self, Write};

struct Stdout;
Expand All @@ -17,20 +17,42 @@ pub fn printk(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}


#[macro_export]
//#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::os::printk::printk(format_args!($fmt $(, $($arg)+)?));
}
}

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

// pub(crate) use print;
// pub(crate) use println;
// #[macro_export]
#[cfg(debug_assertions)]
macro_rules! debug_print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::os::printk::print!("[D]");
$crate::os::printk::print!($fmt $(, $($arg)+)?);
}
}

// #[macro_export]
#[cfg(debug_assertions)]
macro_rules! debug_println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::os::printk::print!("[D]");
$crate::os::printk::println!($fmt $(, $($arg)+)?);
}
}

#[doc(inline)]
pub(crate) use {
print,
println,
debug_print,
debug_println,
};

0 comments on commit 8efb454

Please sign in to comment.