From a48f0bdbd9070de7e1d8526c54c299fa9d36c88d Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Wed, 12 Jul 2017 23:51:24 +0300 Subject: [PATCH] Add missing PhantomData to builders I keep forgetting about this :/ :/ :/ --- CHANGELOG.markdown | 7 +++++++ src/child_frame.rs | 13 +++++++------ src/lib.rs | 8 ++++---- src/plothistogram.rs | 9 ++++++--- src/plotlines.rs | 10 +++++++--- src/progressbar.rs | 10 ++++++---- 6 files changed, 37 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 5f6d006..602bb66 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -18,6 +18,8 @@ - Button, selectable, histogram, plotlines, and progress bar accept size with `Into` - `ImString::new` always succeeds and any interior NULs truncate the string. **Breaking change** +- All builder constructor functions (e.g. Window::new) are no longer public. + The only safe way to construct builders is through a `&Ui` reference. ### Deprecated @@ -25,6 +27,11 @@ - `ImString::from_bytes_unchecked` (renamed to `ImString::from_utf8_unchecked`) - `ImStr::from_bytes_unchecked` (renamed to `ImStr::from_utf8_with_nul_unchecked`) +### Fixed + +- Histogram, plotlines, progressbar builders were not tied to the `&Ui` + lifetime, so it was possible to misuse them. + ## [0.0.14] - 2017-06-18 ### Added diff --git a/src/child_frame.rs b/src/child_frame.rs index 84d8ad5..a612ceb 100644 --- a/src/child_frame.rs +++ b/src/child_frame.rs @@ -1,8 +1,7 @@ use imgui_sys; -use ImStr; -use ImVec2; -use ImGuiWindowFlags; +use std::marker::PhantomData; +use super::{ImStr, ImVec2, ImGuiWindowFlags, Ui}; use super::{ImGuiWindowFlags_NoMove, ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar, @@ -11,18 +10,20 @@ use super::{ImGuiWindowFlags_NoMove, ImGuiWindowFlags_NoScrollbar, ImGuiWindowFl ImGuiWindowFlags_AlwaysHorizontalScrollbar, ImGuiWindowFlags_AlwaysUseWindowPadding}; #[must_use] -pub struct ChildFrame<'p> { +pub struct ChildFrame<'ui, 'p> { name: &'p ImStr, size: ImVec2, flags: ImGuiWindowFlags, + _phantom: PhantomData<&'ui Ui<'ui>>, } -impl<'p> ChildFrame<'p> { - pub(crate) fn new>(name: &'p ImStr, size: S) -> ChildFrame<'p> { +impl<'ui, 'p> ChildFrame<'ui, 'p> { + pub(crate) fn new>(name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { ChildFrame { name, size: size.into(), flags: ImGuiWindowFlags::empty(), + _phantom: PhantomData, } } #[inline] diff --git a/src/lib.rs b/src/lib.rs index 0d6e7c4..dd3da93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -715,13 +715,13 @@ impl<'ui> Ui<'ui> { } impl<'ui> Ui<'ui> { - pub fn plot_lines<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotLines<'p> { + pub fn plot_lines<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotLines<'ui, 'p> { PlotLines::new(label, values) } } impl<'ui> Ui<'ui> { - pub fn plot_histogram<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotHistogram<'p> { + pub fn plot_histogram<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotHistogram<'ui, 'p> { PlotHistogram::new(label, values) } } @@ -753,7 +753,7 @@ impl<'ui> Ui<'ui> { /// .overlay_text(im_str!("Progress!")) /// .build(); /// ``` - pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'p> { ProgressBar::new(fraction) } + pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'ui, 'p> { ProgressBar::new(fraction) } } impl<'ui> Ui<'ui> { @@ -777,7 +777,7 @@ impl<'ui> Ui<'ui> { /// ui.text_colored((1.0, 0.0, 0.0, 1.0), im_str!("hello mate!")); /// }); /// }); - pub fn child_frame<'p, S: Into>(&self, name: &'p ImStr, size: S) -> ChildFrame<'p> { ChildFrame::new(name, size.into()) } + pub fn child_frame<'p, S: Into>(&self, name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { ChildFrame::new(name, size.into()) } } impl<'ui> Ui<'ui> { diff --git a/src/plothistogram.rs b/src/plothistogram.rs index f734aae..23e04d3 100644 --- a/src/plothistogram.rs +++ b/src/plothistogram.rs @@ -1,11 +1,12 @@ use imgui_sys; use std::{f32, mem, ptr}; +use std::marker::PhantomData; use std::os::raw::c_float; -use super::{ImStr, ImVec2}; +use super::{ImStr, ImVec2, Ui}; #[must_use] -pub struct PlotHistogram<'p> { +pub struct PlotHistogram<'ui, 'p> { label: &'p ImStr, values: &'p [f32], values_offset: usize, @@ -13,9 +14,10 @@ pub struct PlotHistogram<'p> { scale_min: f32, scale_max: f32, graph_size: ImVec2, + _phantom: PhantomData<&'ui Ui<'ui>>, } -impl<'p> PlotHistogram<'p> { +impl<'ui, 'p> PlotHistogram<'ui, 'p> { pub(crate) fn new(label: &'p ImStr, values: &'p [f32]) -> Self { PlotHistogram { label: label, @@ -25,6 +27,7 @@ impl<'p> PlotHistogram<'p> { scale_min: f32::MAX, scale_max: f32::MAX, graph_size: ImVec2::new(0.0f32, 0.0f32), + _phantom: PhantomData, } } diff --git a/src/plotlines.rs b/src/plotlines.rs index 3566fb5..18575ab 100644 --- a/src/plotlines.rs +++ b/src/plotlines.rs @@ -1,10 +1,12 @@ use imgui_sys; use std::{f32, mem, ptr}; +use std::marker::PhantomData; use std::os::raw::c_float; -use super::{ImStr, ImVec2}; +use super::{ImStr, ImVec2, Ui}; + #[must_use] -pub struct PlotLines<'p> { +pub struct PlotLines<'ui, 'p> { label: &'p ImStr, values: &'p [f32], values_offset: usize, @@ -12,9 +14,10 @@ pub struct PlotLines<'p> { scale_min: f32, scale_max: f32, graph_size: ImVec2, + _phantom: PhantomData<&'ui Ui<'ui>>, } -impl<'p> PlotLines<'p> { +impl<'ui, 'p> PlotLines<'ui, 'p> { pub(crate) fn new(label: &'p ImStr, values: &'p [f32]) -> Self { PlotLines { label: label, @@ -24,6 +27,7 @@ impl<'p> PlotLines<'p> { scale_min: f32::MAX, scale_max: f32::MAX, graph_size: ImVec2::new(0.0f32, 0.0f32), + _phantom: PhantomData, } } diff --git a/src/progressbar.rs b/src/progressbar.rs index d316320..0694e21 100644 --- a/src/progressbar.rs +++ b/src/progressbar.rs @@ -1,19 +1,20 @@ #![warn(missing_docs)] - use imgui_sys; +use std::marker::PhantomData; use std::ptr; -use super::{ImStr, ImVec2}; +use super::{ImStr, ImVec2, Ui}; /// Progress bar widget. #[must_use] -pub struct ProgressBar<'p> { +pub struct ProgressBar<'ui, 'p> { fraction: f32, size: ImVec2, overlay_text: Option<&'p ImStr>, + _phantom: PhantomData<&'ui Ui<'ui>>, } -impl<'p> ProgressBar<'p> { +impl<'ui, 'p> ProgressBar<'ui, 'p> { /// Creates a progress bar with a given fraction showing /// the progress (0.0 = 0%, 1.0 = 100%). /// The progress bar will be automatically sized to fill @@ -24,6 +25,7 @@ impl<'p> ProgressBar<'p> { fraction: fraction, size: ImVec2::new(-1.0, 0.0), overlay_text: None, + _phantom: PhantomData, } }