From 96fe2a8e15766ef16dc0dd3e63a904a2983e0847 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Thu, 13 Jul 2017 00:15:03 +0300 Subject: [PATCH] Builder constructors take &Ui instead --- CHANGELOG.markdown | 4 ++-- src/child_frame.rs | 2 +- src/input.rs | 14 +++++------ src/lib.rs | 56 ++++++++++++++++++++++---------------------- src/menus.rs | 4 ++-- src/plothistogram.rs | 2 +- src/plotlines.rs | 2 +- src/progressbar.rs | 2 +- src/sliders.rs | 8 +++---- src/string.rs | 6 ++--- src/trees.rs | 4 ++-- src/window.rs | 2 +- 12 files changed, 53 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 602bb66..cae3ec5 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -18,8 +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. +- All builder constructor functions (e.g. Window::new) now take `&Ui` reference + to tie the lifetime of the builder to it. ### Deprecated diff --git a/src/child_frame.rs b/src/child_frame.rs index a612ceb..1a8d5ae 100644 --- a/src/child_frame.rs +++ b/src/child_frame.rs @@ -18,7 +18,7 @@ pub struct ChildFrame<'ui, 'p> { } impl<'ui, 'p> ChildFrame<'ui, 'p> { - pub(crate) fn new>(name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { + pub fn new>(_: &Ui<'ui>, name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { ChildFrame { name, size: size.into(), diff --git a/src/input.rs b/src/input.rs index 0e70e2d..6d86661 100644 --- a/src/input.rs +++ b/src/input.rs @@ -135,7 +135,7 @@ pub struct InputText<'ui, 'p> { } impl<'ui, 'p> InputText<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, buf: &'p mut ImString) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, buf: &'p mut ImString) -> Self { InputText { label: label, buf: buf, @@ -172,7 +172,7 @@ pub struct InputInt<'ui, 'p> { } impl<'ui, 'p> InputInt<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut i32) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut i32) -> Self { InputInt { label: label, value: value, @@ -209,7 +209,7 @@ pub struct InputFloat<'ui, 'p> { } impl<'ui, 'p> InputFloat<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut f32) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut f32) -> Self { InputFloat { label: label, value: value, @@ -249,7 +249,7 @@ macro_rules! impl_input_floatn { } impl<'ui, 'p> $InputFloatN<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32;$N]) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32;$N]) -> Self { $InputFloatN { label: label, value: value, @@ -290,7 +290,7 @@ macro_rules! impl_input_intn { } impl<'ui, 'p> $InputIntN<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut [i32;$N]) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [i32;$N]) -> Self { $InputIntN { label: label, value: value, @@ -325,7 +325,7 @@ pub struct ColorEdit3<'ui, 'p> { } impl<'ui, 'p> ColorEdit3<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32; 3]) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32; 3]) -> Self { ColorEdit3 { label: label, value: value, @@ -347,7 +347,7 @@ pub struct ColorEdit4<'ui, 'p> { } impl<'ui, 'p> ColorEdit4<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32; 4]) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32; 4]) -> Self { ColorEdit4 { label: label, value: value, diff --git a/src/lib.rs b/src/lib.rs index dd3da93..1a63ecf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -369,7 +369,7 @@ impl<'a> Ui<'a> { // Window impl<'ui> Ui<'ui> { - pub fn window<'p>(&self, name: &'p ImStr) -> Window<'ui, 'p> { Window::new(name) } + pub fn window<'p>(&self, name: &'p ImStr) -> Window<'ui, 'p> { Window::new(self, name) } } // Layout @@ -501,49 +501,49 @@ impl<'ui> Ui<'ui> { label: &'p ImStr, value: &'p mut [f32; 3]) -> ColorEdit3<'ui, 'p> { - ColorEdit3::new(label, value) + ColorEdit3::new(self, label, value) } pub fn color_edit4<'p>(&self, label: &'p ImStr, value: &'p mut [f32; 4]) -> ColorEdit4<'ui, 'p> { - ColorEdit4::new(label, value) + ColorEdit4::new(self, label, value) } pub fn input_text<'p>(&self, label: &'p ImStr, buf: &'p mut ImString) -> InputText<'ui, 'p> { - InputText::new(label, buf) + InputText::new(self, label, buf) } pub fn input_float<'p>(&self, label: &'p ImStr, value: &'p mut f32) -> InputFloat<'ui, 'p> { - InputFloat::new(label, value) + InputFloat::new(self, label, value) } pub fn input_float2<'p>(&self, label: &'p ImStr, value: &'p mut [f32; 2]) -> InputFloat2<'ui, 'p> { - InputFloat2::new(label, value) + InputFloat2::new(self, label, value) } pub fn input_float3<'p>(&self, label: &'p ImStr, value: &'p mut [f32; 3]) -> InputFloat3<'ui, 'p> { - InputFloat3::new(label, value) + InputFloat3::new(self, label, value) } pub fn input_float4<'p>(&self, label: &'p ImStr, value: &'p mut [f32; 4]) -> InputFloat4<'ui, 'p> { - InputFloat4::new(label, value) + InputFloat4::new(self, label, value) } pub fn input_int<'p>(&self, label: &'p ImStr, value: &'p mut i32) -> InputInt<'ui, 'p> { - InputInt::new(label, value) + InputInt::new(self, label, value) } pub fn input_int2<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 2]) -> InputInt2<'ui, 'p> { - InputInt2::new(label, value) + InputInt2::new(self, label, value) } pub fn input_int3<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 3]) -> InputInt3<'ui, 'p> { - InputInt3::new(label, value) + InputInt3::new(self, label, value) } pub fn input_int4<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 4]) -> InputInt4<'ui, 'p> { - InputInt4::new(label, value) + InputInt4::new(self, label, value) } } @@ -555,7 +555,7 @@ impl<'ui> Ui<'ui> { min: f32, max: f32) -> SliderFloat<'ui, 'p> { - SliderFloat::new(label, value, min, max) + SliderFloat::new(self, label, value, min, max) } pub fn slider_float2<'p>(&self, label: &'p ImStr, @@ -563,7 +563,7 @@ impl<'ui> Ui<'ui> { min: f32, max: f32) -> SliderFloat2<'ui, 'p> { - SliderFloat2::new(label, value, min, max) + SliderFloat2::new(self, label, value, min, max) } pub fn slider_float3<'p>(&self, label: &'p ImStr, @@ -571,7 +571,7 @@ impl<'ui> Ui<'ui> { min: f32, max: f32) -> SliderFloat3<'ui, 'p> { - SliderFloat3::new(label, value, min, max) + SliderFloat3::new(self, label, value, min, max) } pub fn slider_float4<'p>(&self, label: &'p ImStr, @@ -579,7 +579,7 @@ impl<'ui> Ui<'ui> { min: f32, max: f32) -> SliderFloat4<'ui, 'p> { - SliderFloat4::new(label, value, min, max) + SliderFloat4::new(self, label, value, min, max) } pub fn slider_int<'p>(&self, label: &'p ImStr, @@ -587,7 +587,7 @@ impl<'ui> Ui<'ui> { min: i32, max: i32) -> SliderInt<'ui, 'p> { - SliderInt::new(label, value, min, max) + SliderInt::new(self, label, value, min, max) } pub fn slider_int2<'p>(&self, label: &'p ImStr, @@ -595,7 +595,7 @@ impl<'ui> Ui<'ui> { min: i32, max: i32) -> SliderInt2<'ui, 'p> { - SliderInt2::new(label, value, min, max) + SliderInt2::new(self, label, value, min, max) } pub fn slider_int3<'p>(&self, label: &'p ImStr, @@ -603,7 +603,7 @@ impl<'ui> Ui<'ui> { min: i32, max: i32) -> SliderInt3<'ui, 'p> { - SliderInt3::new(label, value, min, max) + SliderInt3::new(self, label, value, min, max) } pub fn slider_int4<'p>(&self, label: &'p ImStr, @@ -611,15 +611,15 @@ impl<'ui> Ui<'ui> { min: i32, max: i32) -> SliderInt4<'ui, 'p> { - SliderInt4::new(label, value, min, max) + SliderInt4::new(self, label, value, min, max) } } // Widgets: Trees impl<'ui> Ui<'ui> { - pub fn tree_node<'p>(&self, id: &'p ImStr) -> TreeNode<'ui, 'p> { TreeNode::new(id) } + pub fn tree_node<'p>(&self, id: &'p ImStr) -> TreeNode<'ui, 'p> { TreeNode::new(self, id) } pub fn collapsing_header<'p>(&self, label: &'p ImStr) -> CollapsingHeader<'ui, 'p> { - CollapsingHeader::new(label) + CollapsingHeader::new(self, label) } } @@ -655,8 +655,8 @@ impl<'ui> Ui<'ui> { unsafe { imgui_sys::igEndMenuBar() }; } } - pub fn menu<'p>(&self, label: &'p ImStr) -> Menu<'ui, 'p> { Menu::new(label) } - pub fn menu_item<'p>(&self, label: &'p ImStr) -> MenuItem<'ui, 'p> { MenuItem::new(label) } + pub fn menu<'p>(&self, label: &'p ImStr) -> Menu<'ui, 'p> { Menu::new(self, label) } + pub fn menu_item<'p>(&self, label: &'p ImStr) -> MenuItem<'ui, 'p> { MenuItem::new(self, label) } } // Widgets: Popups @@ -716,13 +716,13 @@ impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> { pub fn plot_lines<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotLines<'ui, 'p> { - PlotLines::new(label, values) + PlotLines::new(self, label, values) } } impl<'ui> Ui<'ui> { pub fn plot_histogram<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotHistogram<'ui, 'p> { - PlotHistogram::new(label, values) + PlotHistogram::new(self, 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<'ui, 'p> { ProgressBar::new(fraction) } + pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'ui, 'p> { ProgressBar::new(self, 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<'ui, 'p> { ChildFrame::new(name, size.into()) } + pub fn child_frame<'p, S: Into>(&self, name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { ChildFrame::new(self, name, size.into()) } } impl<'ui> Ui<'ui> { diff --git a/src/menus.rs b/src/menus.rs index 6d99e80..8598ee1 100644 --- a/src/menus.rs +++ b/src/menus.rs @@ -12,7 +12,7 @@ pub struct Menu<'ui, 'p> { } impl<'ui, 'p> Menu<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr) -> Self { Menu { label: label, enabled: true, @@ -43,7 +43,7 @@ pub struct MenuItem<'ui, 'p> { } impl<'ui, 'p> MenuItem<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr) -> Self { MenuItem { label: label, shortcut: None, diff --git a/src/plothistogram.rs b/src/plothistogram.rs index 23e04d3..a00ebd3 100644 --- a/src/plothistogram.rs +++ b/src/plothistogram.rs @@ -18,7 +18,7 @@ pub struct PlotHistogram<'ui, 'p> { } impl<'ui, 'p> PlotHistogram<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, values: &'p [f32]) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, values: &'p [f32]) -> Self { PlotHistogram { label: label, values: values, diff --git a/src/plotlines.rs b/src/plotlines.rs index 18575ab..a9c562d 100644 --- a/src/plotlines.rs +++ b/src/plotlines.rs @@ -18,7 +18,7 @@ pub struct PlotLines<'ui, 'p> { } impl<'ui, 'p> PlotLines<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, values: &'p [f32]) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, values: &'p [f32]) -> Self { PlotLines { label: label, values: values, diff --git a/src/progressbar.rs b/src/progressbar.rs index 0694e21..112935c 100644 --- a/src/progressbar.rs +++ b/src/progressbar.rs @@ -20,7 +20,7 @@ impl<'ui, 'p> ProgressBar<'ui, 'p> { /// The progress bar will be automatically sized to fill /// the entire width of the window if no custom size is /// specified. - pub(crate) fn new(fraction: f32) -> Self { + pub fn new(_: &Ui<'ui>, fraction: f32) -> Self { ProgressBar { fraction: fraction, size: ImVec2::new(-1.0, 0.0), diff --git a/src/sliders.rs b/src/sliders.rs index 730c618..bcb6899 100644 --- a/src/sliders.rs +++ b/src/sliders.rs @@ -16,7 +16,7 @@ pub struct SliderInt<'ui, 'p> { } impl<'ui, 'p> SliderInt<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut i32, min: i32, max: i32) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut i32, min: i32, max: i32) -> Self { SliderInt { label: label, value: value, @@ -55,7 +55,7 @@ macro_rules! impl_slider_intn { } impl<'ui, 'p> $SliderIntN<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut [i32; $N], min: i32, max: i32) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [i32; $N], min: i32, max: i32) -> Self { $SliderIntN { label: label, value: value, @@ -100,7 +100,7 @@ pub struct SliderFloat<'ui, 'p> { } impl<'ui, 'p> SliderFloat<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut f32, min: f32, max: f32) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut f32, min: f32, max: f32) -> Self { SliderFloat { label: label, value: value, @@ -147,7 +147,7 @@ macro_rules! impl_slider_floatn { } impl<'ui, 'p> $SliderFloatN<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32; $N], min: f32, max: f32) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32; $N], min: f32, max: f32) -> Self { $SliderFloatN { label: label, value: value, diff --git a/src/string.rs b/src/string.rs index e079cc5..2b5604f 100644 --- a/src/string.rs +++ b/src/string.rs @@ -116,15 +116,15 @@ 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 { + pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &ImStr { ImStr::from_utf8_with_nul_unchecked(bytes) } - pub unsafe fn from_utf8_with_nul_unchecked<'a>(bytes: &'a [u8]) -> &'a ImStr { + pub unsafe fn from_utf8_with_nul_unchecked(bytes: &[u8]) -> &ImStr { mem::transmute(bytes) } pub fn as_ptr(&self) -> *const c_char { self.0.as_ptr() } pub fn to_str(&self) -> &str { - unsafe { str::from_utf8_unchecked(&self.0.to_bytes()) } + unsafe { str::from_utf8_unchecked(self.0.to_bytes()) } } } diff --git a/src/trees.rs b/src/trees.rs index c8d2708..f676161 100644 --- a/src/trees.rs +++ b/src/trees.rs @@ -16,7 +16,7 @@ pub struct TreeNode<'ui, 'p> { } impl<'ui, 'p> TreeNode<'ui, 'p> { - pub(crate) fn new(id: &'p ImStr) -> Self { + pub fn new(_: &Ui<'ui>, id: &'p ImStr) -> Self { TreeNode { id: id, label: None, @@ -62,7 +62,7 @@ pub struct CollapsingHeader<'ui, 'p> { } impl<'ui, 'p> CollapsingHeader<'ui, 'p> { - pub(crate) fn new(label: &'p ImStr) -> Self { + pub fn new(_: &Ui<'ui>, label: &'p ImStr) -> Self { CollapsingHeader { label: label, flags: ImGuiTreeNodeFlags::empty(), diff --git a/src/window.rs b/src/window.rs index b56ca2e..6672bdc 100644 --- a/src/window.rs +++ b/src/window.rs @@ -26,7 +26,7 @@ pub struct Window<'ui, 'p> { } impl<'ui, 'p> Window<'ui, 'p> { - pub(crate) fn new(name: &'p ImStr) -> Window<'ui, 'p> { + pub fn new(_: &Ui<'ui>, name: &'p ImStr) -> Window<'ui, 'p> { Window { pos: (0.0, 0.0), pos_cond: ImGuiSetCond::empty(),