OptionMinMax

Struct OptionMinMax 

Source
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>

Source

pub const NONE: Self

Source

pub const fn new(val: Option<T>) -> Self

Source

pub fn new_concrete(val: T) -> Self

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub fn get(&self) -> Option<T>

Source

pub fn unwrap(&self) -> T

can panic

Trait Implementations§

Source§

impl<T: Clone + Ord + Copy> Clone for OptionMinMax<T>

Source§

fn clone(&self) -> OptionMinMax<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Ord + Copy> Debug for OptionMinMax<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Ord + Copy> Default for OptionMinMax<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Copy + Ord + Copy> Copy for OptionMinMax<T>

Auto Trait Implementations§

§

impl<T> Freeze for OptionMinMax<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for OptionMinMax<T>
where T: RefUnwindSafe,

§

impl<T> Send for OptionMinMax<T>
where T: Send,

§

impl<T> Sync for OptionMinMax<T>
where T: Sync,

§

impl<T> Unpin for OptionMinMax<T>
where T: Unpin,

§

impl<T> UnwindSafe for OptionMinMax<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.