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
//! Value-position yield (v2.1): `let` initializers that are
//! if/match/loop/block expressions containing yields, value-carrying
//! `break`, and control flow as the function's trailing expression.

use diapause::{Coroutine, CoroutineState};

// === let + if ===

#[diapause::coroutine(yield = u32, resume = u32)]
fn pick_if(c: bool) -> u32 {
    let x: u32 = if c {
        let r = yield_!(1);
        r + 10
    } else {
        2
    };
    x * 100
}

#[test]
fn let_if_taken() {
    let mut c = pick_if(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(5), CoroutineState::Complete(1500));
}

#[test]
fn let_if_skipped() {
    let mut c = pick_if(false);
    assert_eq!(c.start(), CoroutineState::Complete(200));
}

/// The result binding is mutable and crosses a later yield, so it is
/// stored in a state variant and unpacked `mut`.
#[diapause::coroutine(yield = u32, resume = u32)]
fn mutable_result(c: bool) -> u32 {
    let mut x: u32 = if c {
        let r = yield_!(1);
        r
    } else {
        5
    };
    x += 1;
    yield_!(x);
    x
}

#[test]
fn let_if_mut_binding_crosses_a_later_yield() {
    let mut c = mutable_result(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(9), CoroutineState::Yielded(10));
    assert_eq!(c.resume(0), CoroutineState::Complete(10));

    let mut c = mutable_result(false);
    assert_eq!(c.start(), CoroutineState::Yielded(6));
    assert_eq!(c.resume(0), CoroutineState::Complete(6));
}

/// `if` without `else` in value position: the false edge produces `()`.
#[diapause::coroutine(yield = u32)]
fn unit_if(c: bool) -> u32 {
    let x: () = if c {
        yield_!(1);
    };
    let () = x;
    9
}

#[test]
fn let_if_without_else() {
    let mut c = unit_if(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(()), CoroutineState::Complete(9));
    let mut c = unit_if(false);
    assert_eq!(c.start(), CoroutineState::Complete(9));
}

// === let + match ===

#[diapause::coroutine(yield = u32, resume = u32)]
fn pick_match(k: u32) -> u32 {
    let x: u32 = match k {
        0 => {
            let r = yield_!(10);
            r
        }
        1 => 111,
        n => {
            // The arm binding may not cross the yield; rebind first.
            let n2: u32 = n;
            yield_!(n2);
            n2 + 1
        }
    };
    x + 1000
}

#[test]
fn let_match_yielding_arm_with_resume_value() {
    let mut c = pick_match(0);
    assert_eq!(c.start(), CoroutineState::Yielded(10));
    assert_eq!(c.resume(7), CoroutineState::Complete(1007));
}

#[test]
fn let_match_opaque_arm() {
    let mut c = pick_match(1);
    assert_eq!(c.start(), CoroutineState::Complete(1111));
}

#[test]
fn let_match_arm_consuming_its_pattern_binding() {
    let mut c = pick_match(5);
    assert_eq!(c.start(), CoroutineState::Yielded(5));
    assert_eq!(c.resume(0), CoroutineState::Complete(1006));
}

// === let + loop with value-carrying break ===

#[diapause::coroutine(yield = u32, resume = u32)]
fn accumulate_until(limit: u32) -> u32 {
    let mut acc: u32 = 0;
    let total: u32 = loop {
        let r = yield_!(acc);
        acc += r;
        if acc >= limit {
            yield_!(999);
            break acc;
        }
    };
    total * 2
}

#[test]
fn let_loop_break_value() {
    let mut c = accumulate_until(10);
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(4), CoroutineState::Yielded(4));
    assert_eq!(c.resume(7), CoroutineState::Yielded(999));
    assert_eq!(c.resume(0), CoroutineState::Complete(22));
}

// === let + labeled block ===

#[diapause::coroutine(yield = u32)]
fn labeled_block(c: bool) -> u32 {
    let x: u32 = 'b: {
        yield_!(1);
        if c {
            yield_!(2);
            break 'b 10;
        }
        20
    };
    x + 1
}

#[test]
fn let_labeled_block_break_value() {
    let mut c = labeled_block(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(()), CoroutineState::Yielded(2));
    assert_eq!(c.resume(()), CoroutineState::Complete(11));
}

#[test]
fn let_labeled_block_tail_value() {
    let mut c = labeled_block(false);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(()), CoroutineState::Complete(21));
}

// === Nesting ===

/// A match arm whose tail is an `if`, and another arm containing a
/// nested value-`loop` `let` before its own tail.
#[diapause::coroutine(yield = u32, resume = u32)]
fn nested(k: u32) -> u32 {
    let x: u32 = match k {
        0 => {
            if k == 0 {
                let r = yield_!(0);
                r
            } else {
                1
            }
        }
        _ => {
            let y: u32 = loop {
                let r = yield_!(5);
                break r + 5;
            };
            y * 2
        }
    };
    x + 100
}

#[test]
fn nested_if_inside_match_arm() {
    let mut c = nested(0);
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(7), CoroutineState::Complete(107));
}

#[test]
fn nested_value_loop_inside_match_arm() {
    let mut c = nested(1);
    assert_eq!(c.start(), CoroutineState::Yielded(5));
    assert_eq!(c.resume(3), CoroutineState::Complete(116));
}

// === Function-tail control flow ===

#[diapause::coroutine(yield = u32, resume = u32)]
fn tail_if(c: bool) -> u32 {
    if c {
        let r = yield_!(1);
        r + 1
    } else {
        yield_!(2);
        20
    }
}

#[test]
fn fn_tail_if_arms_complete_directly() {
    let mut c = tail_if(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(5), CoroutineState::Complete(6));

    let mut c = tail_if(false);
    assert_eq!(c.start(), CoroutineState::Yielded(2));
    assert_eq!(c.resume(0), CoroutineState::Complete(20));
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn tail_match(k: u32) -> u32 {
    match k {
        0 => {
            yield_!(0);
            1
        }
        n => n * 2,
    }
}

#[test]
fn fn_tail_match() {
    let mut c = tail_match(0);
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(0), CoroutineState::Complete(1));

    let mut c = tail_match(21);
    assert_eq!(c.start(), CoroutineState::Complete(42));
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn tail_loop() -> u32 {
    let mut acc: u32 = 0;
    loop {
        let r = yield_!(acc);
        if r == 0 {
            yield_!(acc);
            break acc;
        }
        acc += r;
    }
}

#[test]
fn fn_tail_loop_break_value_completes() {
    let mut c = tail_loop();
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(4), CoroutineState::Yielded(4));
    assert_eq!(c.resume(0), CoroutineState::Yielded(4));
    assert_eq!(c.resume(0), CoroutineState::Complete(4));
}

// === Parenthesized value positions ===
//
// Parens around a yield-containing expression are transparent: the
// parenthesized forms behave exactly like the bare ones.

#[diapause::coroutine(yield = u32, resume = u32)]
fn paren_tail_if(c: bool) -> u32 {
    (if c {
        let r = yield_!(1);
        r + 1
    } else {
        yield_!(2);
        20
    })
}

#[test]
fn paren_fn_tail_if_matches_the_bare_form() {
    let mut c = paren_tail_if(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(5), CoroutineState::Complete(6));

    let mut c = paren_tail_if(false);
    assert_eq!(c.start(), CoroutineState::Yielded(2));
    assert_eq!(c.resume(0), CoroutineState::Complete(20));
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn paren_tail_match(k: u32) -> u32 {
    (match k {
        0 => {
            yield_!(0);
            1
        }
        n => n * 2,
    })
}

#[test]
fn paren_fn_tail_match_matches_the_bare_form() {
    let mut c = paren_tail_match(0);
    assert_eq!(c.start(), CoroutineState::Yielded(0));
    assert_eq!(c.resume(0), CoroutineState::Complete(1));

    let mut c = paren_tail_match(21);
    assert_eq!(c.start(), CoroutineState::Complete(42));
}

#[diapause::coroutine(yield = u32, resume = u32)]
fn paren_tail_block(n: u32) -> u32 {
    ({
        let r = yield_!(n);
        r + n
    })
}

#[test]
fn paren_fn_tail_block() {
    let mut c = paren_tail_block(3);
    assert_eq!(c.start(), CoroutineState::Yielded(3));
    assert_eq!(c.resume(4), CoroutineState::Complete(7));
}

/// The condition of a parenthesized tail `if` hoists like the bare form.
#[diapause::coroutine(yield = u32, resume = u32)]
fn paren_tail_if_cond(x: u32) -> u32 {
    (if yield_!(x) == 0 {
        yield_!(1);
        10
    } else {
        20
    })
}

#[test]
fn paren_tail_if_condition_hoists() {
    let mut c = paren_tail_if_cond(5);
    assert_eq!(c.start(), CoroutineState::Yielded(5));
    assert_eq!(c.resume(0), CoroutineState::Yielded(1));
    assert_eq!(c.resume(9), CoroutineState::Complete(10));

    let mut c = paren_tail_if_cond(5);
    assert_eq!(c.start(), CoroutineState::Yielded(5));
    assert_eq!(c.resume(3), CoroutineState::Complete(20));
}

/// A parenthesized yield-containing tail inside a value block.
#[diapause::coroutine(yield = u32, resume = u32)]
fn paren_block_tail(c: bool) -> u32 {
    let x: u32 = {
        (if c {
            let r = yield_!(1);
            r
        } else {
            2
        })
    };
    x * 10
}

#[test]
fn paren_tail_of_a_value_block() {
    let mut c = paren_block_tail(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(7), CoroutineState::Complete(70));

    let mut c = paren_block_tail(false);
    assert_eq!(c.start(), CoroutineState::Complete(20));
}

/// Statement-position parenthesized control flow containing yield.
#[diapause::coroutine(yield = u32)]
fn paren_stmt_if(c: bool) -> u32 {
    (if c {
        yield_!(1);
    });
    7
}

#[test]
fn paren_statement_if() {
    let mut c = paren_stmt_if(true);
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(()), CoroutineState::Complete(7));
    let mut c = paren_stmt_if(false);
    assert_eq!(c.start(), CoroutineState::Complete(7));
}

// === Annotation not required when the binding never enters a state ===

/// Every arm diverges via `break`, so the binding is only ever defined
/// and used inside one dispatch arm and needs no annotation.
#[diapause::coroutine(yield = u32, resume = u32)]
fn no_annotation_needed() -> u32 {
    let out: u32 = loop {
        let x = loop {
            let r = yield_!(1);
            break r * 2;
        };
        break x + 1;
    };
    out
}

#[test]
fn unannotated_binding_confined_to_one_arm() {
    let mut c = no_annotation_needed();
    assert_eq!(c.start(), CoroutineState::Yielded(1));
    assert_eq!(c.resume(10), CoroutineState::Complete(21));
}