From 41b54360ff917a261d3f6f752e3c2c51edada773 Mon Sep 17 00:00:00 2001 From: Max Verevkin Date: Sun, 3 Apr 2022 22:52:12 +0300 Subject: small refactoring --- src/main.rs | 21 ++++----------------- src/ord_adaptor.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 src/ord_adaptor.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 85d61d8..1fe335e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -#![allow(clippy::derive_ord_xor_partial_ord)] - #[macro_use] extern crate log; @@ -31,6 +29,7 @@ mod color; mod config; mod i3bar_protocol; mod lines_buffer; +mod ord_adaptor; mod pointer_btn; mod river_protocols; mod status_cmd; @@ -40,6 +39,7 @@ mod text; use button_manager::ButtonManager; use config::Config; use i3bar_protocol::{Block, MinWidth}; +use ord_adaptor::DefaultLess; use pointer_btn::PointerBtn; use river_protocols::zriver_command_callback_v1; use river_protocols::zriver_control_v1; @@ -632,10 +632,10 @@ fn render_blocks( let mut heap = BinaryHeap::new(); for (name, delta) in deltas { if delta > 0.0 { - heap.push((OrdFloat(delta), name)); + heap.push((DefaultLess(delta), name)); } } - while let Some((OrdFloat(delta), to_switch)) = heap.pop() { + while let Some((DefaultLess(delta), to_switch)) = heap.pop() { for (name, full, short) in &mut blocks_computed { if *name == Some(to_switch) { if let Some(short) = short { @@ -684,16 +684,3 @@ fn render_blocks( } } } - -#[derive(PartialEq, PartialOrd)] -struct OrdFloat(f64); - -impl Eq for OrdFloat {} - -impl Ord for OrdFloat { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.0 - .partial_cmp(&other.0) - .unwrap_or(std::cmp::Ordering::Less) - } -} diff --git a/src/ord_adaptor.rs b/src/ord_adaptor.rs new file mode 100644 index 0000000..c7c6763 --- /dev/null +++ b/src/ord_adaptor.rs @@ -0,0 +1,18 @@ +//! Newtypes to use floating numbers in sorting algs + +use std::cmp::Ordering; + +/// Defaults to `Ordering::Less` if the wrapped value can't be compared +#[derive(PartialEq, PartialOrd)] +#[repr(transparent)] +pub struct DefaultLess(pub T); + +// Not ideal, but `Ord` requires `Eq`, so... +impl Eq for DefaultLess {} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl Ord for DefaultLess { + fn cmp(&self, other: &Self) -> Ordering { + self.partial_cmp(other).unwrap_or(Ordering::Less) + } +} -- cgit v1.3.1