Struct fringe::generator::Generator [] [src]

pub struct Generator<Input: Send, Output: Send, Stack: Stack> { /* fields omitted */ }

Generator wraps a function and allows suspending its execution more than once, returning a value each time.

The first time resume(input0) is called, the function is called as f(yielder, input0). It runs until it suspends its execution through yielder.suspend(output0), after which resume(input0) returns output0. The function can be resumed again using resume(input1), after which yielder.suspend(output0) returns input1, and so on. Once the function returns, the resume() call will return None, and it will return None every time it is called after that.

If the generator function panics, the panic is propagated through the resume() call as usual.

After the generator function returns or panics, it is safe to reclaim the generator stack using unwrap().

state() can be used to determine whether the generator function has returned; the state is State::Runnable after creation and suspension, and State::Unavailable once the generator function returns or panics.

When the input type is (), a generator implements the Iterator trait.

Example

use fringe::{OsStack, Generator};

let stack = OsStack::new(0).unwrap();
let mut add_one = Generator::new(stack, move |yielder, mut input| {
  loop {
    if input == 0 { break }
    input = yielder.suspend(input + 1)
  }
});
println!("{:?}", add_one.resume(2)); // prints Some(3)
println!("{:?}", add_one.resume(3)); // prints Some(4)
println!("{:?}", add_one.resume(0)); // prints None

Iterator example

use fringe::{OsStack, Generator};

let stack = OsStack::new(0).unwrap();
let mut nat = Generator::new(stack, move |yielder, ()| {
  for i in 1.. { yielder.suspend(i) }
});
println!("{:?}", nat.next()); // prints Some(0)
println!("{:?}", nat.next()); // prints Some(1)
println!("{:?}", nat.next()); // prints Some(2)

Methods

impl<Input, Output, Stack> Generator<Input, Output, Stack> where
    Input: Send,
    Output: Send,
    Stack: Stack
[src]

Creates a new generator.

See also the contract that needs to be fulfilled by stack.

Same as new, but does not require stack to have a guard page.

This function is unsafe because the generator function can easily violate memory safety by overflowing the stack. It is useful in environments where guarded stacks do not exist, e.g. in absence of an MMU.

See also the contract that needs to be fulfilled by stack.

Resumes the generator and return the next value it yields. If the generator function has returned, returns None.

Returns the state of the generator.

Extracts the stack from a generator when the generator function has returned. If the generator function has not returned (i.e. self.state() == State::Runnable), panics.

Trait Implementations

impl<Input: Debug + Send, Output: Debug + Send, Stack: Debug + Stack> Debug for Generator<Input, Output, Stack>
[src]

Formats the value using the given formatter.

impl<Output, Stack> Iterator for Generator<(), Output, Stack> where
    Output: Send,
    Stack: Stack
[src]

The type of the elements being iterated over.

Advances the iterator and returns the next value. Read more

Returns the bounds on the remaining length of the iterator. Read more

Consumes the iterator, counting the number of iterations and returning it. Read more

Consumes the iterator, returning the last element. Read more

Returns the nth element of the iterator. Read more

🔬 This is a nightly-only experimental API. (iterator_step_by)

unstable replacement of Range::step_by

Creates an iterator starting at the same point, but stepping by the given amount at each iteration. Read more

Takes two iterators and creates a new iterator over both in sequence. Read more

'Zips up' two iterators into a single iterator of pairs. Read more

Takes a closure and creates an iterator which calls that closure on each element. Read more

Creates an iterator which uses a closure to determine if an element should be yielded. Read more

Creates an iterator that both filters and maps. Read more

Creates an iterator which gives the current iteration count as well as the next value. Read more

Creates an iterator which can use peek to look at the next element of the iterator without consuming it. Read more

Creates an iterator that [skip]s elements based on a predicate. Read more

Creates an iterator that yields elements based on a predicate. Read more

Creates an iterator that skips the first n elements. Read more

Creates an iterator that yields its first n elements. Read more

An iterator adaptor similar to [fold] that holds internal state and produces a new iterator. Read more

Creates an iterator that works like map, but flattens nested structure. Read more

Creates an iterator which ends after the first [None]. Read more

Do something with each element of an iterator, passing the value on. Read more

Borrows an iterator, rather than consuming it. Read more

Transforms an iterator into a collection. Read more

Consumes an iterator, creating two collections from it. Read more

An iterator adaptor that applies a function, producing a single, final value. Read more

Tests if every element of the iterator matches a predicate. Read more

Tests if any element of the iterator matches a predicate. Read more

Searches for an element of an iterator that satisfies a predicate. Read more

Searches for an element in an iterator, returning its index. Read more

Searches for an element in an iterator from the right, returning its index. Read more

Returns the maximum element of an iterator. Read more

Returns the minimum element of an iterator. Read more

Returns the element that gives the maximum value from the specified function. Read more

Returns the element that gives the maximum value with respect to the specified comparison function. Read more

Returns the element that gives the minimum value from the specified function. Read more

Returns the element that gives the minimum value with respect to the specified comparison function. Read more

Reverses an iterator's direction. Read more

Converts an iterator of pairs into a pair of containers. Read more

Creates an iterator which [clone]s all of its elements. Read more

Repeats an iterator endlessly. Read more

Sums the elements of an iterator. Read more

Iterates over the entire iterator, multiplying all the elements Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Determines if the elements of this Iterator are equal to those of another. Read more

Determines if the elements of this Iterator are unequal to those of another. Read more

Determines if the elements of this Iterator are lexicographically less than those of another. Read more

Determines if the elements of this Iterator are lexicographically less or equal to those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than or equal to those of another. Read more