1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crateFixedPointCoro;
use cratefrom_control_flow;
use cratewith_state;
/// A coroutine that yields its inputs `n` times, then returns `()`.
///
/// This is most commonly used as the argument to the `compose()` operator to
/// limit how many times the source coroutine yields.
///
/// ```rust
/// use cocoro::Coro;
/// use cocoro::take;
/// use cocoro::yield_with;
///
/// fn iota<R>() -> impl Coro<(), i32, R> {
/// let mut i = 0;
/// yield_with(move |()| {
/// i += 1;
/// i
/// })
/// }
///
/// // iota() by itself will continue yielding forever.
/// iota()
/// // Limit the yielded values to 3.
/// .compose(take(3))
/// .assert_yields(1, ())
/// .assert_yields(2, ())
/// .assert_yields(3, ())
/// .assert_returns((), ());
/// ```
///
/// If the source coroutine returns something other than `()`, you must remap
/// the source coroutine's return type or the `take()` coroutine's return type
/// to match the other. One easy way to do this is to lift the return value
/// into an `Option`:
///
/// ```rust
/// use cocoro::Coro;
/// use cocoro::Yield;
/// use cocoro::from_fn;
/// use cocoro::just_return;
/// use cocoro::take;
///
/// from_fn(|()| Yield(1, from_fn(|()| Yield(2, just_return("done")))))
/// // Map the source coroutine's return value to `Some` to indicate that it
/// // returned.
/// .map_return(Some)
/// // Map the `take()` coroutine's return value to `None` to indicate that
/// // the limit was reached, and to match the type of the source.
/// .compose(take(5).map_return(|_| None))
/// .assert_yields(1, ())
/// .assert_yields(2, ())
/// .assert_returns(Some("done"), ());
/// ```