future_clicker/
state.rs

1use std::{fmt, sync::Arc, task::Waker};
2
3use parking_lot::Mutex;
4use strum::Display;
5
6#[derive(Display)]
7pub(crate) enum State<T> {
8    Incomplete,
9    Waiting(Waker),
10    Complete(Option<T>),
11    Dropped,
12}
13
14impl<T> State<T> {
15    pub(crate) fn new() -> (Arc<Mutex<Self>>, Arc<Mutex<Self>>) {
16        let this = Self::Incomplete.into_arc_mutex();
17        (this.clone(), this)
18    }
19
20    pub(crate) fn new_completed(value: T) -> Arc<Mutex<Self>> {
21        Self::Complete(Some(value)).into_arc_mutex()
22    }
23
24    fn into_arc_mutex(self) -> Arc<Mutex<Self>> {
25        Arc::new(self.into())
26    }
27}
28
29impl<T> fmt::Debug for State<T> {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "{}", self)
32    }
33}
34
35impl<T> From<Option<T>> for State<T> {
36    fn from(value: Option<T>) -> Self {
37        match value {
38            value @ Some(_) => Self::Complete(value),
39            None => Self::Incomplete,
40        }
41    }
42}