1use futures::task::Context;
2use futures::FutureExt;
3use std::future::Future;
4use std::pin::Pin;
5use std::task;
6use tokio::sync::oneshot;
7
8pub struct FutureReply<T>(oneshot::Receiver<T>);
9
10impl<T> FutureReply<T> {
11 pub fn channel() -> (FutureReplySender<T>, FutureReply<T>) {
12 let (tx, rx) = oneshot::channel();
13 (FutureReplySender(tx), FutureReply(rx))
14 }
15}
16
17pub struct FutureReplySender<T>(oneshot::Sender<T>);
18impl<T> FutureReplySender<T> {
19 pub fn send(self, value: T) -> Result<(), T> {
20 self.0.send(value)
21 }
22}
23
24impl<T> Future for FutureReply<T> {
25 type Output = Option<T>;
26
27 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> task::Poll<Self::Output> {
28 self.0.poll_unpin(cx).map(|res| res.ok())
29 }
30}