summaryrefslogtreecommitdiff
path: root/tools/rust/src/print.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tools/rust/src/print.rs')
-rw-r--r--tools/rust/src/print.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/tools/rust/src/print.rs b/tools/rust/src/print.rs
new file mode 100644
index 0000000..b9f0ead
--- /dev/null
+++ b/tools/rust/src/print.rs
@@ -0,0 +1,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)*));
+ }
+ };
+}