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.
- Slightly less efficient than
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>
impl<Output, Item, Func> DynFolder<Output, Item, Func>
sourcepub fn new(initial: Output, func: Func) -> Selfwhere
Func: Fn(Output, Item) -> Output,
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.
sourcepub fn into_inner(self) -> Output
pub fn into_inner(self) -> Output
Returns the contained value, consuming the self value.
Trait Implementations§
source§impl<Output, Item, Func> Extend<Item> for DynFolder<Output, Item, Func>where
Func: Fn(Output, Item) -> Output,
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)
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)
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)
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
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more