todo-baggins/src/utils/reverse_ord_option.rs
Matouš Volf 6b2bfb5ebe
All checks were successful
actionlint check / actionlint check (pull_request) Successful in 6s
checkov check / checkov check (pull_request) Successful in 2m35s
conventional commit messages check / conventional commit messages check (pull_request) Successful in 4s
conventional pull request title check / conventional pull request title check (pull_request) Successful in 3s
dotenv-linter check / dotenv-linter check (pull_request) Successful in 5s
GitLeaks check / GitLeaks check (pull_request) Successful in 7s
hadolint check / hadolint check (pull_request) Successful in 19s
htmlhint check / htmlhint check (pull_request) Successful in 10s
markdownlint check / markdownlint check (pull_request) Successful in 9s
Prettier check / Prettier check (pull_request) Successful in 9s
Rust check / Rust check (pull_request) Successful in 15m57s
ShellCheck check / ShellCheck check (pull_request) Successful in 13s
Stylelint check / Stylelint check (pull_request) Successful in 11s
yamllint check / yamllint check (pull_request) Successful in 13s
style: remove needless lifetimes
2024-12-29 21:07:07 +01:00

40 lines
1.1 KiB
Rust

use std::cmp::Ordering;
use std::ops::Deref;
/* 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<T> Deref for ReverseOrdOption<'_, T> {
type Target = Option<T>;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl<T: Ord> Eq for ReverseOrdOption<'_, T> {}
impl<T: Ord> PartialOrd<Self> for ReverseOrdOption<'_, T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: Ord> Ord for ReverseOrdOption<'_, T> {
fn cmp(&self, other: &Self) -> Ordering {
match (self.as_ref(), other.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)
}
}