Avoid extra copy when using im_str! formatting

This commit is contained in:
Joonas Javanainen 2015-08-21 18:33:12 +03:00
parent eef67ddecc
commit f6f5dc6162

View File

@ -13,6 +13,7 @@ extern crate sdl2;
use libc::{c_char, c_float, c_int, c_uchar}; use libc::{c_char, c_float, c_int, c_uchar};
use std::borrow::Cow; use std::borrow::Cow;
use std::ffi::CStr; use std::ffi::CStr;
use std::fmt;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use std::slice; use std::slice;
@ -53,7 +54,7 @@ macro_rules! im_str {
unsafe { ::imgui::ImStr::from_bytes(value.as_bytes()) } unsafe { ::imgui::ImStr::from_bytes(value.as_bytes()) }
}); });
($e:tt, $($arg:tt)*) => ({ ($e:tt, $($arg:tt)*) => ({
::imgui::ImStr::from_str(&format!($e, $($arg)*)) ::imgui::ImStr::from_fmt(format_args!($e, $($arg)*))
}) })
} }
@ -74,6 +75,13 @@ impl<'a> ImStr<'a> {
bytes: Cow::Owned(bytes) bytes: Cow::Owned(bytes)
} }
} }
pub fn from_fmt(args: fmt::Arguments) -> ImStr<'a> {
let mut bytes = fmt::format(args).into_bytes();
bytes.push(0);
ImStr {
bytes: Cow::Owned(bytes)
}
}
fn as_ptr(&self) -> *const c_char { self.bytes.as_ptr() as *const c_char } fn as_ptr(&self) -> *const c_char { self.bytes.as_ptr() as *const c_char }
} }