From 3655dfa081794280593ed90af6d4373b6e654e39 Mon Sep 17 00:00:00 2001 From: Michael Fairley Date: Wed, 30 May 2018 23:59:06 -0500 Subject: [PATCH 1/2] Allow using strings and pointers with push_id --- src/lib.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9053d5d..7026b39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -562,10 +562,50 @@ impl<'ui> Ui<'ui> { } } +pub enum ImId<'a> { + Int(i32), + Str(&'a str), + Ptr(*const c_void), +} + +impl From for ImId<'static> { + fn from(i: i32) -> Self { ImId::Int(i) } +} + +impl<'a, T: ?Sized + AsRef> From<&'a T> for ImId<'a> { + fn from(s: &'a T) -> Self { ImId::Str(s.as_ref()) } +} + +impl From<*const T> for ImId<'static> { + fn from(p: *const T) -> Self { ImId::Ptr(p as *const c_void) } +} + +impl 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>>(&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.add(s.len()) as *const c_char; + sys::igPushIDStrRange(start, end); + } + ImId::Ptr(p) => { + sys::igPushIDPtr(p as *const c_void); + } + } + } + } /// Pops an identifier from the ID stack. /// @@ -574,9 +614,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(&self, id: i32, f: F) + pub fn with_id<'a, F, I>(&self, id: I, f: F) where F: FnOnce(), + I: Into>, { self.push_id(id); f(); From 3a070ff124f5c4542649b6ffe4344acbc2d7f6c9 Mon Sep 17 00:00:00 2001 From: Michael Fairley Date: Sun, 3 Jun 2018 10:28:54 -0500 Subject: [PATCH 2/2] Remove use of rust feature newer than oldest supported version --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7026b39..078dd26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -597,7 +597,7 @@ impl<'ui> Ui<'ui> { } ImId::Str(s) => { let start = s.as_ptr() as *const c_char; - let end = start.add(s.len()) as *const c_char; + let end = start.offset(s.len() as isize); sys::igPushIDStrRange(start, end); } ImId::Ptr(p) => {