Created
March 2, 2015 18:41
-
-
Save zrsmith92/6d52866c4cc3af538872 to your computer and use it in GitHub Desktop.
Rust printf interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use libc::{c_char, c_int}; | |
extern { | |
#[allow(dead_code)] | |
pub fn puts(str: *const c_char) -> c_int; | |
#[allow(dead_code)] | |
pub fn printf(format: *const c_char, ...) -> c_int; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![macro_use] | |
macro_rules! cstr { | |
($arg:expr) => (concat!($arg, "\0")) | |
} | |
macro_rules! print { | |
($str:expr) => (basic_print!(cstr!($str))) | |
} | |
macro_rules! basic_print { | |
($str:expr) => (unsafe { | |
use external::puts; | |
let (ptr, _): (*const libc::c_char, usize) = core::mem::transmute($str); | |
puts(ptr); | |
}) | |
} | |
macro_rules! printf { | |
($str:expr, $($args:expr),*) => (unsafe { | |
use external::printf; | |
let s = cstr!($str); | |
let (ptr, _): (*const libc::c_char, usize) = core::mem::transmute(s); | |
printf(ptr, $($args),*); | |
}) | |
} | |
macro_rules! println { | |
($str:expr) => (print!(concat!($str, "\n"))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![feature(lang_items)] | |
#[allow(unstable)] #[macro_use] extern crate core; | |
#[allow(unstable)] extern crate libc; | |
#[allow(unstable)] extern crate alloc; | |
#[allow(unstable)] #[macro_use] extern crate collections; | |
use collections::vec::Vec; | |
use collections::string::String; | |
mod external; | |
mod macros; | |
#[start] | |
fn main(_: isize, _: *const *const u8) -> isize { | |
let v: Vec<i32> = vec![1, 2, 3, 4]; | |
printf!("Vec Length: %d\n", v.len()); | |
return 0; | |
} | |
#[lang = "stack_exhausted"] extern fn stack_exhausted() {} | |
#[lang = "eh_personality"] extern fn eh_personality() {} | |
#[lang = "panic_fmt"] extern fn panic_fmt() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment