axplat/
console.rs

1//! Console input and output.
2
3use core::fmt::{Arguments, Result, Write};
4
5/// Console input and output interface.
6#[def_plat_interface]
7pub trait ConsoleIf {
8    /// Writes given bytes to the console.
9    fn write_bytes(bytes: &[u8]);
10
11    /// Reads bytes from the console into the given mutable slice.
12    ///
13    /// Returns the number of bytes read.
14    fn read_bytes(bytes: &mut [u8]) -> usize;
15}
16
17struct EarlyConsole;
18
19impl Write for EarlyConsole {
20    fn write_str(&mut self, s: &str) -> Result {
21        write_bytes(s.as_bytes());
22        Ok(())
23    }
24}
25
26/// Simple console print operation.
27#[macro_export]
28macro_rules! console_print {
29    ($($arg:tt)*) => {
30        $crate::console::__simple_print(format_args!($($arg)*));
31    }
32}
33
34/// Simple console print operation, with a newline.
35#[macro_export]
36macro_rules! console_println {
37    () => { $crate::ax_print!("\n") };
38    ($($arg:tt)*) => {
39        $crate::console::__simple_print(format_args!("{}\n", format_args!($($arg)*)));
40    }
41}
42
43#[doc(hidden)]
44pub fn __simple_print(fmt: Arguments) {
45    EarlyConsole.write_fmt(fmt).unwrap();
46}