From 714c99ae8f84eba51ff4e8361128717c248ea7f0 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 2 Aug 2024 23:14:50 +0200 Subject: [PATCH 1/3] add lints and fix the issues --- Cargo.toml | 10 +++++++++ src/anchor.rs | 28 ++++++++++++------------ src/lib.rs | 59 ++++++++++++++++++++++++++++++++++----------------- src/toast.rs | 42 ++++++++++++++++++++++++------------ 4 files changed, 91 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index db234b5..cef1afe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,13 @@ eframe = { version = "0.28.0", default-features = false, features = [ "glow", ] } egui-phosphor = { git = "https://github.com/ItsEthra/egui-phosphor", branch = "main" } + +[lints.rust] +unsafe_code = "forbid" + +[lints.clippy] +nursery = { level = "deny", priority = 0 } +pedantic = { level = "deny", priority = 1 } +enum_glob_use = { level = "deny", priority = 2 } +module_name_repetitions = { level = "allow", priority = 3 } +cast_precision_loss = { level = "allow", priority = 4 } diff --git a/src/anchor.rs b/src/anchor.rs index 8fcecfc..1caa6c9 100644 --- a/src/anchor.rs +++ b/src/anchor.rs @@ -15,41 +15,41 @@ pub enum Anchor { impl Anchor { #[inline] - pub(crate) fn anim_side(&self) -> f32 { + pub(crate) const fn anim_side(self) -> f32 { match self { - Anchor::TopRight | Anchor::BottomRight => 1., - Anchor::TopLeft | Anchor::BottomLeft => -1., + Self::TopRight | Self::BottomRight => 1., + Self::TopLeft | Self::BottomLeft => -1., } } } impl Anchor { - pub(crate) fn screen_corner(&self, sc: Pos2, margin: Vec2) -> Pos2 { + pub(crate) fn screen_corner(self, sc: Pos2, margin: Vec2) -> Pos2 { let mut out = match self { - Anchor::TopRight => pos2(sc.x, 0.), - Anchor::TopLeft => pos2(0., 0.), - Anchor::BottomRight => sc, - Anchor::BottomLeft => pos2(0., sc.y), + Self::TopRight => pos2(sc.x, 0.), + Self::TopLeft => pos2(0., 0.), + Self::BottomRight => sc, + Self::BottomLeft => pos2(0., sc.y), }; self.apply_margin(&mut out, margin); out } - pub(crate) fn apply_margin(&self, pos: &mut Pos2, margin: Vec2) { + pub(crate) fn apply_margin(self, pos: &mut Pos2, margin: Vec2) { match self { - Anchor::TopRight => { + Self::TopRight => { pos.x -= margin.x; pos.y += margin.y; } - Anchor::TopLeft => { + Self::TopLeft => { pos.x += margin.x; - pos.y += margin.y + pos.y += margin.y; } - Anchor::BottomRight => { + Self::BottomRight => { pos.x -= margin.x; pos.y -= margin.y; } - Anchor::BottomLeft => { + Self::BottomLeft => { pos.x += margin.x; pos.y -= margin.y; } diff --git a/src/lib.rs b/src/lib.rs index 7537ec0..f95fd17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,7 @@ pub struct Toasts { impl Toasts { /// Creates new [`Toasts`] instance. + #[must_use] pub const fn new() -> Self { Self { anchor: Anchor::TopRight, @@ -65,15 +66,18 @@ impl Toasts { /// Adds new toast to the collection. /// By default adds toast at the end of the list, can be changed with `self.reverse`. + /// # Panics + /// + /// Will panic if after adding a toast the list is empty. + #[must_use] pub fn add(&mut self, toast: Toast) -> &mut Toast { if self.reverse { self.toasts.insert(0, toast); return self.toasts.get_mut(0).unwrap(); - } else { - self.toasts.push(toast); - let l = self.toasts.len() - 1; - return self.toasts.get_mut(l).unwrap(); } + self.toasts.push(toast); + let l = self.toasts.len() - 1; + return self.toasts.get_mut(l).unwrap(); } /// Dismisses the oldest toast @@ -92,37 +96,43 @@ impl Toasts { /// Dismisses all toasts pub fn dismiss_all_toasts(&mut self) { - for toast in self.toasts.iter_mut() { + for toast in &mut self.toasts { toast.dismiss(); } } /// Shortcut for adding a toast with info `success`. + #[must_use] pub fn success(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::success(caption)) } /// Shortcut for adding a toast with info `level`. + #[must_use] pub fn info(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::info(caption)) } /// Shortcut for adding a toast with warning `level`. + #[must_use] pub fn warning(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::warning(caption)) } /// Shortcut for adding a toast with error `level`. + #[must_use] pub fn error(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::error(caption)) } /// Shortcut for adding a toast with no level. + #[must_use] pub fn basic(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::basic(caption)) } /// Shortcut for adding a toast with custom `level`. + #[must_use] pub fn custom( &mut self, caption: impl Into, @@ -136,36 +146,42 @@ impl Toasts { } /// Should toasts be added in reverse order? + #[must_use] pub const fn reverse(mut self, reverse: bool) -> Self { self.reverse = reverse; self } /// Where toasts should appear. + #[must_use] pub const fn with_anchor(mut self, anchor: Anchor) -> Self { self.anchor = anchor; self } /// Sets spacing between adjacent toasts. + #[must_use] pub const fn with_spacing(mut self, spacing: f32) -> Self { self.spacing = spacing; self } /// Margin or distance from screen to toasts' bounding boxes + #[must_use] pub const fn with_margin(mut self, margin: Vec2) -> Self { self.margin = margin; self } /// Padding or distance from toasts' bounding boxes to inner contents. + #[must_use] pub const fn with_padding(mut self, padding: Vec2) -> Self { self.padding = padding; self } /// Changes the default font used for all toasts. + #[must_use] pub fn with_default_font(mut self, font: FontId) -> Self { self.font = Some(font); self @@ -173,6 +189,7 @@ impl Toasts { } impl Toasts { + #[allow(clippy::cognitive_complexity, clippy::too_many_lines)] /// Displays toast queue pub fn show(&mut self, ctx: &Context) { let Self { @@ -195,13 +212,13 @@ impl Toasts { toasts.retain(|t| !t.state.disappeared()); // Start disappearing expired toasts - toasts.iter_mut().for_each(|t| { + for t in toasts.iter_mut() { if let Some((_initial_d, current_d)) = t.duration { if current_d <= 0. { - t.state = ToastState::Disapper + t.state = ToastState::Disappear; } } - }); + } // `held` used to prevent sticky removal if ctx.input(|i| i.pointer.primary_released()) { @@ -265,11 +282,10 @@ impl Toasts { ToastLevel::None => None, }; - let (action_width, action_height) = if let Some(icon_galley) = icon_galley.as_ref() { - (icon_galley.rect.width(), icon_galley.rect.height()) - } else { - (0., 0.) - }; + let (action_width, action_height) = + icon_galley.as_ref().map_or((0., 0.), |icon_galley| { + (icon_galley.rect.width(), icon_galley.rect.height()) + }); // Create closing cross let cross_galley = if toast.closable { @@ -287,11 +303,10 @@ impl Toasts { None }; - let (cross_width, cross_height) = if let Some(cross_galley) = cross_galley.as_ref() { - (cross_galley.rect.width(), cross_galley.rect.height()) - } else { - (0., 0.) - }; + let (cross_width, cross_height) = + cross_galley.as_ref().map_or((0., 0.), |cross_galley| { + (cross_galley.rect.width(), cross_galley.rect.height()) + }); let icon_x_padding = (0., padding.x); let cross_x_padding = (padding.x, 0.); @@ -307,8 +322,12 @@ impl Toasts { cross_width + cross_x_padding.0 + cross_x_padding.1 }; - toast.width = icon_width_padded + caption_width + cross_width_padded + (padding.x * 2.); - toast.height = action_height.max(caption_height).max(cross_height) + padding.y * 2.; + toast.width = padding + .x + .mul_add(2., icon_width_padded + caption_width + cross_width_padded); + toast.height = padding + .y + .mul_add(2., action_height.max(caption_height).max(cross_height)); let anim_offset = toast.width * (1. - ease_in_cubic(toast.value)); pos.x += anim_offset * anchor.anim_side(); diff --git a/src/toast.rs b/src/toast.rs index 5935cb4..143a781 100644 --- a/src/toast.rs +++ b/src/toast.rs @@ -16,24 +16,40 @@ pub enum ToastLevel { } #[derive(Debug)] -pub(crate) enum ToastState { +/// State of the toast +pub enum ToastState { + /// Toast is appearing Appear, - Disapper, + /// Toast is disappearing + Disappear, + /// Toast has disappeared Disappeared, + /// Toast is idling Idle, } impl ToastState { - pub fn appearing(&self) -> bool { + /// Returns `true` if the toast is appearing + #[must_use] + pub const fn appearing(&self) -> bool { matches!(self, Self::Appear) } - pub fn disappearing(&self) -> bool { - matches!(self, Self::Disapper) + + /// Returns `true` if the toast is disappearing + #[must_use] + pub const fn disappearing(&self) -> bool { + matches!(self, Self::Disappear) } - pub fn disappeared(&self) -> bool { + + /// Returns `true` if the toast has disappeared + #[must_use] + pub const fn disappeared(&self) -> bool { matches!(self, Self::Disappeared) } - pub fn idling(&self) -> bool { + + /// Returns `true` if the toast is idling + #[must_use] + pub const fn idling(&self) -> bool { matches!(self, Self::Idle) } } @@ -84,12 +100,10 @@ impl Toast { caption: caption.into(), height: TOAST_HEIGHT, width: TOAST_WIDTH, - duration: if let Some(dur) = options.duration { + duration: options.duration.map(|dur| { let max_dur = duration_to_seconds_f32(dur); - Some((max_dur, max_dur)) - } else { - None - }, + (max_dur, max_dur) + }), closable: options.closable, show_progress_bar: options.show_progress_bar, level: options.level, @@ -161,7 +175,7 @@ impl Toast { ) } - /// Set the options with a ToastOptions + /// Set the options with a `ToastOptions` pub fn set_options(&mut self, options: ToastOptions) -> &mut Self { self.set_closable(options.closable); self.set_duration(options.duration); @@ -219,7 +233,7 @@ impl Toast { /// Dismiss this toast pub fn dismiss(&mut self) { - self.state = ToastState::Disapper; + self.state = ToastState::Disappear; } pub(crate) fn calc_anchored_rect(&self, pos: Pos2, anchor: Anchor) -> Rect { From bf09e83309fe193cd5c5491ca50ddaf297e0c135 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 3 Aug 2024 11:55:55 +0200 Subject: [PATCH 2/3] fix review findings --- Cargo.toml | 5 +---- src/anchor.rs | 6 +++--- src/lib.rs | 16 +--------------- src/toast.rs | 6 +----- 4 files changed, 6 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cef1afe..586b77d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,5 @@ egui-phosphor = { git = "https://github.com/ItsEthra/egui-phosphor", branch = "m unsafe_code = "forbid" [lints.clippy] -nursery = { level = "deny", priority = 0 } -pedantic = { level = "deny", priority = 1 } +all = { level = "deny", priority = 0 } enum_glob_use = { level = "deny", priority = 2 } -module_name_repetitions = { level = "allow", priority = 3 } -cast_precision_loss = { level = "allow", priority = 4 } diff --git a/src/anchor.rs b/src/anchor.rs index 1caa6c9..c495e6f 100644 --- a/src/anchor.rs +++ b/src/anchor.rs @@ -15,7 +15,7 @@ pub enum Anchor { impl Anchor { #[inline] - pub(crate) const fn anim_side(self) -> f32 { + pub(crate) const fn anim_side(&self) -> f32 { match self { Self::TopRight | Self::BottomRight => 1., Self::TopLeft | Self::BottomLeft => -1., @@ -24,7 +24,7 @@ impl Anchor { } impl Anchor { - pub(crate) fn screen_corner(self, sc: Pos2, margin: Vec2) -> Pos2 { + pub(crate) fn screen_corner(&self, sc: Pos2, margin: Vec2) -> Pos2 { let mut out = match self { Self::TopRight => pos2(sc.x, 0.), Self::TopLeft => pos2(0., 0.), @@ -35,7 +35,7 @@ impl Anchor { out } - pub(crate) fn apply_margin(self, pos: &mut Pos2, margin: Vec2) { + pub(crate) fn apply_margin(&self, pos: &mut Pos2, margin: Vec2) { match self { Self::TopRight => { pos.x -= margin.x; diff --git a/src/lib.rs b/src/lib.rs index f95fd17..7db1cdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,6 @@ impl Toasts { /// # Panics /// /// Will panic if after adding a toast the list is empty. - #[must_use] pub fn add(&mut self, toast: Toast) -> &mut Toast { if self.reverse { self.toasts.insert(0, toast); @@ -77,7 +76,7 @@ impl Toasts { } self.toasts.push(toast); let l = self.toasts.len() - 1; - return self.toasts.get_mut(l).unwrap(); + self.toasts.get_mut(l).unwrap() } /// Dismisses the oldest toast @@ -102,37 +101,31 @@ impl Toasts { } /// Shortcut for adding a toast with info `success`. - #[must_use] pub fn success(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::success(caption)) } /// Shortcut for adding a toast with info `level`. - #[must_use] pub fn info(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::info(caption)) } /// Shortcut for adding a toast with warning `level`. - #[must_use] pub fn warning(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::warning(caption)) } /// Shortcut for adding a toast with error `level`. - #[must_use] pub fn error(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::error(caption)) } /// Shortcut for adding a toast with no level. - #[must_use] pub fn basic(&mut self, caption: impl Into) -> &mut Toast { self.add(Toast::basic(caption)) } /// Shortcut for adding a toast with custom `level`. - #[must_use] pub fn custom( &mut self, caption: impl Into, @@ -146,42 +139,36 @@ impl Toasts { } /// Should toasts be added in reverse order? - #[must_use] pub const fn reverse(mut self, reverse: bool) -> Self { self.reverse = reverse; self } /// Where toasts should appear. - #[must_use] pub const fn with_anchor(mut self, anchor: Anchor) -> Self { self.anchor = anchor; self } /// Sets spacing between adjacent toasts. - #[must_use] pub const fn with_spacing(mut self, spacing: f32) -> Self { self.spacing = spacing; self } /// Margin or distance from screen to toasts' bounding boxes - #[must_use] pub const fn with_margin(mut self, margin: Vec2) -> Self { self.margin = margin; self } /// Padding or distance from toasts' bounding boxes to inner contents. - #[must_use] pub const fn with_padding(mut self, padding: Vec2) -> Self { self.padding = padding; self } /// Changes the default font used for all toasts. - #[must_use] pub fn with_default_font(mut self, font: FontId) -> Self { self.font = Some(font); self @@ -189,7 +176,6 @@ impl Toasts { } impl Toasts { - #[allow(clippy::cognitive_complexity, clippy::too_many_lines)] /// Displays toast queue pub fn show(&mut self, ctx: &Context) { let Self { diff --git a/src/toast.rs b/src/toast.rs index 143a781..c0c555a 100644 --- a/src/toast.rs +++ b/src/toast.rs @@ -30,25 +30,21 @@ pub enum ToastState { impl ToastState { /// Returns `true` if the toast is appearing - #[must_use] pub const fn appearing(&self) -> bool { matches!(self, Self::Appear) } /// Returns `true` if the toast is disappearing - #[must_use] pub const fn disappearing(&self) -> bool { matches!(self, Self::Disappear) } /// Returns `true` if the toast has disappeared - #[must_use] pub const fn disappeared(&self) -> bool { matches!(self, Self::Disappeared) } /// Returns `true` if the toast is idling - #[must_use] pub const fn idling(&self) -> bool { matches!(self, Self::Idle) } @@ -175,7 +171,7 @@ impl Toast { ) } - /// Set the options with a `ToastOptions` + /// Set the options with a [`ToastOptions`] pub fn set_options(&mut self, options: ToastOptions) -> &mut Self { self.set_closable(options.closable); self.set_duration(options.duration); From ca88bb65c46a71eeb359e25cae8943e020b71adb Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 3 Aug 2024 14:18:52 +0200 Subject: [PATCH 3/3] Remove panic --- src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7db1cdb..2dc812f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,9 +66,6 @@ impl Toasts { /// Adds new toast to the collection. /// By default adds toast at the end of the list, can be changed with `self.reverse`. - /// # Panics - /// - /// Will panic if after adding a toast the list is empty. pub fn add(&mut self, toast: Toast) -> &mut Toast { if self.reverse { self.toasts.insert(0, toast);