Skip to content

Commit

Permalink
refactor: simplify partial ordering fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed May 15, 2022
1 parent c1d3b4e commit 9c5efc5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/canvas/components/text_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ mod test {
use crate::app::ScrollDirection::{self, Down, Up};

#[track_caller]

fn test_get(
bar: usize, rows: usize, direction: ScrollDirection, selected: usize, force: bool,
expected_posn: usize, expected_bar: usize,
Expand Down
9 changes: 7 additions & 2 deletions src/utils/gen_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub fn get_decimal_prefix(quantity: u64, unit: &str) -> (f64, String) {
}
}

#[inline]
pub fn sort_partial_fn<T: std::cmp::PartialOrd>(is_reverse: bool) -> fn(T, T) -> Ordering {
if is_reverse {
partial_ordering_rev
Expand All @@ -101,15 +102,19 @@ pub fn sort_partial_fn<T: std::cmp::PartialOrd>(is_reverse: bool) -> fn(T, T) ->
}

/// Returns an [`Ordering`] between two [`PartialOrd`]s.
#[inline]
pub fn partial_ordering<T: std::cmp::PartialOrd>(a: T, b: T) -> Ordering {
// TODO: Switch to `total_cmp` on 1.62
a.partial_cmp(&b).unwrap_or(Ordering::Equal)
}

/// Returns a reversed [`Ordering`] between two [`PartialOrd`]s.
///
/// This is simply a wrapper function around [`partial_ordering`] that reverses
/// the result.
#[inline]
pub fn partial_ordering_rev<T: std::cmp::PartialOrd>(a: T, b: T) -> Ordering {
// TODO: Switch to `total_cmp` on 1.62
a.partial_cmp(&b).unwrap_or(Ordering::Equal).reverse()
partial_ordering(a, b).reverse()
}

#[cfg(test)]
Expand Down

0 comments on commit 9c5efc5

Please sign in to comment.