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); impl Deref for ReverseOrdOption<'_, T> { type Target = Option; fn deref(&self) -> &Self::Target { self.0 } } impl Eq for ReverseOrdOption<'_, T> {} impl PartialOrd for ReverseOrdOption<'_, T> { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl 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> for ReverseOrdOption<'a, T> { fn from(value: &'a Option) -> Self { ReverseOrdOption(value) } }