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
65
66
use std::{mem, sync::Arc};

use parking_lot::Mutex;
use tracing::{instrument, trace};

use crate::{state::State, Error, Result};

/// Used to complete a [`ControlledFuture`][crate::ControlledFuture] so it may resolve to the given value.
///
/// Dropping a [`FutureClicker`] without calling [`FutureClicker::complete`] will
/// cause the [`ControlledFuture`][crate::ControlledFuture] to panic.
#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct FutureClicker<T: Unpin + Send + 'static> {
    pub(crate) state: Arc<Mutex<State<T>>>,
}

impl<T: Unpin + Send + 'static> FutureClicker<T> {
    /// Complete the associated [`ControlledFuture`][crate::ControlledFuture].
    ///
    /// # Errors
    /// - [`Error::AlreadyCompleted`] - The [`ControlledFuture`][crate::ControlledFuture] future is already resolved.
    /// - [`Error::CompleterDropped`] - The [`FutureClicker`] has already been dropped.
    #[instrument(skip_all)]
    pub fn complete(self, value: T) -> Result<()> {
        use State::{Complete, Dropped, Incomplete, Waiting};

        trace!("complete");

        let mut state = self.state.lock_arc();

        trace!("have lock");

        match mem::replace(&mut *state, State::Complete(Some(value))) {
            Incomplete => Ok(()),
            Waiting(waker) => {
                waker.wake();
                Ok(())
            }
            old @ Complete(_) => {
                *state = old;
                Err(Error::AlreadyCompleted)
            }
            old @ Dropped => {
                *state = old;
                Err(Error::CompleterDropped)
            }
        }
    }
}

impl<T: Unpin + Send + 'static> Drop for FutureClicker<T> {
    #[instrument(skip_all)]
    fn drop(&mut self) {
        use State::{Complete, Dropped, Incomplete, Waiting};
        trace!("Drop");
        let mut state = self.state.lock_arc();
        trace!("Locked");

        match mem::replace(&mut *state, Dropped) {
            Incomplete | Dropped => {}
            Waiting(waker) => waker.wake(),
            old @ Complete(_) => *state = old,
        }
    }
}