Struct autofolder::ImplFolder

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

The ImplFolder type uses the FolderTrait for the folding function.

  • Pros:
    • Slighly more efficient than DynFolder due to monomorphization, which turns .fold calls into direct function calls.
    • Can be used with .collect() if the output type implements Default.
  • Cons:
    • Folding function can only use types defined in the user crate, which is a limitation of using traits.
    • Each parameterized ImplFolder, defined by the pair of types, can only have one folding function.

Example:

use autofolder::*;

// Create a type wrapper for usize:
pub struct Usize(usize);

// Create an autofolder that sums `u16` items into an `Usize` output.
let mut sum = ImplFolder::<Usize, u16>::new(Usize(7));

// Implement FolderTrait for the desired ImplFolder type.
autofolder_impl_foldertrait!(|a: Usize, b: u16| {
    Usize(a.0 + 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().0);

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

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

Implementations§

source§

impl<Output, Item> ImplFolder<Output, Item>

source

pub fn new(initial: Output) -> Self

Creates a new ImplFolder with the provided initial value.

source

pub fn into_inner(self) -> Output

Deconstruct self and return the inner value.

source

pub fn fold(&mut self, item: Item)where Self: FolderTrait<Output, Item>,

Folds an individual value into self.

Trait Implementations§

source§

impl<Output, Item> AsRef<Output> for ImplFolder<Output, Item>

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> Clone for ImplFolder<Output, Item>

source§

fn clone(&self) -> ImplFolder<Output, 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<Output: Debug, Item: Debug> Debug for ImplFolder<Output, Item>

source§

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

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

impl<Output, Item> Default for ImplFolder<Output, Item>where Output: Default,

source§

fn default() -> Self

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

impl<Output, Item> Extend<Item> for ImplFolder<Output, Item>where ImplFolder<Output, Item>: FolderTrait<Output, 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<Output, Item> From<Output> for ImplFolder<Output, Item>

source§

fn from(output: Output) -> Self

Converts to this type from the input type.
source§

impl<Output, Item> FromIterator<Item> for ImplFolder<Output, Item>where Output: Default, ImplFolder<Output, Item>: FolderTrait<Output, Item>,

source§

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

Creates a value from an iterator. Read more
source§

impl<Output: Copy, Item: Copy> Copy for ImplFolder<Output, Item>

Auto Trait Implementations§

§

impl<Output, Item> RefUnwindSafe for ImplFolder<Output, Item>where Item: RefUnwindSafe, Output: RefUnwindSafe,

§

impl<Output, Item> Send for ImplFolder<Output, Item>where Item: Send, Output: Send,

§

impl<Output, Item> Sync for ImplFolder<Output, Item>where Item: Sync, Output: Sync,

§

impl<Output, Item> Unpin for ImplFolder<Output, Item>where Item: Unpin, Output: Unpin,

§

impl<Output, Item> UnwindSafe for ImplFolder<Output, Item>where 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<!> 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.