diapause 0.2.0

Coroutines/generators for stable Rust via code transformation — no async, no Pin, no allocation, no unsafe code
Documentation
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! The `?` operator inside coroutine bodies: Result and Option
//! coroutines, error conversion, and interaction with yields.

use diapause::{Coroutine, CoroutineState};

#[derive(Debug, PartialEq)]
pub struct ParseError;

fn parse(s: &str) -> Result<u32, ParseError> {
    s.parse().map_err(|_| ParseError)
}

#[diapause::coroutine(yield = u32)]
fn sum_two(a: &'static str, b: &'static str) -> Result<u32, ParseError> {
    let x: u32 = parse(a)?;
    yield_!(x);
    let y: u32 = parse(b)?;
    Ok(x + y)
}

#[test]
fn result_success_path() {
    let mut c = sum_two("1", "2");
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(()), CoroutineState::Complete(Ok(3)));
}

#[test]
fn result_failure_before_first_yield() {
    let mut c = sum_two("x", "2");
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

#[test]
fn result_failure_after_resume() {
    let mut c = sum_two("1", "x");
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(()), CoroutineState::Complete(Err(ParseError)));
}

/// A completing `?` transitions to Done; further resumes panic as after
/// a normal completion.
#[test]
#[should_panic(expected = "Already done")]
fn resume_after_try_exit_panics() {
    let mut c = sum_two("x", "2");
    let _ = c.start();
    let _ = c.resume(());
}

#[derive(Debug, PartialEq)]
pub struct WrappedError(&'static str);

impl From<ParseError> for WrappedError {
    fn from(_: ParseError) -> Self {
        WrappedError("bad number")
    }
}

/// `?` converts the error through `From`, as in a plain function.
#[diapause::coroutine(yield = u32)]
fn converts_error(s: &'static str) -> Result<u32, WrappedError> {
    let x: u32 = parse(s)?;
    yield_!(x);
    Ok(x)
}

#[test]
fn result_from_conversion() {
    let mut c = converts_error("nope");
    assert_eq!(
        c.start(),
        CoroutineState::Complete(Err(WrappedError("bad number")))
    );
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn option_try(start: Option<u32>) -> Option<u32> {
    let x: u32 = start?;
    let r = yield_!(x);
    Some(x.checked_add(r)?)
}

#[test]
fn option_success_path() {
    let mut c = option_try(Some(4));
    assert_eq!(c.start(), CoroutineState::Yielded(4));
    assert_eq!(c.resume(1), CoroutineState::Complete(Some(5)));
}

#[test]
fn option_none_short_circuits() {
    let mut c = option_try(None);
    assert_eq!(c.start(), CoroutineState::Complete(None));
}

#[test]
fn option_none_in_yield_value() {
    let mut c = option_try(Some(u32::MAX));
    assert_eq!(c.start(), CoroutineState::Yielded(u32::MAX));
    assert_eq!(c.resume(1), CoroutineState::Complete(None));
}

/// `?` → yield → `?`, with the second `?` on a value computed from the
/// resume argument.
#[diapause::coroutine(yield = u32, resume = &'static str)]
fn try_yield_try(a: &'static str) -> Result<u32, ParseError> {
    let x: u32 = parse(a)?;
    let b = yield_!(x);
    let y: u32 = parse(b)?;
    Ok(x * 100 + y)
}

#[test]
fn try_then_yield_then_try() {
    let mut c = try_yield_try("7");
    assert_eq!(c.start(), CoroutineState::Yielded(7));
    assert_eq!(c.resume("42"), CoroutineState::Complete(Ok(742)));

    let mut c = try_yield_try("7");
    assert_eq!(c.start(), CoroutineState::Yielded(7));
    assert_eq!(c.resume("x"), CoroutineState::Complete(Err(ParseError)));
}

/// `?` inside an if that contains no yield (an opaque statement).
#[diapause::coroutine(yield = u32)]
fn try_in_opaque_if(s: &'static str, check: bool) -> Result<u32, ParseError> {
    let mut x: u32 = 1;
    if check {
        x += parse(s)?;
    } else {
        x += 100;
    }
    yield_!(x);
    Ok(x)
}

#[test]
fn try_inside_opaque_if() {
    let mut c = try_in_opaque_if("9", true);
    assert_eq!(c.start(), CoroutineState::Yielded(10));
    assert_eq!(c.resume(()), CoroutineState::Complete(Ok(10)));

    let mut c = try_in_opaque_if("x", true);
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));

    let mut c = try_in_opaque_if("x", false);
    assert_eq!(c.start(), CoroutineState::Yielded(101));
}

/// `?` inside the value expression of a yield.
#[diapause::coroutine(yield = u32)]
fn try_in_yield_value(s: &'static str) -> Result<u32, ParseError> {
    yield_!(parse(s)?);
    Ok(0)
}

#[test]
fn try_inside_yield_value() {
    let mut c = try_in_yield_value("3");
    assert_eq!(c.start(), CoroutineState::Yielded(3));
    assert_eq!(c.resume(()), CoroutineState::Complete(Ok(0)));

    let mut c = try_in_yield_value("x");
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

/// Nested `?`: the operand of a `?` itself contains a `?`.
#[diapause::coroutine(yield = u32)]
fn nested_try(a: Option<&'static str>) -> Result<u32, ParseError> {
    let x: u32 = parse(a.ok_or(ParseError)?)?;
    yield_!(x);
    Ok(x)
}

#[test]
fn nested_try_operands() {
    let mut c = nested_try(Some("5"));
    assert_eq!(c.start(), CoroutineState::Yielded(5));

    let mut c = nested_try(None);
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));

    let mut c = nested_try(Some("x"));
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

// === `?` on a delegation's completion value ===

/// Errors before its first yield when `s` does not parse.
#[diapause::coroutine(yield = u32, resume = u32)]
fn try_sub(s: &'static str) -> Result<u32, ParseError> {
    let x: u32 = parse(s)?;
    let r = yield_!(x);
    Ok(x + r)
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn delegate_try_let(s: &'static str) -> Result<u32, ParseError> {
    let sub: try_sub::State = try_sub(s);
    let v: u32 = yield_all!(sub)?;
    let r = yield_!(v);
    Ok(v + r)
}

#[test]
fn delegation_try_ok_path() {
    let mut c = delegate_try_let("7");
    assert_eq!(c.start(), CoroutineState::Yielded(7)); // sub yields x
    assert_eq!(c.resume(1), CoroutineState::Yielded(8)); // sub completes Ok(7+1), `?` unwraps
    assert_eq!(c.resume(2), CoroutineState::Complete(Ok(10)));
}

#[test]
fn delegation_try_err_short_circuits() {
    let mut c = delegate_try_let("x");
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

/// Yields once, then errors on resume: the delegation suspends before
/// the `?` takes the Err exit.
#[diapause::coroutine(yield = u32, resume = u32)]
fn yield_then_err(s: &'static str) -> Result<u32, ParseError> {
    let r = yield_!(0);
    let x: u32 = parse(s)?;
    Ok(x + r)
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn delegate_try_mid(s: &'static str) -> Result<u32, ParseError> {
    let sub: yield_then_err::State = yield_then_err(s);
    let v: u32 = yield_all!(sub)?;
    Ok(v + 100)
}

#[test]
fn delegation_err_after_a_forwarded_yield() {
    let mut c = delegate_try_mid("x");
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(1), CoroutineState::Complete(Err(ParseError)));
}

/// A completing delegation `?` transitions to Done like any completion.
#[test]
#[should_panic(expected = "Already done")]
fn resume_after_delegation_try_exit_panics() {
    let mut c = delegate_try_mid("x");
    let _ = c.start();
    let _ = c.resume(1);
    let _ = c.resume(2);
}

/// Statement position: the Ok value (a non-`()` `u32`) is discarded,
/// the Err still exits early.
#[diapause::coroutine(yield = u32, resume = u32)]
fn delegate_try_stmt(s: &'static str) -> Result<u32, ParseError> {
    let sub: try_sub::State = try_sub(s);
    yield_all!(sub)?;
    Ok(1)
}

#[test]
fn statement_position_delegation_try() {
    let mut c = delegate_try_stmt("7");
    assert_eq!(c.start(), CoroutineState::Yielded(7));
    assert_eq!(c.resume(1), CoroutineState::Complete(Ok(1)));

    let mut c = delegate_try_stmt("x");
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

/// Tail position: the delegate completes with a nested
/// `Result<Result<u32, _>, _>`; the `?` unwraps the outer layer and the
/// inner Result is the coroutine's completion value.
#[diapause::coroutine(yield = u32, resume = u32)]
fn nested_result_sub(s: &'static str) -> Result<Result<u32, ParseError>, ParseError> {
    let r = yield_!(0);
    let x: u32 = parse(s)?;
    Ok(Ok(x + r))
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn delegate_try_tail(s: &'static str) -> Result<u32, ParseError> {
    let sub: nested_result_sub::State = nested_result_sub(s);
    yield_all!(sub)?
}

#[test]
fn tail_position_delegation_try() {
    let mut c = delegate_try_tail("7");
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(1), CoroutineState::Complete(Ok(8)));

    let mut c = delegate_try_tail("x");
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(1), CoroutineState::Complete(Err(ParseError)));
}

/// The `box` modifier combines with `?`.
#[diapause::coroutine(yield = u32, resume = u32)]
fn delegate_try_boxed(s: &'static str) -> Result<u32, ParseError> {
    let sub: try_sub::State = try_sub(s);
    let v: u32 = yield_all!(box sub)?;
    Ok(v)
}

#[test]
fn boxed_delegation_try() {
    let mut c = delegate_try_boxed("7");
    assert_eq!(c.start(), CoroutineState::Yielded(7));
    assert_eq!(c.resume(1), CoroutineState::Complete(Ok(8)));

    let mut c = delegate_try_boxed("x");
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

/// `yield_all_resume!` combines with `?` the same way.
#[diapause::coroutine(yield = u32, resume = u32)]
fn delegate_resume_try(sub: try_sub::State, first: u32) -> Result<u32, ParseError> {
    let v: u32 = yield_all_resume!(sub, first)?;
    Ok(v * 10)
}

#[test]
fn resume_delegation_try() {
    let mut sub = try_sub("7");
    assert_eq!(sub.start(), CoroutineState::Yielded(7));
    let mut c = delegate_resume_try(sub, 1); // sub completes Ok(7+1) on entry
    assert_eq!(c.start(), CoroutineState::Complete(Ok(80)));
}

// === `?` inside a `return` value ===

/// The manual-driver shape (a `loop { match sub.start()/resume() }`
/// forwarding yields) with `?` applied inside an unbraced match-arm
/// `return`. Regression: the expression-position return used to be
/// pre-rewritten into a completion block whose construction re-parsed
/// the `?`'s synthesized exit; the opaque rewriter then wrapped it in a
/// second completion and the generated code failed with E0308.
#[diapause::coroutine(yield = u32, resume = u32)]
fn drive(mut sub: try_sub::State) -> Result<u32, ParseError> {
    let mut step: CoroutineState<u32, Result<u32, ParseError>> = sub.start();
    loop {
        match step {
            CoroutineState::Complete(result) => return Ok(result? + 100),
            CoroutineState::Yielded(y) => {
                let r: u32 = yield_!(y);
                step = sub.resume(r);
            }
        }
    }
}

#[test]
fn try_in_a_match_arm_return() {
    let mut c = drive(try_sub("7"));
    assert_eq!(c.start(), CoroutineState::Yielded(7)); // sub yields x
    assert_eq!(c.resume(1), CoroutineState::Complete(Ok(108))); // Ok(7+1)? + 100

    let mut c = drive(try_sub("x"));
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

/// The braced form of the same arm goes through the statement-return
/// path (a `Return` terminator inside the arm's block).
#[diapause::coroutine(yield = u32, resume = u32)]
fn drive_braced(mut sub: try_sub::State) -> Result<u32, ParseError> {
    let mut step: CoroutineState<u32, Result<u32, ParseError>> = sub.start();
    loop {
        match step {
            CoroutineState::Complete(result) => {
                return Ok(result? + 100);
            }
            CoroutineState::Yielded(y) => {
                let r: u32 = yield_!(y);
                step = sub.resume(r);
            }
        }
    }
}

#[test]
fn try_in_a_braced_match_arm_return() {
    let mut c = drive_braced(try_sub("7"));
    assert_eq!(c.start(), CoroutineState::Yielded(7));
    assert_eq!(c.resume(1), CoroutineState::Complete(Ok(108)));

    let mut c = drive_braced(try_sub("x"));
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));
}

/// The same arm return with `?` where the match is a `let` initializer:
/// the returning arm diverges, the yielding arm produces the value.
#[diapause::coroutine(yield = u32, resume = u32)]
fn try_in_stored_arm_return(r: Result<u32, ParseError>, flag: bool) -> Result<u32, ParseError> {
    let x: u32 = match flag {
        true => return Ok(r? + 1),
        false => {
            let v = yield_!(9);
            v
        }
    };
    Ok(x * 2)
}

#[test]
fn try_in_a_stored_arm_return() {
    let mut c = try_in_stored_arm_return(Ok(4), true);
    assert_eq!(c.start(), CoroutineState::Complete(Ok(5)));

    let mut c = try_in_stored_arm_return(Err(ParseError), true);
    assert_eq!(c.start(), CoroutineState::Complete(Err(ParseError)));

    let mut c = try_in_stored_arm_return(Ok(4), false);
    assert_eq!(c.start(), CoroutineState::Yielded(9));
    assert_eq!(c.resume(3), CoroutineState::Complete(Ok(6)));
}

/// `?` inside a closure belongs to the closure, not the coroutine.
#[diapause::coroutine(yield = u32)]
fn closure_try(s: &'static str) -> u32 {
    let double = |s: &str| -> Result<u32, ParseError> { Ok(parse(s)? * 2) };
    let x: u32 = double(s).unwrap_or(0);
    yield_!(x);
    x
}

#[test]
fn try_inside_closure_is_untouched() {
    let mut c = closure_try("21");
    assert_eq!(c.start(), CoroutineState::Yielded(42));
    assert_eq!(c.resume(()), CoroutineState::Complete(42));

    let mut c = closure_try("x");
    assert_eq!(c.start(), CoroutineState::Yielded(0));
}