summaryrefslogtreecommitdiff
path: root/tools/rust/src/print.rs
blob: b9f0ead6973e154c8b54eec4635f61ee1b8f6ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: MIT
use core::ffi::c_void;

extern "C" {
    fn iodev_console_write(buf: *const c_void, len: u64);
}

pub struct IODevConsoleWriter;

impl core::fmt::Write for IODevConsoleWriter {
    #[inline]
    fn write_str(&mut self, msg: &str) -> core::fmt::Result {
        write(msg)
    }
}

impl IODevConsoleWriter {
    #[inline]
    pub fn write_fmt(args: core::fmt::Arguments) -> core::fmt::Result {
        core::fmt::Write::write_fmt(&mut Self, args)
    }

    #[inline]
    pub fn write_str(msg: &str) -> core::fmt::Result {
        write(msg)
    }

    #[inline]
    pub fn write_nl() -> core::fmt::Result {
        write("\n")
    }
}

#[inline]
fn write(msg: &str) -> core::fmt::Result {
    unsafe { iodev_console_write(msg.as_ptr() as _, msg.len() as u64) };
    Ok(())
}

#[macro_export]
macro_rules! println {
    () => { $crate::println!("") };
    ($($arg:tt)*) => {
        #[allow(unused_must_use)]
        {
            $crate::print::IODevConsoleWriter::write_fmt(format_args!($($arg)*));
            $crate::print::IODevConsoleWriter::write_nl();
        }
    };
}

#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {
        #[allow(unused_must_use)]
        {
            $crate::print::IODevConsoleWriter::write_fmt(format_args!($($arg)*));
        }
    };
}