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
use futures::{task, Async, Future, Poll};
use lapin::{
    pinky_swear::{NotifyReady, PinkySwear},
    Result,
};

use crate::Error;

pub struct ConfirmationFuture<T, I = ()>(PinkySwear<Result<T>, I>);

pub(crate) struct Watcher(task::Task);

impl Default for Watcher {
    fn default() -> Self {
        Self(task::current())
    }
}

impl NotifyReady for Watcher {
    fn notify(&self) {
        self.0.notify();
    }
}

impl<T: Send + 'static, I: 'static> Future for ConfirmationFuture<T, I> {
    type Item = T;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0.subscribe(Box::new(Watcher::default()));
        Ok(if let Some(res) = self.0.try_wait() {
            Async::Ready(res?)
        } else {
            Async::NotReady
        })
    }
}

impl<T, I> From<PinkySwear<Result<T>, I>> for ConfirmationFuture<T, I> {
    fn from(promise: PinkySwear<Result<T>, I>) -> Self {
        Self(promise)
    }
}