2018-09-06 20:41:12 +08:00
|
|
|
//@ run-pass
|
|
|
|
|
2023-10-20 05:46:28 +08:00
|
|
|
#![feature(coroutines, coroutine_trait)]
|
2017-10-07 22:36:28 +08:00
|
|
|
|
2023-10-20 00:06:43 +08:00
|
|
|
use std::ops::{Coroutine, CoroutineState};
|
2018-10-05 02:49:38 +08:00
|
|
|
use std::pin::Pin;
|
2017-10-07 22:36:28 +08:00
|
|
|
|
|
|
|
fn foo(_: &str) -> String {
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
|
2023-10-20 00:06:43 +08:00
|
|
|
fn bar(baz: String) -> impl Coroutine<(), Yield = String, Return = ()> {
|
2024-04-11 21:15:34 +08:00
|
|
|
#[coroutine] move || {
|
2017-10-07 22:36:28 +08:00
|
|
|
yield foo(&baz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo2(_: &str) -> Result<String, ()> {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
|
2023-10-20 00:06:43 +08:00
|
|
|
fn bar2(baz: String) -> impl Coroutine<(), Yield = String, Return = ()> {
|
2024-04-11 21:15:34 +08:00
|
|
|
#[coroutine] move || {
|
2017-10-07 22:36:28 +08:00
|
|
|
if let Ok(quux) = foo2(&baz) {
|
|
|
|
yield quux;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-01-26 03:03:10 +08:00
|
|
|
assert_eq!(
|
|
|
|
Pin::new(&mut bar(String::new())).resume(()),
|
2023-10-20 00:06:43 +08:00
|
|
|
CoroutineState::Yielded(String::new())
|
2020-01-26 03:03:10 +08:00
|
|
|
);
|
2023-10-20 00:06:43 +08:00
|
|
|
assert_eq!(Pin::new(&mut bar2(String::new())).resume(()), CoroutineState::Complete(()));
|
2017-10-07 22:36:28 +08:00
|
|
|
}
|