Skip to main content

agner_test_actor/
exited.rs

1use std::fmt;
2use std::future::Future;
3use std::pin::Pin;
4
5use agner_actors::Exit;
6use agner_utils::std_error_pp::StdErrorPP;
7use tokio::sync::Mutex;
8
9pub enum Exited {
10    Waiting(Pin<Box<dyn Future<Output = Exit> + Send + Sync + 'static>>),
11    Ready(Exit),
12}
13
14pub async fn wait(mutex: &Mutex<Exited>) -> Exit {
15    let mut locked = mutex.lock().await;
16    let exited = &mut *locked;
17    match exited {
18        Exited::Ready(reason) => reason.to_owned(),
19        Exited::Waiting(join_handle) => {
20            let reason = join_handle.await;
21            *exited = Exited::Ready(reason.to_owned());
22            reason
23        },
24    }
25}
26
27impl fmt::Debug for Exited {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::Ready(reason) => write!(f, "Exited: {}", reason.pp()),
31            Self::Waiting { .. } => write!(f, "Waiting"),
32        }
33    }
34}