From 7d6aac38826e8dde1759408b1d273f2e5e1d9833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Volf?= Date: Tue, 10 Sep 2024 16:12:14 +0200 Subject: [PATCH] refactor: implement `Deref` for `ReverseOrdOption` --- src/utils/reverse_ord_option.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/reverse_ord_option.rs b/src/utils/reverse_ord_option.rs index fa5f9f3..b40f29c 100644 --- a/src/utils/reverse_ord_option.rs +++ b/src/utils/reverse_ord_option.rs @@ -1,10 +1,18 @@ 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<'a, T> Deref for ReverseOrdOption<'a, T> { + type Target = Option; + + fn deref(&self) -> &Self::Target { + self.0 + } +} + impl<'a, T: Ord> Eq for ReverseOrdOption<'a, T> {} impl<'a, T: Ord> PartialOrd for ReverseOrdOption<'a, T> { @@ -15,7 +23,7 @@ impl<'a, T: Ord> PartialOrd for ReverseOrdOption<'a, T> { impl<'a, T: Ord> Ord for ReverseOrdOption<'a, T> { fn cmp(&self, other: &Self) -> Ordering { - match (self.0.as_ref(), other.0.as_ref()) { + match (self.as_ref(), other.as_ref()) { (None, None) => Ordering::Equal, (None, Some(_)) => Ordering::Greater, (Some(_), None) => Ordering::Less,