Restructure ImString/ImStr creation functions

This commit is contained in:
Joonas Javanainen 2017-07-12 23:30:27 +03:00
parent 401026a697
commit 932319256f
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
4 changed files with 35 additions and 14 deletions

View File

@ -9,10 +9,21 @@
- Support for scoped style customization
- Support for scoped color customization
- Support for child frames
- Unsafe ImString/ImStr creation functions for advanced users:
+ `ImString::from_utf8_unchecked` (renamed from `ImString::from_bytes_unchecked`)
+ `ImString::from_utf8_with_nul_unchecked`)
+ `ImStr::from_utf8_with_nul_unchecked` (renamed from `ImStr::from_bytes_unchecked`)
### Changed
- Button, selectable, and progress bar accept size with `Into<ImVec2>`
- `ImString::new` always succeeds and any interior NULs truncate the string. **Breaking change**
### Deprecated
- `ImString::from_string_unchecked` (please use `ImString::new`)
- `ImString::from_bytes_unchecked` (renamed to `ImString::from_utf8_unchecked`)
- `ImStr::from_bytes_unchecked` (renamed to `ImStr::from_utf8_with_nul_unchecked`)
## [0.0.14] - 2017-06-18

View File

@ -71,15 +71,13 @@ pub struct ImGui {
#[macro_export]
macro_rules! im_str {
($e:tt) => ({
debug_assert!(!($e).as_bytes().contains(&0));
let value = concat!($e, "\0");
unsafe { ::imgui::ImStr::from_bytes_unchecked(value.as_bytes()) }
unsafe { ::imgui::ImStr::from_utf8_with_nul_unchecked(value.as_bytes()) }
});
($e:tt, $($arg:tt)*) => ({
let mut bytes: Vec<u8> = format!($e, $($arg)*).into();
debug_assert!(!bytes.contains(&0));
bytes.push(b'\0');
unsafe { &::imgui::ImString::from_vec_unchecked(bytes) }
unsafe { &::imgui::ImString::from_utf8_unchecked(bytes) }
})
}

View File

@ -22,7 +22,7 @@ impl<'ui, 'p> SliderInt<'ui, 'p> {
value: value,
min: min,
max: max,
display_format: unsafe { ImStr::from_bytes_unchecked(b"%.0f\0") },
display_format: unsafe { ImStr::from_utf8_with_nul_unchecked(b"%.0f\0") },
_phantom: PhantomData,
}
}
@ -61,7 +61,7 @@ macro_rules! impl_slider_intn {
value: value,
min: min,
max: max,
display_format: unsafe { ImStr::from_bytes_unchecked(b"%.0f\0") },
display_format: unsafe { ImStr::from_utf8_with_nul_unchecked(b"%.0f\0") },
_phantom: PhantomData,
}
}
@ -106,7 +106,7 @@ impl<'ui, 'p> SliderFloat<'ui, 'p> {
value: value,
min: min,
max: max,
display_format: unsafe { ImStr::from_bytes_unchecked(b"%.3f\0") },
display_format: unsafe { ImStr::from_utf8_with_nul_unchecked(b"%.3f\0") },
power: 1.0,
_phantom: PhantomData,
}
@ -153,7 +153,7 @@ macro_rules! impl_slider_floatn {
value: value,
min: min,
max: max,
display_format: unsafe { ImStr::from_bytes_unchecked(b"%.3f\0") },
display_format: unsafe { ImStr::from_utf8_with_nul_unchecked(b"%.3f\0") },
power: 1.0,
_phantom: PhantomData,
}

View File

@ -1,5 +1,5 @@
use std::borrow::Borrow;
use std::ffi::{CStr, CString, NulError};
use std::ffi::{CStr};
use std::fmt;
use std::mem;
use std::ops::Deref;
@ -10,21 +10,29 @@ use std::str;
pub struct ImString(Vec<u8>);
impl ImString {
pub fn new<T: Into<String>>(t: T) -> Result<ImString, NulError> {
CString::new(t.into()).map(|cstring| ImString(cstring.into_bytes_with_nul()))
pub fn new<T: Into<String>>(value: T) -> ImString {
unsafe { ImString::from_utf8_unchecked(value.into().into_bytes()) }
}
pub fn with_capacity(capacity: usize) -> ImString {
let mut v = Vec::with_capacity(capacity + 1);
v.push(b'\0');
ImString(v)
}
#[deprecated(since = "0.0.15", note = "please use ImString::new instead")]
pub unsafe fn from_string_unchecked(s: String) -> ImString {
ImString::from_vec_unchecked(s.into())
ImString::new(s)
}
pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> ImString {
#[deprecated(since = "0.0.15", note = "please use ImString::from_utf8_unchecked instead")]
pub unsafe fn from_vec_unchecked(v: Vec<u8>) -> ImString {
ImString::from_utf8_unchecked(v)
}
pub unsafe fn from_utf8_unchecked(mut v: Vec<u8>) -> ImString {
v.push(b'\0');
ImString(v)
}
pub unsafe fn from_utf8_with_nul_unchecked(v: Vec<u8>) -> ImString {
ImString(v)
}
pub fn clear(&mut self) {
self.0.clear();
self.0.push(b'\0');
@ -96,7 +104,7 @@ pub struct ImStr(CStr);
impl<'a> Default for &'a ImStr {
fn default() -> &'a ImStr {
static SLICE: &'static [u8] = &[0];
unsafe { ImStr::from_bytes_unchecked(SLICE) }
unsafe { ImStr::from_utf8_with_nul_unchecked(SLICE) }
}
}
@ -107,7 +115,11 @@ impl fmt::Debug for ImStr {
}
impl ImStr {
#[deprecated(since = "0.0.15", note = "please use ImStr::from_bytes_with_nul_unchecked instead")]
pub unsafe fn from_bytes_unchecked<'a>(bytes: &'a [u8]) -> &'a ImStr {
ImStr::from_utf8_with_nul_unchecked(bytes)
}
pub unsafe fn from_utf8_with_nul_unchecked<'a>(bytes: &'a [u8]) -> &'a ImStr {
mem::transmute(bytes)
}
pub fn as_ptr(&self) -> *const c_char { self.0.as_ptr() }