Skip to content

Commit

Permalink
Replace Id::null() with Id::NULL (#3544)
Browse files Browse the repository at this point in the history
Shorter and more idiomatic
  • Loading branch information
emilk committed Nov 11, 2023
1 parent b27aa27 commit 6ba356d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub(crate) struct TooltipState {

impl TooltipState {
pub fn load(ctx: &Context) -> Option<Self> {
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<Vec2> {
Expand Down
3 changes: 3 additions & 0 deletions crates/egui/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/widgets/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,5 @@ fn color_cache_set(ctx: &Context, rgba: impl Into<Rgba>, hsva: Hsva) {

// To ensure we keep hue slider when `srgba` is gray we store the full [`Hsva`] in a cache:
fn use_color_cache<R>(ctx: &Context, f: impl FnOnce(&mut FixedCache<Rgba, Hsva>) -> 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)))
}
2 changes: 1 addition & 1 deletion crates/egui_extras/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
10 changes: 5 additions & 5 deletions crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,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);
});
};
Expand Down Expand Up @@ -892,7 +892,7 @@ impl Plot {
// Find the cursors from other plots we need to draw
let draw_cursors: Vec<Cursor> = 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
Expand Down Expand Up @@ -921,7 +921,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);
Expand Down Expand Up @@ -1164,7 +1164,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,
Expand All @@ -1177,7 +1177,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 {
Expand Down
16 changes: 5 additions & 11 deletions crates/emath/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,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`].
Expand Down Expand Up @@ -107,7 +101,7 @@ where
R: Copy + Add<R, Output = R>,
{
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.
Expand Down Expand Up @@ -174,7 +168,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)
Expand Down

0 comments on commit 6ba356d

Please sign in to comment.