Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: BoxFuture which implements Send #271

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
}
}
Loading