hermit/
macros.rs

1/// Prints to the standard output.
2///
3/// Adapted from [`std::print`].
4///
5/// [`std::print`]: https://doc.rust-lang.org/stable/std/macro.print.html
6#[cfg(target_os = "none")]
7#[macro_export]
8macro_rules! print {
9    ($($arg:tt)*) => {{
10        $crate::console::_print(::core::format_args!($($arg)*));
11    }};
12}
13
14/// Prints to the standard output, with a newline.
15///
16/// Adapted from [`std::println`].
17///
18/// [`std::println`]: https://doc.rust-lang.org/stable/std/macro.println.html
19#[cfg(target_os = "none")]
20#[macro_export]
21macro_rules! println {
22    () => {
23        $crate::print!("\n")
24    };
25    ($($arg:tt)*) => {{
26        $crate::console::_print(::core::format_args!("{}\n", format_args!($($arg)*)));
27    }};
28}
29
30/// Emergency output.
31#[cfg(target_os = "none")]
32#[macro_export]
33macro_rules! panic_println {
34    () => {{
35        $crate::console::_panic_print(::core::format_args!("\n"));
36    }};
37    ($($arg:tt)*) => {{
38        $crate::console::_panic_print(::core::format_args!("{}\n", format_args!($($arg)*)));
39    }};
40}
41
42#[cfg(not(target_os = "none"))]
43#[macro_export]
44macro_rules! panic_println {
45    ($($arg:tt)*) => {
46        println!($($arg)*);
47    };
48}
49
50/// Prints and returns the value of a given expression for quick and dirty
51/// debugging.
52// Copied from std/macros.rs
53#[cfg(target_os = "none")]
54#[macro_export]
55macro_rules! dbg {
56    // NOTE: We cannot use `concat!` to make a static string as a format argument
57    // of `eprintln!` because `file!` could contain a `{` or
58    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
59    // will be malformed.
60    () => {
61        $crate::println!("[{}:{}]", ::core::file!(), ::core::line!())
62    };
63    ($val:expr $(,)?) => {
64        // Use of `match` here is intentional because it affects the lifetimes
65        // of temporaries - https://stackoverflow.com/a/48732525/1063961
66        match $val {
67            tmp => {
68                $crate::println!("[{}:{}] {} = {:#?}",
69                    ::core::file!(), ::core::line!(), ::core::stringify!($val), &tmp);
70                tmp
71            }
72        }
73    };
74    ($($val:expr),+ $(,)?) => {
75        ($($crate::dbg!($val)),+,)
76    };
77}
78
79/// Returns the value of the specified environment variable.
80///
81/// The value is fetched from the current runtime environment and, if not
82/// present, falls back to the same environment variable set at compile time
83/// (might not be present as well).
84#[allow(unused_macros)]
85macro_rules! hermit_var {
86	($name:expr) => {{
87		use alloc::borrow::Cow;
88
89		match crate::env::var($name) {
90			Some(val) => Some(Cow::from(val)),
91			None => option_env!($name).map(Cow::Borrowed),
92		}
93	}};
94}
95
96/// Tries to fetch the specified environment variable with a default value.
97///
98/// Fetches according to [`hermit_var`] or returns the specified default value.
99#[allow(unused_macros)]
100macro_rules! hermit_var_or {
101	($name:expr, $default:expr) => {
102		hermit_var!($name).as_deref().unwrap_or($default)
103	};
104}