Use c_char instead of i8 for portability

This commit is contained in:
Birk Tjelmeland 2021-08-30 16:44:31 +02:00 committed by 4bb4
parent 06cc3061c1
commit eba023b5f5
3 changed files with 18 additions and 15 deletions

View file

@ -22,6 +22,7 @@ use implot_sys as sys;
pub use self::{context::*, plot::*, plot_elements::*}; pub use self::{context::*, plot::*, plot_elements::*};
use imgui::im_str; use imgui::im_str;
pub use sys::{ImPlotLimits, ImPlotPoint, ImPlotRange, ImVec2, ImVec4}; pub use sys::{ImPlotLimits, ImPlotPoint, ImPlotRange, ImVec2, ImVec4};
use std::os::raw::c_char;
mod context; mod context;
mod plot; mod plot;
@ -575,7 +576,7 @@ pub fn is_plot_y_axis_hovered(y_axis_choice: Option<YAxisChoice>) -> bool {
/// Returns true if the given item in the legend of the current plot is hovered. /// Returns true if the given item in the legend of the current plot is hovered.
pub fn is_legend_entry_hovered(legend_entry: &str) -> bool { pub fn is_legend_entry_hovered(legend_entry: &str) -> bool {
unsafe { sys::ImPlot_IsLegendEntryHovered(im_str!("{}", legend_entry).as_ptr() as *const i8) } unsafe { sys::ImPlot_IsLegendEntryHovered(im_str!("{}", legend_entry).as_ptr() as *const c_char) }
} }
// --- Demo window ------------------------------------------------------------------------------- // --- Demo window -------------------------------------------------------------------------------

View file

@ -8,6 +8,7 @@ pub use imgui::Condition;
use imgui::{im_str, ImString}; use imgui::{im_str, ImString};
use implot_sys as sys; use implot_sys as sys;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use std::os::raw::c_char;
pub use sys::{ImPlotLimits, ImPlotPoint, ImPlotRange, ImVec2, ImVec4}; pub use sys::{ImPlotLimits, ImPlotPoint, ImPlotRange, ImVec2, ImVec4};
const DEFAULT_PLOT_SIZE_X: f32 = 400.0; const DEFAULT_PLOT_SIZE_X: f32 = 400.0;
@ -491,8 +492,8 @@ impl Plot {
let labels_pointer = if let Some(labels_value) = &self.x_tick_labels { let labels_pointer = if let Some(labels_value) = &self.x_tick_labels {
pointer_vec = labels_value pointer_vec = labels_value
.iter() .iter()
.map(|x| x.as_ptr() as *const i8) .map(|x| x.as_ptr() as *const c_char)
.collect::<Vec<*const i8>>(); .collect::<Vec<*const c_char>>();
pointer_vec.as_mut_ptr() pointer_vec.as_mut_ptr()
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
@ -520,8 +521,8 @@ impl Plot {
let labels_pointer = if let Some(labels_value) = &labels { let labels_pointer = if let Some(labels_value) = &labels {
pointer_vec = labels_value pointer_vec = labels_value
.iter() .iter()
.map(|x| x.as_ptr() as *const i8) .map(|x| x.as_ptr() as *const c_char)
.collect::<Vec<*const i8>>(); .collect::<Vec<*const c_char>>();
pointer_vec.as_mut_ptr() pointer_vec.as_mut_ptr()
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()

View file

@ -5,6 +5,7 @@
//! see `plot`. //! see `plot`.
use crate::sys; use crate::sys;
use imgui::{im_str, ImString}; use imgui::{im_str, ImString};
use std::os::raw::c_char;
pub use crate::sys::ImPlotPoint; pub use crate::sys::ImPlotPoint;
@ -31,7 +32,7 @@ impl PlotLine {
} }
unsafe { unsafe {
sys::ImPlot_PlotLinedoublePtrdoublePtr( sys::ImPlot_PlotLinedoublePtrdoublePtr(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const c_char,
x.as_ptr(), x.as_ptr(),
y.as_ptr(), y.as_ptr(),
x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45. This is safe here. x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45. This is safe here.
@ -65,7 +66,7 @@ impl PlotStairs {
} }
unsafe { unsafe {
sys::ImPlot_PlotStairsdoublePtrdoublePtr( sys::ImPlot_PlotStairsdoublePtrdoublePtr(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const c_char,
x.as_ptr(), x.as_ptr(),
y.as_ptr(), y.as_ptr(),
x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45. This is safe here. x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45. This is safe here.
@ -99,7 +100,7 @@ impl PlotScatter {
} }
unsafe { unsafe {
sys::ImPlot_PlotScatterdoublePtrdoublePtr( sys::ImPlot_PlotScatterdoublePtrdoublePtr(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const c_char,
x.as_ptr(), x.as_ptr(),
y.as_ptr(), y.as_ptr(),
x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45. This is safe here. x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45. This is safe here.
@ -163,18 +164,18 @@ impl PlotBars {
let (plot_function, x, y); let (plot_function, x, y);
if self.horizontal_bars { if self.horizontal_bars {
plot_function = sys::ImPlot_PlotBarsHdoublePtrdoublePtr plot_function = sys::ImPlot_PlotBarsHdoublePtrdoublePtr
as unsafe extern "C" fn(*const i8, *const f64, *const f64, i32, f64, i32, i32); as unsafe extern "C" fn(*const c_char, *const f64, *const f64, i32, f64, i32, i32);
x = bar_values; x = bar_values;
y = axis_positions; y = axis_positions;
} else { } else {
plot_function = sys::ImPlot_PlotBarsdoublePtrdoublePtr plot_function = sys::ImPlot_PlotBarsdoublePtrdoublePtr
as unsafe extern "C" fn(*const i8, *const f64, *const f64, i32, f64, i32, i32); as unsafe extern "C" fn(*const c_char, *const f64, *const f64, i32, f64, i32, i32);
x = axis_positions; x = axis_positions;
y = bar_values; y = bar_values;
}; };
plot_function( plot_function(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const c_char,
x.as_ptr(), x.as_ptr(),
y.as_ptr(), y.as_ptr(),
number_of_points as i32, // "as" casts saturate as of Rust 1.45. This is safe here. number_of_points as i32, // "as" casts saturate as of Rust 1.45. This is safe here.
@ -228,7 +229,7 @@ impl PlotText {
unsafe { unsafe {
sys::ImPlot_PlotText( sys::ImPlot_PlotText(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const c_char,
x, x,
y, y,
vertical, vertical,
@ -313,7 +314,7 @@ impl PlotHeatmap {
unsafe { unsafe {
sys::ImPlot_PlotHeatmapdoublePtr( sys::ImPlot_PlotHeatmapdoublePtr(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const c_char,
values.as_ptr(), values.as_ptr(),
number_of_rows as i32, // Not sure why C++ code uses a signed value here number_of_rows as i32, // Not sure why C++ code uses a signed value here
number_of_cols as i32, // Not sure why C++ code uses a signed value here number_of_cols as i32, // Not sure why C++ code uses a signed value here
@ -322,7 +323,7 @@ impl PlotHeatmap {
// "no label" is taken as null pointer in the C++ code, but we're using // "no label" is taken as null pointer in the C++ code, but we're using
// option types in the Rust bindings because they are more idiomatic. // option types in the Rust bindings because they are more idiomatic.
if self.label_format.is_some() { if self.label_format.is_some() {
self.label_format.as_ref().unwrap().as_ptr() as *const i8 self.label_format.as_ref().unwrap().as_ptr() as *const c_char
} else { } else {
std::ptr::null() std::ptr::null()
}, },
@ -369,7 +370,7 @@ impl PlotStems {
} }
unsafe { unsafe {
sys::ImPlot_PlotStemsdoublePtrdoublePtr( sys::ImPlot_PlotStemsdoublePtrdoublePtr(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const c_char,
axis_positions.as_ptr(), axis_positions.as_ptr(),
stem_values.as_ptr(), stem_values.as_ptr(),
number_of_points as i32, // "as" casts saturate as of Rust 1.45. This is safe here. number_of_points as i32, // "as" casts saturate as of Rust 1.45. This is safe here.