pub struct OptionMinMax<T: Ord + Copy>(/* private fields */);Expand description
Simple helper type that makes it easier to get mins and maxes when some values may be None. Typical usage looks something like:
let mut m = aoclib_rs::option_min_max::OptionMinMax::NONE;
for i in [1, 5, 8, 3, 2, 12, 7] {
m = m.max(i);
}
assert_eq!(m.unwrap(), 12);Implementations§
Source§impl<T: Ord + Copy> OptionMinMax<T>
impl<T: Ord + Copy> OptionMinMax<T>
pub const NONE: Self
pub const fn new(val: Option<T>) -> Self
pub fn new_concrete(val: T) -> Self
Sourcepub fn min(&self, other: T) -> Self
pub fn min(&self, other: T) -> Self
Returns a new OptionMinMax containing the minimum value, or other if self contains
None.
use aoclib_rs::option_min_max::OptionMinMax;
assert_eq!(OptionMinMax::NONE.min(3).unwrap(), 3);
assert_eq!(OptionMinMax::new_concrete(2).min(3).unwrap(), 2);Sourcepub fn max(&self, other: T) -> Self
pub fn max(&self, other: T) -> Self
Returns a new OptionMinMax containing the maximum value, or other if self contains
None.
use aoclib_rs::option_min_max::OptionMinMax;
assert_eq!(OptionMinMax::NONE.max(3).unwrap(), 3);
assert_eq!(OptionMinMax::new_concrete(2).max(3).unwrap(), 3);Sourcepub fn min_self(&self, other: Self) -> Self
pub fn min_self(&self, other: Self) -> Self
Returns a new OptionMinMax containing the non-None value, if one exists. If both values
are non-None, returns the minimum.
use aoclib_rs::option_min_max::OptionMinMax;
assert_eq!(OptionMinMax::<i32>::NONE.min_self(OptionMinMax::NONE).get(), None);
assert_eq!(OptionMinMax::NONE.min_self(OptionMinMax::new_concrete(3)).unwrap(), 3);
assert_eq!(OptionMinMax::new_concrete(4).min_self(OptionMinMax::NONE).unwrap(), 4);
assert_eq!(OptionMinMax::new_concrete(4).min_self(OptionMinMax::new_concrete(2)).unwrap(), 2);Sourcepub fn max_self(&self, other: Self) -> Self
pub fn max_self(&self, other: Self) -> Self
Returns a new OptionMinMax containing the non-None value, if one exists. If both values
are non-None, returns the maximum.
use aoclib_rs::option_min_max::OptionMinMax;
assert_eq!(OptionMinMax::<i32>::NONE.max_self(OptionMinMax::NONE).get(), None);
assert_eq!(OptionMinMax::NONE.max_self(OptionMinMax::new_concrete(3)).unwrap(), 3);
assert_eq!(OptionMinMax::new_concrete(4).max_self(OptionMinMax::NONE).unwrap(), 4);
assert_eq!(OptionMinMax::new_concrete(4).max_self(OptionMinMax::new_concrete(2)).unwrap(), 4);Sourcepub fn min_option(&self, other: Option<T>) -> Self
pub fn min_option(&self, other: Option<T>) -> Self
Returns a new OptionMinMax containing the non-None value, if one exists. If both values
are non-None, returns the minimum.
use aoclib_rs::option_min_max::OptionMinMax;
assert_eq!(OptionMinMax::<i32>::NONE.min_option(None).get(), None);
assert_eq!(OptionMinMax::NONE.min_option(Some(3)).unwrap(), 3);
assert_eq!(OptionMinMax::new_concrete(4).min_option(None).unwrap(), 4);
assert_eq!(OptionMinMax::new_concrete(4).min_option(Some(2)).unwrap(), 2);Sourcepub fn max_option(&self, other: Option<T>) -> Self
pub fn max_option(&self, other: Option<T>) -> Self
Returns a new OptionMinMax containing the non-None value, if one exists. If both values
are non-None, returns the minimum.
use aoclib_rs::option_min_max::OptionMinMax;
assert_eq!(OptionMinMax::<i32>::NONE.max_option(None).get(), None);
assert_eq!(OptionMinMax::NONE.max_option(Some(3)).unwrap(), 3);
assert_eq!(OptionMinMax::new_concrete(4).max_option(None).unwrap(), 4);
assert_eq!(OptionMinMax::new_concrete(4).max_option(Some(2)).unwrap(), 4);pub fn get(&self) -> Option<T>
Trait Implementations§
Source§impl<T: Clone + Ord + Copy> Clone for OptionMinMax<T>
impl<T: Clone + Ord + Copy> Clone for OptionMinMax<T>
Source§fn clone(&self) -> OptionMinMax<T>
fn clone(&self) -> OptionMinMax<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more