Struct autofolder::ImplReduce

source ·
pub struct ImplReduce<Item> { /* private fields */ }
Expand description

The ImplReduce type uses the ReduceTrait for the reduce function.

This is essentially an ImplFolder that doesn’t require an initial value - the first value to be “reduced” is incorporated as-is instead.

Example:

use autofolder::*;

// Create a type wrapper for usize:
#[derive(Debug)]
pub struct Usize(usize);

// Create an autofolder that collects the max Usize.
let mut max = ImplReduce::<Usize>::default();

// Implement ReduceTrait for the desired ImplReduce type.
autofolder_impl_reducetrait!(|a, b| -> Usize {
    Usize(std::cmp::max(a.0, b.0))
});

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

// We can then peek at the running output:
println!("Partial max is {:?}", max.as_ref());

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

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

Implementations§

source§

impl<Item> ImplReduce<Item>

source

pub fn new(initial: Item) -> Self

Creates a new ImplReduce with the provided initial value.

source

pub fn into_inner(self) -> Option<Item>

Deconstruct self and return the inner value.

source

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

Returns a reference to the inner value, if there is one.

source

pub fn reduce(&mut self, item: Item)where Self: ReduceTrait<Item>,

Reduce the given item into the current self item.

Trait Implementations§

source§

impl<Item: Clone> Clone for ImplReduce<Item>

source§

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

Returns a copy 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 ImplReduce<Item>

source§

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

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

impl<Item> Default for ImplReduce<Item>

source§

fn default() -> Self

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

impl<Item> Extend<Item> for ImplReduce<Item>where ImplReduce<Item>: ReduceTrait<Item>,

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 ImplReduce<Item>

source§

fn from(item: Item) -> Self

Converts to this type from the input type.
source§

impl<Item> FromIterator<Item> for ImplReduce<Item>where ImplReduce<Item>: ReduceTrait<Item>,

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 ImplReduce<Item>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<Item> UnwindSafe for ImplReduce<Item>where Item: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.