Skip to content

Commit ea6d876

Browse files
committed
Feat: BoxFuture which implements Send
1 parent 1344092 commit ea6d876

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

monoio-compat/src/box_future.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{future::Future, io};
22

33
use monoio::BufResult;
4-
use reusable_box_future::ReusableLocalBoxFuture;
4+
use reusable_box_future::{ReusableLocalBoxFuture, ReusableBoxFuture};
55

66
use crate::buf::{Buf, RawBuf};
77

@@ -83,3 +83,40 @@ impl Default for MaybeArmedBoxFuture<io::Result<()>> {
8383
}
8484
}
8585
}
86+
87+
#[derive(Debug)]
88+
pub struct SendableMaybeArmedBoxFuture<T> {
89+
slot: ReusableBoxFuture<T>,
90+
armed: bool,
91+
}
92+
93+
impl<T> SendableMaybeArmedBoxFuture<T> {
94+
pub fn armed(&self) -> bool {
95+
self.armed
96+
}
97+
pub fn arm_future<F>(&mut self, f: F)
98+
where
99+
F: Future<Output = T> + 'static + Send,
100+
{
101+
self.armed = true;
102+
self.slot.set(f);
103+
}
104+
pub fn poll(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<T> {
105+
match self.slot.poll(cx) {
106+
r @ std::task::Poll::Ready(_) => {
107+
self.armed = false;
108+
r
109+
}
110+
p => p,
111+
}
112+
}
113+
pub fn new<F>(f: F) -> Self
114+
where
115+
F: Future<Output = T> + 'static + Send,
116+
{
117+
Self {
118+
slot: ReusableBoxFuture::new(f),
119+
armed: false,
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)