Struct autofolder::DynFolder

source ·
pub struct DynFolder<Output, Item, Func> { /* private fields */ }
Expand description

The DynFolder type uses a struct field for the folding function.

  • Pros:
    • Folding function can use any type, builtin or otherwise.
    • Can’t be used with .collect() because instances have to be explicitly built with a folding function.
  • Cons:
    • Slightly less efficient than ImplFolder due to the use of dynamic dispatch - we are effectively using a function pointer instead of a function call, after all.
    • Each instance can use a different folding function, provided as a constructor argument.

Example:

use autofolder::*;

// Create an autofolder that sums `u16` items into an `usize` output.
let mut sum = DynFolder::<usize, u16, _>::new(7, |a, b| a + b as usize);

// We can "fold-in" individual items:
sum.fold(3);

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

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

// And finally consume the autofolder to get the final output value:
println!("Total sum is {}", sum.into_inner());

Implementations§

source§

impl<Output, Item, Func> DynFolder<Output, Item, Func>

source

pub fn new(initial: Output, func: Func) -> Selfwhere Func: Fn(Output, Item) -> Output,

Creates a new DynFolder with the provided initial value and folding function.

source

pub fn into_inner(self) -> Output

Returns the contained value, consuming the self value.

source

pub fn fold(&mut self, item: Item)where Func: Fn(Output, Item) -> Output,

Folds an individual value into self.

Trait Implementations§

source§

impl<Output, Item, Func> AsRef<Output> for DynFolder<Output, Item, Func>

source§

fn as_ref(&self) -> &Output

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<Output: Clone, Item: Clone, Func: Clone> Clone for DynFolder<Output, Item, Func>

source§

fn clone(&self) -> DynFolder<Output, Item, Func>

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<Output, Item, Func> Debug for DynFolder<Output, Item, Func>where Output: Debug,

source§

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

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

impl<Output, Item, Func> Extend<Item> for DynFolder<Output, Item, Func>where Func: Fn(Output, Item) -> Output,

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<Output: Copy, Item: Copy, Func: Copy> Copy for DynFolder<Output, Item, Func>

Auto Trait Implementations§

§

impl<Output, Item, Func> RefUnwindSafe for DynFolder<Output, Item, Func>where Func: RefUnwindSafe, Item: RefUnwindSafe, Output: RefUnwindSafe,

§

impl<Output, Item, Func> Send for DynFolder<Output, Item, Func>where Func: Send, Item: Send, Output: Send,

§

impl<Output, Item, Func> Sync for DynFolder<Output, Item, Func>where Func: Sync, Item: Sync, Output: Sync,

§

impl<Output, Item, Func> Unpin for DynFolder<Output, Item, Func>where Func: Unpin, Item: Unpin, Output: Unpin,

§

impl<Output, Item, Func> UnwindSafe for DynFolder<Output, Item, Func>where Func: UnwindSafe, Item: UnwindSafe, Output: 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<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.