From ea6d876d9ae4ea09e47037c8c44bc92ab44cef19 Mon Sep 17 00:00:00 2001 From: Harsha Manivannan Date: Mon, 17 Jun 2024 17:22:19 +0000 Subject: [PATCH] Feat: BoxFuture which implements Send --- monoio-compat/src/box_future.rs | 39 ++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/monoio-compat/src/box_future.rs b/monoio-compat/src/box_future.rs index a0d37fcd..c51cf093 100644 --- a/monoio-compat/src/box_future.rs +++ b/monoio-compat/src/box_future.rs @@ -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}; @@ -83,3 +83,40 @@ impl Default for MaybeArmedBoxFuture> { } } } + +#[derive(Debug)] +pub struct SendableMaybeArmedBoxFuture { + slot: ReusableBoxFuture, + armed: bool, +} + +impl SendableMaybeArmedBoxFuture { + pub fn armed(&self) -> bool { + self.armed + } + pub fn arm_future(&mut self, f: F) + where + F: Future + 'static + Send, + { + self.armed = true; + self.slot.set(f); + } + pub fn poll(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll { + match self.slot.poll(cx) { + r @ std::task::Poll::Ready(_) => { + self.armed = false; + r + } + p => p, + } + } + pub fn new(f: F) -> Self + where + F: Future + 'static + Send, + { + Self { + slot: ReusableBoxFuture::new(f), + armed: false, + } + } +}