Skip to content

Commit

Permalink
Feat: BoxFuture which implements Send (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
har23k authored Jun 18, 2024
1 parent 1344092 commit 7494153
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion monoio-compat/src/box_future.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{future::Future, io};

use monoio::BufResult;
use reusable_box_future::ReusableLocalBoxFuture;
use reusable_box_future::{ReusableLocalBoxFuture, ReusableBoxFuture};

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

Expand Down Expand Up @@ -83,3 +83,40 @@ impl Default for MaybeArmedBoxFuture<io::Result<()>> {
}
}
}

#[derive(Debug)]
pub struct SendableMaybeArmedBoxFuture<T> {
slot: ReusableBoxFuture<T>,
armed: bool,
}

impl<T> SendableMaybeArmedBoxFuture<T> {
pub fn armed(&self) -> bool {
self.armed
}
pub fn arm_future<F>(&mut self, f: F)
where
F: Future<Output = T> + 'static + Send,
{
self.armed = true;
self.slot.set(f);
}
pub fn poll(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<T> {
match self.slot.poll(cx) {
r @ std::task::Poll::Ready(_) => {
self.armed = false;
r
}
p => p,
}
}
pub fn new<F>(f: F) -> Self
where
F: Future<Output = T> + 'static + Send,
{
Self {
slot: ReusableBoxFuture::new(f),
armed: false,
}
}
}

0 comments on commit 7494153

Please sign in to comment.