42 lines
640 B
Rust
42 lines
640 B
Rust
use core::fmt;
|
|
|
|
use super::posix;
|
|
use super::string;
|
|
|
|
pub struct OStream {
|
|
file: *mut libc::FILE
|
|
}
|
|
|
|
pub unsafe fn stdout() -> OStream {
|
|
OStream { file: posix::stdout }
|
|
}
|
|
|
|
pub unsafe fn stderr() -> OStream {
|
|
OStream { file: posix::stderr }
|
|
}
|
|
|
|
impl OStream {
|
|
pub fn write(&mut self, b: &[u8]) {
|
|
unsafe {
|
|
libc::fwrite(
|
|
b.as_ptr() as *const libc::c_void,
|
|
1,
|
|
b.len(),
|
|
self.file,
|
|
);
|
|
}
|
|
}
|
|
|
|
pub fn puts(&mut self, s: &string::CStr) {
|
|
unsafe {
|
|
libc::fputs(s.as_ptr(), self.file);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Write for OStream {
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
self.write(s.as_bytes());
|
|
Ok(())
|
|
}
|
|
}
|