veilid_tools/
mutable_future.rs

1use super::*;
2
3pub struct MutableFuture<O, T: Future<Output = O>> {
4    inner: Arc<Mutex<Pin<Box<T>>>>,
5}
6
7impl<O, T: Future<Output = O>> MutableFuture<O, T> {
8    pub fn new(inner: T) -> Self {
9        Self {
10            inner: Arc::new(Mutex::new(Box::pin(inner))),
11        }
12    }
13
14    pub fn set(&self, inner: T) {
15        *self.inner.lock() = Box::pin(inner);
16    }
17}
18
19impl<O, T: Future<Output = O>> Clone for MutableFuture<O, T> {
20    fn clone(&self) -> Self {
21        Self {
22            inner: self.inner.clone(),
23        }
24    }
25}
26
27impl<O, T: Future<Output = O>> Future for MutableFuture<O, T> {
28    type Output = O;
29    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
30        let mut inner = self.inner.lock();
31        T::poll(inner.as_mut(), cx)
32    }
33}