Trait pushgen::traits::Generator

source ·
pub trait Generator {
    type Output;

    // Required method
    fn run(
        &mut self,
        output: impl FnMut(Self::Output) -> ValueResult
    ) -> GeneratorResult;

    // Provided method
    fn try_advance(&mut self, n: NonZeroUsize) -> (usize, GeneratorResult) { ... }
}
Expand description

Trait for generating values into a closure.

When a Generator is run() it generates values that are fed an output closure. It continues to feed values to the closure for as long as it can, unless the closure returns ValueResult::Stop.

When all values have been generated the run() method returns GeneratorResult::Complete. If output returns ValueResult::Stop for any value the generator must not call output with any further values and return GeneratorResult::Stopped as well.

The generator must not assume that it won’t be called again after it returns.

Example

A generic generator can be written like this:

use pushgen::{Generator, ValueResult, GeneratorResult};
struct GenericGenerator<Out, Gen>
where
    Gen: FnMut() -> Option<Out>,
{
    generator: Gen,
}

impl<Out, Gen> Generator for GenericGenerator<Out, Gen>
    where
        Gen: FnMut() -> Option<Out>,
{
    type Output = Out;

    fn run(&mut self, mut output: impl FnMut(Self::Output) -> ValueResult) -> GeneratorResult {
        while let Some(value) = (self.generator)() {
            if output(value) == ValueResult::Stop {
                return GeneratorResult::Stopped;
            }
        }
        GeneratorResult::Complete
    }
}

Required Associated Types§

source

type Output

Data-type generated by the generator.

Required Methods§

source

fn run( &mut self, output: impl FnMut(Self::Output) -> ValueResult ) -> GeneratorResult

Run the generator, emitting values to the output closure.

New values are emitted for as long as the closure returns ValueResult::MoreValues. If the closure returns ValueResult::Stop the generator must return GeneratorResult::Stopped.

Provided Methods§

source

fn try_advance(&mut self, n: NonZeroUsize) -> (usize, GeneratorResult)

Try to advance the generator n values, ignoring them.

This function has a default implementation but should be implemented by adaptors and source generators for additional performance gains.

Returns

The number of steps that the generator was actually advanced, and if the generator was stopped or completed.

Examples
use pushgen::{IntoGenerator, Generator, GeneratorExt, GeneratorResult};
use core::num::NonZeroUsize;
let data = [1, 2, 3, 4, 5];
let mut gen = data.into_gen();
let advance_result = gen.try_advance(NonZeroUsize::new(3).unwrap());
assert_eq!(advance_result, (3, GeneratorResult::Stopped));
assert_eq!(gen.next(), Ok(4));
assert_eq!(gen.next(), Ok(5));

Implementations on Foreign Types§

source§

impl<T: Generator> Generator for &mut T

§

type Output = <T as Generator>::Output

source§

fn run( &mut self, output: impl FnMut(Self::Output) -> ValueResult ) -> GeneratorResult

source§

fn try_advance(&mut self, n: NonZeroUsize) -> (usize, GeneratorResult)

Implementors§

source§

impl<'a, Src, T> Generator for Cloned<Src>where T: 'a + Clone, Src: Generator<Output = &'a T>,

§

type Output = T

source§

impl<'a, Src, T> Generator for Copied<Src>where T: 'a + Copy, Src: Generator<Output = &'a T>,

§

type Output = T

source§

impl<'a, T> Generator for SliceGenerator<'a, T>

source§

impl<'a, T> Generator for MultiStoppingGen<'a, T>

Available on crate feature test only.
source§

impl<'a, T> Generator for StoppingGen<'a, T>

Available on crate feature test only.
source§

impl<First, Second> Generator for Chain<First, Second>where First: Generator, Second: Generator<Output = First::Output>,

§

type Output = <First as Generator>::Output

source§

impl<Gen> Generator for Skip<Gen>where Gen: Generator,

§

type Output = <Gen as Generator>::Output

source§

impl<Gen, Func, Out> Generator for FilterMap<Gen, Func>where Gen: Generator, Func: FnMut(Gen::Output) -> Option<Out>,

§

type Output = Out

source§

impl<Gen, Func, Out> Generator for Map<Gen, Func>where Gen: Generator, Func: FnMut(Gen::Output) -> Out,

§

type Output = Out

source§

impl<Gen, Pred> Generator for Filter<Gen, Pred>where Gen: Generator, Pred: FnMut(&Gen::Output) -> bool,

§

type Output = <Gen as Generator>::Output

source§

impl<I: Iterator> Generator for FromIter<I>

§

type Output = <I as Iterator>::Item

source§

impl<L, R> Generator for Either<L, R>where L: Generator, R: Generator<Output = L::Output>,

§

type Output = <L as Generator>::Output

source§

impl<Left, Right> Generator for Zip<Left, Right>where Left: Generator, Right: Generator,

§

type Output = (<Left as Generator>::Output, <Right as Generator>::Output)

source§

impl<Src> Generator for Dedup<Src>where Src: Generator, Src::Output: PartialEq,

§

type Output = <Src as Generator>::Output

source§

impl<Src> Generator for Enumerate<Src>where Src: Generator,

§

type Output = (usize, <Src as Generator>::Output)

source§

impl<Src> Generator for Flatten<Src>where Src: Generator, Src::Output: IntoGenerator,

source§

impl<Src> Generator for Reverse<Src>where Src: ReverseGenerator,

§

type Output = <Src as Generator>::Output

source§

impl<Src, F> Generator for Inspect<Src, F>where Src: Generator, F: FnMut(&Src::Output),

§

type Output = <Src as Generator>::Output

source§

impl<Src, P> Generator for SkipWhile<Src, P>where Src: Generator, P: FnMut(&Src::Output) -> bool,

§

type Output = <Src as Generator>::Output

source§

impl<Src, P> Generator for TakeWhile<Src, P>where Src: Generator, P: FnMut(&Src::Output) -> bool,

§

type Output = <Src as Generator>::Output

source§

impl<Src, State, F, B> Generator for Scan<Src, State, F>where Src: Generator, F: FnMut(&mut State, Src::Output) -> Option<B>,

§

type Output = B

source§

impl<Src: Clone + Generator> Generator for Cycle<Src>

§

type Output = <Src as Generator>::Output

source§

impl<Src: Generator> Generator for StepBy<Src>

§

type Output = <Src as Generator>::Output

source§

impl<Src: Generator> Generator for Take<Src>

§

type Output = <Src as Generator>::Output

source§

impl<T> Generator for BoxedGenerator<T>

Available on crate feature std only.
§

type Output = T

source§

impl<T> Generator for OptionGen<T>

§

type Output = T

source§

impl<T, F> Generator for FromFn<F>where F: FnMut() -> Option<T>,

§

type Output = T

source§

impl<T, const N: usize> Generator for ArrayGenerator<T, N>

§

type Output = T