Trait option_operations::ord::OptionOrd
source · pub trait OptionOrd<Rhs, InnerRhs = Rhs> {
// Required method
fn opt_cmp(&self, rhs: Rhs) -> Option<Ordering>;
// Provided methods
fn opt_lt(&self, rhs: Rhs) -> Option<bool> { ... }
fn opt_le(&self, rhs: Rhs) -> Option<bool> { ... }
fn opt_gt(&self, rhs: Rhs) -> Option<bool> { ... }
fn opt_ge(&self, rhs: Rhs) -> Option<bool> { ... }
}
Expand description
Trait for values and Option
s that can be compared for a sort-order.
This implementation is mainly intended at working around the PartialOrd
implementation for Option
, which compares Option
s
depending on the order of declaration in the enum
.
§PartialOrd
implementation for Option
let some_0 = Some(0);
let none: Option<u64> = None;
assert_eq!(none.partial_cmp(&some_0), Some(Ordering::Less));
assert_eq!(some_0.partial_cmp(&none), Some(Ordering::Greater));
§Alternative behavior
In some cases, we might consider that None
reflects a value which
is not defined and thus can not be compared with Some(_)
.
assert_eq!(none.opt_cmp(&some_0), None);
assert_eq!(some_0.opt_cmp(&none), None);
§Implementations
Implementing this type leads to the following auto-implementations:
OptionOrd<Option<InnerRhs>> for T
.OptionOrd<Rhs> for Option<T>
.OptionOrd<Option<InnerRhs>> for Option<T>
.- … and some variants with references.
This trait is auto-implemented for OptionOperations
types
implementing PartialOrd<Rhs>
.
Required Methods§
Provided Methods§
sourcefn opt_lt(&self, rhs: Rhs) -> Option<bool>
fn opt_lt(&self, rhs: Rhs) -> Option<bool>
Tests whether self
is less than rhs
.
Returns None
if they can’t be compared, e.g. if
at most one argument is None
.
sourcefn opt_le(&self, rhs: Rhs) -> Option<bool>
fn opt_le(&self, rhs: Rhs) -> Option<bool>
Tests whether self
is less or equal to rhs
.
Returns None
if they can’t be compared, e.g. if
at most one argument is None
.