diff options
| -rw-r--r-- | src/main.rs | 21 | ||||
| -rw-r--r-- | src/ord_adaptor.rs | 18 |
2 files changed, 22 insertions, 17 deletions
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<T: PartialOrd>(pub T); + +// Not ideal, but `Ord` requires `Eq`, so... +impl<T: PartialOrd> Eq for DefaultLess<T> {} + +#[allow(clippy::derive_ord_xor_partial_ord)] +impl<T: PartialOrd> Ord for DefaultLess<T> { + fn cmp(&self, other: &Self) -> Ordering { + self.partial_cmp(other).unwrap_or(Ordering::Less) + } +} |
