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
69
70
71
72
73
74
75
76
77
78
79
80
use PhantomData;
use ControlFlow;
use *;
use *;
use crateCoro;
use crateSuspend;
/// Creates a new coroutine from a function that returns [`ControlFlow`].
///
/// The provided function `f` is called each time the coroutine is resumed
/// with an input of type `I`.
/// - If `f` returns `ControlFlow::Continue(y)`, the coroutine yields `y`.
/// - If `f` returns `ControlFlow::Break(r)`, the coroutine returns `r`.
///
/// This constructor is useful for creating coroutines with state that
/// determine dynamically whether to yield or return based on the input.
///
/// NOTE: when the `Try` trait is stabilized, this function will likely be
/// renamed to `try_with` and be extended to support anything that implements
/// `Try`.
///
/// # Examples
///
/// ```rust
/// use core::ops::ControlFlow;
///
/// use cocoro::Coro;
/// use cocoro::Void;
/// use cocoro::from_control_flow;
///
/// let mut countdown = 3;
/// from_control_flow(move |input: i32| {
/// if countdown > 0 {
/// let old_countdown = countdown;
/// countdown -= 1;
/// ControlFlow::Continue(format!(
/// "Input: {}, Countdown: {}",
/// input, old_countdown
/// ))
/// } else {
/// ControlFlow::Break(format!("Final input: {}", input))
/// }
/// })
/// .assert_yields("Input: 10, Countdown: 3".to_string(), 10)
/// .assert_yields("Input: 20, Countdown: 2".to_string(), 20)
/// .assert_yields("Input: 30, Countdown: 1".to_string(), 30)
/// .assert_returns("Final input: 40".to_string(), 40);
/// ```