genawaiter/sync/
engine.rs

1use crate::{core, core::Next};
2use std::{
3    mem,
4    sync::{Arc, Mutex},
5};
6
7pub struct Airlock<Y, R>(Arc<Mutex<Next<Y, R>>>);
8
9impl<Y, R> Default for Airlock<Y, R> {
10    fn default() -> Self {
11        Self(Arc::new(Mutex::new(Next::Empty)))
12    }
13}
14
15impl<Y, R> Clone for Airlock<Y, R> {
16    fn clone(&self) -> Self {
17        Self(self.0.clone())
18    }
19}
20
21impl<Y, R> core::Airlock for Airlock<Y, R> {
22    type Yield = Y;
23    type Resume = R;
24
25    fn peek(&self) -> Next<(), ()> {
26        self.0.lock().unwrap().without_values()
27    }
28
29    fn replace(&self, next: Next<Y, R>) -> Next<Y, R> {
30        mem::replace(&mut self.0.lock().unwrap(), next)
31    }
32}
33
34/// This object lets you yield values from the generator by calling the `yield_`
35/// method.
36///
37/// "Co" can stand for either _controller_ or _coroutine_, depending on how
38/// theoretical you are feeling.
39///
40/// [_See the module-level docs for examples._](.)
41pub type Co<Y, R = ()> = core::Co<Airlock<Y, R>>;