From cd63747f2bea9e4a010f9dbb20452662d08e0972 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 11 Nov 2023 21:25:56 +0100 Subject: [PATCH 1/2] Replace `Id::nul()` with `Id::NULL` --- crates/egui/src/containers/popup.rs | 4 ++-- crates/egui/src/id.rs | 3 +++ crates/egui/src/widgets/color_picker.rs | 2 +- crates/egui_extras/src/syntax_highlighting.rs | 2 +- crates/egui_plot/src/lib.rs | 10 +++++----- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/egui/src/containers/popup.rs b/crates/egui/src/containers/popup.rs index 78be080ba47..a82c82ee288 100644 --- a/crates/egui/src/containers/popup.rs +++ b/crates/egui/src/containers/popup.rs @@ -13,11 +13,11 @@ pub(crate) struct TooltipState { impl TooltipState { pub fn load(ctx: &Context) -> Option { - ctx.data_mut(|d| d.get_temp(Id::null())) + ctx.data_mut(|d| d.get_temp(Id::NULL)) } fn store(self, ctx: &Context) { - ctx.data_mut(|d| d.insert_temp(Id::null(), self)); + ctx.data_mut(|d| d.insert_temp(Id::NULL, self)); } fn individual_tooltip_size(&self, common_id: Id, index: usize) -> Option { diff --git a/crates/egui/src/id.rs b/crates/egui/src/id.rs index 75cc9f8568f..9caa3bafd22 100644 --- a/crates/egui/src/id.rs +++ b/crates/egui/src/id.rs @@ -35,6 +35,9 @@ impl Id { /// /// The null [`Id`] is still a valid id to use in all circumstances, /// though obviously it will lead to a lot of collisions if you do use it! + pub const NULL: Self = Self(0); + + #[deprecated = "Use Id::NULL"] pub fn null() -> Self { Self(0) } diff --git a/crates/egui/src/widgets/color_picker.rs b/crates/egui/src/widgets/color_picker.rs index 5958d0758dc..a3752ef684c 100644 --- a/crates/egui/src/widgets/color_picker.rs +++ b/crates/egui/src/widgets/color_picker.rs @@ -442,5 +442,5 @@ fn color_cache_set(ctx: &Context, rgba: impl Into, hsva: Hsva) { // To ensure we keep hue slider when `srgba` is gray we store the full [`Hsva`] in a cache: fn use_color_cache(ctx: &Context, f: impl FnOnce(&mut FixedCache) -> R) -> R { - ctx.data_mut(|d| f(d.get_temp_mut_or_default(Id::null()))) + ctx.data_mut(|d| f(d.get_temp_mut_or_default(Id::NULL))) } diff --git a/crates/egui_extras/src/syntax_highlighting.rs b/crates/egui_extras/src/syntax_highlighting.rs index 775d7207bbb..8708dcf9fbf 100644 --- a/crates/egui_extras/src/syntax_highlighting.rs +++ b/crates/egui_extras/src/syntax_highlighting.rs @@ -248,7 +248,7 @@ impl CodeTheme { /// Show UI for changing the color theme. pub fn ui(&mut self, ui: &mut egui::Ui) { ui.horizontal_top(|ui| { - let selected_id = egui::Id::null(); + let selected_id = egui::Id::NULL; let mut selected_tt: TokenType = ui.data_mut(|d| *d.get_persisted_mut_or(selected_id, TokenType::Comment)); diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index 7e8507a622b..b8c12e5410b 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -840,7 +840,7 @@ impl Plot { if let Some((name, _)) = linked_axes.as_ref() { ui.memory_mut(|memory| { let link_groups: &mut BoundsLinkGroups = - memory.data.get_temp_mut_or_default(Id::null()); + memory.data.get_temp_mut_or_default(Id::NULL); link_groups.0.remove(name); }); }; @@ -925,7 +925,7 @@ impl Plot { // Find the cursors from other plots we need to draw let draw_cursors: Vec = if let Some((id, _)) = linked_cursors.as_ref() { ui.memory_mut(|memory| { - let frames: &mut CursorLinkGroups = memory.data.get_temp_mut_or_default(Id::null()); + let frames: &mut CursorLinkGroups = memory.data.get_temp_mut_or_default(Id::NULL); let cursors = frames.0.entry(*id).or_default(); // Look for our previous frame @@ -954,7 +954,7 @@ impl Plot { if let Some((id, axes)) = linked_axes.as_ref() { ui.memory_mut(|memory| { let link_groups: &mut BoundsLinkGroups = - memory.data.get_temp_mut_or_default(Id::null()); + memory.data.get_temp_mut_or_default(Id::NULL); if let Some(linked_bounds) = link_groups.0.get(id) { if axes.x { bounds.set_x(&linked_bounds.bounds); @@ -1197,7 +1197,7 @@ impl Plot { if let Some((id, _)) = linked_cursors.as_ref() { // Push the frame we just drew to the list of frames ui.memory_mut(|memory| { - let frames: &mut CursorLinkGroups = memory.data.get_temp_mut_or_default(Id::null()); + let frames: &mut CursorLinkGroups = memory.data.get_temp_mut_or_default(Id::NULL); let cursors = frames.0.entry(*id).or_default(); cursors.push(PlotFrameCursors { id: plot_id, @@ -1210,7 +1210,7 @@ impl Plot { // Save the linked bounds. ui.memory_mut(|memory| { let link_groups: &mut BoundsLinkGroups = - memory.data.get_temp_mut_or_default(Id::null()); + memory.data.get_temp_mut_or_default(Id::NULL); link_groups.0.insert( *id, LinkedBounds { From 4a800d8a1c310b320dccaacfac84e0292b956f1e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 11 Nov 2023 21:28:39 +0100 Subject: [PATCH 2/2] emath: use associated constant `ONE` for `trait One` --- crates/emath/src/lib.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/emath/src/lib.rs b/crates/emath/src/lib.rs index 8cfe16de07d..75f6ea9b34f 100644 --- a/crates/emath/src/lib.rs +++ b/crates/emath/src/lib.rs @@ -53,21 +53,15 @@ pub use { /// Helper trait to implement [`lerp`] and [`remap`]. pub trait One { - fn one() -> Self; + const ONE: Self; } impl One for f32 { - #[inline(always)] - fn one() -> Self { - 1.0 - } + const ONE: Self = 1.0; } impl One for f64 { - #[inline(always)] - fn one() -> Self { - 1.0 - } + const ONE: Self = 1.0; } /// Helper trait to implement [`lerp`] and [`remap`]. @@ -105,7 +99,7 @@ where R: Copy + Add, { let range = range.into(); - (T::one() - t) * *range.start() + t * *range.end() + (T::ONE - t) * *range.start() + t * *range.end() } /// Where in the range is this value? Returns 0-1 if within the range. @@ -172,7 +166,7 @@ where crate::emath_assert!(from.start() != from.end()); let t = (x - *from.start()) / (*from.end() - *from.start()); // Ensure no numerical inaccuracies sneak in: - if T::one() <= t { + if T::ONE <= t { *to.end() } else { lerp(to, t)