2020-02-19 22:44:53 +00:00
|
|
|
//@ run-pass
|
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
#![feature(coroutines, coroutine_trait)]
|
2020-02-19 22:44:53 +00:00
|
|
|
|
2023-10-19 16:06:43 +00:00
|
|
|
use std::ops::{Coroutine, CoroutineState};
|
2020-02-19 22:44:53 +00:00
|
|
|
|
2020-03-04 16:45:51 +00:00
|
|
|
fn mkstr(my_name: String, my_mood: String) -> String {
|
|
|
|
format!("{} is {}", my_name.trim(), my_mood.trim())
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:06:43 +00:00
|
|
|
fn my_scenario() -> impl Coroutine<String, Yield = &'static str, Return = String> {
|
2024-04-11 13:15:34 +00:00
|
|
|
#[coroutine]
|
2020-02-19 22:44:53 +00:00
|
|
|
|_arg: String| {
|
|
|
|
let my_name = yield "What is your name?";
|
|
|
|
let my_mood = yield "How are you feeling?";
|
2020-03-04 16:45:51 +00:00
|
|
|
mkstr(my_name, my_mood)
|
2020-02-19 22:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut my_session = Box::pin(my_scenario());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
my_session.as_mut().resume("_arg".to_string()),
|
2023-10-19 16:06:43 +00:00
|
|
|
CoroutineState::Yielded("What is your name?")
|
2020-02-19 22:44:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
my_session.as_mut().resume("Your Name".to_string()),
|
2023-10-19 16:06:43 +00:00
|
|
|
CoroutineState::Yielded("How are you feeling?")
|
2020-02-19 22:44:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
my_session.as_mut().resume("Sensory Organs".to_string()),
|
2023-10-19 16:06:43 +00:00
|
|
|
CoroutineState::Complete("Your Name is Sensory Organs".to_string())
|
2020-02-19 22:44:53 +00:00
|
|
|
);
|
|
|
|
}
|