Enum MinMax

Source
pub enum MinMax<Item> {
    None,
    Single(Item),
    Both(Item, Item),
}
Expand description

The MinMax type uses the std::cmp::PartialOrd trait to contain both the smallest and largest iterated values.

Example:

use autofolder::*;

// Create an autofolder that collects the min+max usize.
let mut minmax = MinMax::<usize>::default();

// We can "reduce-in" individual items.
// (note: as this is the first value, we incorporate it
//  without calling the trait function)
minmax.reduce(3);

// Minmax fills up `min` first, so at this moment we can see that
// min is all we have:
println!("Partial minmax is {:?}", minmax.as_ref());

// `eval` does the same as `reduce`, and in this case sets
// min=2 and max=3:
minmax.eval(2);

// We can now peek at the running output as a single `Option`:
println!("Partial minmax is {:?}", minmax.as_ref());

// And still keep on folding by processing whole iterators:
minmax.extend((1..=5));

// And finally consume the autofolder to get the final output value:
let (min, max) = minmax.to_inner().unwrap();
println!("Final min is {}, max is {}", min, max);

Variants§

§

None

Empty; no item evaluated.

§

Single(Item)

Single item evaluated.

§

Both(Item, Item)

Two or more items evaluated - min and max values.

Implementations§

Source§

impl<Item> MinMax<Item>

Source

pub fn new(initial: Item) -> Self

Creates a new MinMax with the provided initial values.

Source

pub fn to_inner(self) -> Option<(Item, Item)>
where Item: Clone,

Deconstruct self and return the inner values that were found.

Source

pub fn as_ref(&self) -> Option<(&Item, &Item)>

Returns a reference to the inner values, if they exist.

Source

pub fn min_as_ref(&self) -> Option<&Item>

Returns a reference to the min inner values, if it exist.

Source

pub fn max_as_ref(&self) -> Option<&Item>

Returns a reference to the max inner values, if it exist.

Source

pub fn reduce(&mut self, item: Item)
where Item: PartialOrd,

Replaces a current value with the new one if the new one is greater/smaller.

When we have a single value, min is always filled up first, and then swapped with max if necessary.

Source

pub fn reduce_ref(&mut self, item: &Item)
where Item: PartialOrd + Clone,

Replaces a current value with the one behind the ref if it is greater/smaller.

When we have a single value, min is always filled up first, and then swapped with max if necessary.

This function requires the Clone trait, but uses it only if necessary.

Source

pub fn eval(&mut self, item: Item)
where Item: PartialOrd,

Alias for MinMax::reduce

Source

pub fn eval_ref(&mut self, item: &Item)
where Item: PartialOrd + Clone,

Trait Implementations§

Source§

impl<Item: Clone> Clone for MinMax<Item>

Source§

fn clone(&self) -> MinMax<Item>

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<Item: Debug> Debug for MinMax<Item>

Source§

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

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

impl<Item> Default for MinMax<Item>

Source§

fn default() -> MinMax<Item>

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

impl<'a, Item> Extend<&'a Item> for MinMax<Item>
where Item: PartialOrd + Clone,

Source§

fn extend<It: IntoIterator<Item = &'a Item>>(&mut self, iter: It)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<Item> Extend<Item> for MinMax<Item>
where Item: PartialOrd,

Source§

fn extend<It: IntoIterator<Item = Item>>(&mut self, iter: It)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<Item> From<Item> for MinMax<Item>

Source§

fn from(item: Item) -> Self

Converts to this type from the input type.
Source§

impl<'a, Item> FromIterator<&'a Item> for MinMax<Item>
where Item: PartialOrd + Clone,

Source§

fn from_iter<It: IntoIterator<Item = &'a Item>>(iter: It) -> Self

Creates a value from an iterator. Read more
Source§

impl<Item> FromIterator<Item> for MinMax<Item>
where Item: PartialOrd,

Source§

fn from_iter<It: IntoIterator<Item = Item>>(iter: It) -> Self

Creates a value from an iterator. Read more
Source§

impl<Item: Copy> Copy for MinMax<Item>

Auto Trait Implementations§

§

impl<Item> Freeze for MinMax<Item>
where Item: Freeze,

§

impl<Item> RefUnwindSafe for MinMax<Item>
where Item: RefUnwindSafe,

§

impl<Item> Send for MinMax<Item>
where Item: Send,

§

impl<Item> Sync for MinMax<Item>
where Item: Sync,

§

impl<Item> Unpin for MinMax<Item>
where Item: Unpin,

§

impl<Item> UnwindSafe for MinMax<Item>
where Item: 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.