use std::{future::Future, io}; use monoio::BufResult; use reusable_box_future::ReusableLocalBoxFuture; use crate::buf::{Buf, RawBuf}; #[derive(Debug)] pub struct MaybeArmedBoxFuture { slot: ReusableLocalBoxFuture, armed: bool, } impl MaybeArmedBoxFuture { pub fn armed(&self) -> bool { self.armed } pub fn arm_future(&mut self, f: F) where F: Future + 'static, { 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, { Self { slot: ReusableLocalBoxFuture::new(f), armed: false, } } } impl Default for MaybeArmedBoxFuture> { fn default() -> Self { Self { slot: ReusableLocalBoxFuture::new(async { (Ok(0), Buf::uninit()) }), armed: false, } } } impl Default for MaybeArmedBoxFuture> { fn default() -> Self { Self { slot: ReusableLocalBoxFuture::new(async { (Ok(0), RawBuf::uninit()) }), armed: false, } } } impl Default for MaybeArmedBoxFuture> where T: Default, { fn default() -> Self { Self { slot: ReusableLocalBoxFuture::new(async { (Ok(0), T::default()) }), armed: false, } } } impl Default for MaybeArmedBoxFuture> { fn default() -> Self { Self { slot: ReusableLocalBoxFuture::new(async { Ok(()) }), armed: false, } } }