forked from M-Labs/libfringe
impl Iterator for Generator<Input=()>.
This commit is contained in:
parent
145c18125a
commit
d4b7c427c9
|
@ -42,6 +42,8 @@ pub enum State {
|
||||||
/// the state is `State::Runnable` after creation and suspension, and `State::Unavailable`
|
/// the state is `State::Runnable` after creation and suspension, and `State::Unavailable`
|
||||||
/// once the generator function returns or panics.
|
/// once the generator function returns or panics.
|
||||||
///
|
///
|
||||||
|
/// When the input type is `()`, a generator implements the Iterator trait.
|
||||||
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -58,6 +60,20 @@ pub enum State {
|
||||||
/// println!("{:?}", add_one.resume(3)); // prints Some(4)
|
/// println!("{:?}", add_one.resume(3)); // prints Some(4)
|
||||||
/// println!("{:?}", add_one.resume(0)); // prints None
|
/// 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.generate(i) }
|
||||||
|
/// });
|
||||||
|
/// println!("{:?}", nat.next()); // prints Some(0)
|
||||||
|
/// println!("{:?}", nat.next()); // prints Some(1)
|
||||||
|
/// println!("{:?}", nat.next()); // prints Some(2)
|
||||||
|
/// ```
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Generator<Input: Send, Output: Send, Stack: stack::Stack> {
|
pub struct Generator<Input: Send, Output: Send, Stack: stack::Stack> {
|
||||||
state: State,
|
state: State,
|
||||||
|
@ -199,3 +215,10 @@ impl<Input, Output, Stack> Yielder<Input, Output, Stack>
|
||||||
self.generate_bare(Some(item))
|
self.generate_bare(Some(item))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<Output, Stack> Iterator for Generator<(), Output, Stack>
|
||||||
|
where Output: Send, Stack: stack::Stack {
|
||||||
|
type Item = Output;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> { self.resume(()) }
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
// This file is part of libfringe, a low-level green threading library.
|
||||||
|
// Copyright (c) whitequark <whitequark@whitequark.org>
|
||||||
|
// See the LICENSE file included in this distribution.
|
||||||
|
extern crate fringe;
|
||||||
|
|
||||||
|
use fringe::OsStack;
|
||||||
|
use fringe::generator::Generator;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn producer() {
|
||||||
|
let stack = OsStack::new(0).unwrap();
|
||||||
|
let mut gen = Generator::new(stack, move |yielder, ()| {
|
||||||
|
for i in 0.. { yielder.generate(i) }
|
||||||
|
});
|
||||||
|
assert_eq!(gen.next(), Some(0));
|
||||||
|
assert_eq!(gen.next(), Some(1));
|
||||||
|
assert_eq!(gen.next(), Some(2));
|
||||||
|
}
|
Loading…
Reference in New Issue