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 crateCoro;
use crateSuspended;
;
/// Creates a coroutine from a function that returns a `Suspend`.
///
/// This is the most straightforward way to implement `Coro` without manually
/// implementing the trait for your own data type.
///
/// Using `from_fn()` exclusively to define a coroutine guarantees its entire
/// state machine is known at compile time and can only proceed in the correct
/// order. Each iteration of such a coroutine has a unique type whose `Coro`
/// implementation (specifically, its `Next` associated type) points to the
/// next state.
///
/// This function is an implementation detail in many combinators. However,
/// it's often preferable to use higher-level functions and combinators like
/// `yield_with()` and `recursive()` to implement coroutines.
///
/// # Examples
///
/// ```rust
/// use cocoro::Coro;
/// use cocoro::Returned;
/// use cocoro::Void;
/// use cocoro::Yield;
/// use cocoro::from_fn;
///
/// // A coroutine that yields 3, 2, 1, and then returns with "Blastoff!"
/// #[rustfmt::skip]
/// let countdown = from_fn(|()| {
/// Yield(3, from_fn(|()| {
/// Yield(2, from_fn(|()| {
/// Yield(1, from_fn(|()| {
/// Returned("Blastoff!") })) })) }))
/// });
/// countdown
/// .assert_yields(3, ())
/// .assert_yields(2, ())
/// .assert_yields(1, ())
/// .assert_returns("Blastoff!", ());
/// ```