Skip to main content

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