printf/lib.rs
1//! This crate provides a method to convert printf-style calls to a rust formatter
2extern crate libc;
3
4use std::ffi::CStr;
5
6use libc as c;
7
8#[link(name = "printf_wrapper")]
9extern "C" {
10 fn printf_wrapper(format: *const c::c_char, args: *mut c::c_void) -> *mut c::c_char;
11}
12
13/// Take a printf c-string and variadic array, and write equiv. out to the formatter
14///
15/// # Safety
16/// This function is UB if the va_list doesn't match the format (c printf syntax)
17///
18/// There must be no panics in this function, so quite often errors are deliberately ignored
19pub unsafe fn printf(format: *const c::c_char, args: *mut c::c_void) -> String
20{
21 let out_char_p = printf_wrapper(format, args);
22 let output = CStr::from_ptr(out_char_p).to_string_lossy().into_owned();
23 c::free(out_char_p as *mut c::c_void);
24 output
25}