Skip to content

Commit

Permalink
Merge pull request #97 from byeongkeunahn/bprint-and-bprintln-macros
Browse files Browse the repository at this point in the history
Implement bprint and bprintln macros
  • Loading branch information
byeongkeunahn authored Jun 6, 2024
2 parents 8af1e04 + 715bb72 commit 8cdefd4
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion basm-std/src/platform/io/writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::platform::services;
use alloc::string::String;
use alloc::string::{String, ToString};
use core::fmt::Arguments;
use core::mem::MaybeUninit;

pub struct Writer<const N: usize = { super::DEFAULT_BUF_SIZE }> {
Expand Down Expand Up @@ -427,6 +428,38 @@ macro_rules! impl_print{

impl_print!(i8 u8 i16 u16 i32 u32 i64 u64 f64 i128 u128 isize usize char);

impl<'a, const N: usize> Print<Arguments<'a>> for Writer<N> {
fn print(&mut self, x: Arguments<'a>) {
if let Some(s) = x.as_str() {
self.print(s);
} else {
self.print(x.to_string());
}
}

fn println(&mut self, x: Arguments<'a>) {
if let Some(s) = x.as_str() {
self.println(s);
} else {
self.println(x.to_string());
}
}
}

#[macro_export]
macro_rules! bprint {
($writer:tt, $($arg:tt)*) => {{
$writer.print(core::format_args!($($arg)*));
}};
}

#[macro_export]
macro_rules! bprintln {
($writer:tt, $($arg:tt)*) => {{
$writer.println(core::format_args!($($arg)*));
}};
}

/*
#[cfg(test)]
mod test {
Expand Down

0 comments on commit 8cdefd4

Please sign in to comment.