Merge pull request #133 from michaelfairley/pushid_overloads_take3

Allow using strings and pointers with push_id
This commit is contained in:
Joonas Javanainen 2018-06-03 20:25:31 +03:00 committed by GitHub
commit d8676b90de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@ pub extern crate imgui_sys as sys;
use std::ffi::CStr;
use std::mem;
use std::os::raw::{c_char, c_float, c_int, c_uchar};
use std::os::raw::{c_char, c_float, c_int, c_uchar, c_void};
use std::ptr;
use std::slice;
use std::str;
@ -603,10 +603,50 @@ impl<'ui> Ui<'ui> {
}
}
pub enum ImId<'a> {
Int(i32),
Str(&'a str),
Ptr(*const c_void),
}
impl From<i32> for ImId<'static> {
fn from(i: i32) -> Self { ImId::Int(i) }
}
impl<'a, T: ?Sized + AsRef<str>> From<&'a T> for ImId<'a> {
fn from(s: &'a T) -> Self { ImId::Str(s.as_ref()) }
}
impl<T> From<*const T> for ImId<'static> {
fn from(p: *const T) -> Self { ImId::Ptr(p as *const c_void) }
}
impl<T> From<*mut T> for ImId<'static> {
fn from(p: *mut T) -> Self { ImId::Ptr(p as *const T as *const c_void) }
}
// ID scopes
impl<'ui> Ui<'ui> {
/// Pushes an identifier to the ID stack.
pub fn push_id(&self, id: i32) { unsafe { sys::igPushIDInt(id) }; }
pub fn push_id<'a, I: Into<ImId<'a>>>(&self, id: I) {
let id = id.into();
unsafe {
match id {
ImId::Int(i) => {
sys::igPushIDInt(i);
}
ImId::Str(s) => {
let start = s.as_ptr() as *const c_char;
let end = start.offset(s.len() as isize);
sys::igPushIDStrRange(start, end);
}
ImId::Ptr(p) => {
sys::igPushIDPtr(p as *const c_void);
}
}
}
}
/// Pops an identifier from the ID stack.
///
@ -615,9 +655,10 @@ impl<'ui> Ui<'ui> {
pub fn pop_id(&self) { unsafe { sys::igPopID() }; }
/// Runs a function after temporarily pushing a value to the ID stack.
pub fn with_id<F>(&self, id: i32, f: F)
pub fn with_id<'a, F, I>(&self, id: I, f: F)
where
F: FnOnce(),
I: Into<ImId<'a>>,
{
self.push_id(id);
f();