feat: create a utility struct for reversing Option ordering

This commit is contained in:
Matouš Volf 2024-09-09 18:48:10 +02:00
parent cbe9aafe40
commit ca8816b5ef
3 changed files with 33 additions and 0 deletions

View File

@ -5,6 +5,7 @@ mod route;
mod schema;
mod server;
mod query;
mod utils;
use components::app::App;
use dioxus::prelude::*;

1
src/utils/mod.rs Normal file
View File

@ -0,0 +1 @@
pub(crate) mod reverse_ord_option;

View File

@ -0,0 +1,31 @@
use std::cmp::Ordering;
/* The default ordering of `Option`s is `None` being less than `Some`. The purpose of this struct is
to reverse that. */
#[derive(PartialEq)]
pub(crate) struct ReverseOrdOption<'a, T>(&'a Option<T>);
impl<'a, T: Ord> Eq for ReverseOrdOption<'a, T> {}
impl<'a, T: Ord> PartialOrd<Self> for ReverseOrdOption<'a, T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<'a, T: Ord> Ord for ReverseOrdOption<'a, T> {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.as_ref(), other.0.as_ref()) {
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(Some(self_time), Some(other_time)) => self_time.cmp(other_time)
}
}
}
impl<'a, T> From<&'a Option<T>> for ReverseOrdOption<'a, T> {
fn from(value: &'a Option<T>) -> Self {
ReverseOrdOption(value)
}
}