Struct 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> Freeze for ImplFolder<Output, Item>
where Output: Freeze,

§

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

§

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

§

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

§

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

§

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