alto_chain/
utils.rs

1use futures::{
2    channel::oneshot,
3    task::{Context, Poll},
4};
5use std::pin::Pin;
6
7/// A future that manages the state of a [oneshot::Sender].
8pub struct OneshotClosedFut<'a, T> {
9    sender: &'a mut oneshot::Sender<T>,
10}
11
12impl<'a, T> OneshotClosedFut<'a, T> {
13    /// Create a new [OneshotClosedFut].
14    pub fn new(sender: &'a mut oneshot::Sender<T>) -> Self {
15        Self { sender }
16    }
17}
18
19impl<T> futures::Future for OneshotClosedFut<'_, T> {
20    type Output = ();
21
22    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
23        // Use poll_canceled to check if the receiver has dropped the channel
24        match self.sender.poll_canceled(cx) {
25            Poll::Ready(()) => Poll::Ready(()), // Receiver dropped, channel closed
26            Poll::Pending => Poll::Pending,     // Channel still open
27        }
28    }
29}