mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-11 13:38:35 +00:00
Avoid extra copy when using im_str! formatting
This commit is contained in:
parent
eef67ddecc
commit
f6f5dc6162
10
src/lib.rs
10
src/lib.rs
@ -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 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user