aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/io/print.rs
blob: 8a5851b146b20b0be138074f258c2050dc7b86e5 (plain) (blame)
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
use fmt;
use io::{self, Write};

// NOTE: We're just gonna use the spin mutex until we figure out how to properly
// implement mutexes with ctrulib functions
use spin::Mutex;
use libc;

pub static STDOUT: Mutex<StdoutRaw> = Mutex::new(StdoutRaw(()));

pub struct StdoutRaw(());

#[stable(feature = "3ds", since = "1.0.0")]
impl Write for StdoutRaw {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        unsafe {
            // devkitPro's version of write(2) fails if zero bytes are written,
            // so let's just exit if the buffer size is zero
            if buf.is_empty() {
                return Ok(buf.len())
            }
            libc::write(libc::STDOUT_FILENO, buf.as_ptr() as *const _, buf.len());
            Ok(buf.len())
        }
    }

    fn flush(&mut self) -> io::Result<()> { Ok(()) }
}

#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
    STDOUT.lock().write_fmt(args).unwrap();
}