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
|
use generator::*;
#[derive(Debug)]
enum Action {
Play(&'static str),
Stop,
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum State {
Playing,
Stopped,
}
use crate::Action::*;
use crate::State::*;
fn main() {
let mut cd_player = Gn::new_scoped(|mut s| {
let mut state = Stopped;
loop {
// println!("{:?}", *state);
// in release mod without this there is bugs!!!!! (rustc 1.59.0 (9d1b2106e 2022-02-23))
std::sync::atomic::compiler_fence(std::sync::atomic::Ordering::AcqRel);
match state {
Stopped => match s.get_yield() {
Some(Play(t)) => {
println!("I'm playing {}", t);
state = Playing;
}
Some(Stop) => println!("I'm already stopped"),
_ => unreachable!("some thing wrong"),
},
Playing => match s.get_yield() {
Some(Stop) => {
println!("I'm stopped");
state = Stopped;
}
Some(Play(_)) => println!("should first stop"),
_ => unreachable!("some thing wrong"),
},
}
s.yield_with(state);
}
});
for _ in 0..1000 {
let ret = cd_player.send(Play("hello world"));
assert_eq!(ret, Playing);
let ret = cd_player.send(Play("hello another day"));
assert_eq!(ret, Playing);
let ret = cd_player.send(Stop);
assert_eq!(ret, Stopped);
let ret = cd_player.send(Stop);
assert_eq!(ret, Stopped);
let ret = cd_player.send(Play("hello another day"));
assert_eq!(ret, Playing);
let ret = cd_player.send(Stop);
assert_eq!(ret, Stopped);
}
}
|