#![allow(unused)]
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::task::{Context, Poll};
pub(crate) struct SeqFuture<F> {
inner: Vec<F>,
idx: usize,
}
impl<F> SeqFuture<F> {
pub(crate) fn new() -> Self {
Self {
inner: Vec::new(),
idx: 0,
}
}
pub(crate) fn push(&mut self, future: F) {
self.inner.push(future);
}
}
impl<F: Future + Unpin> Future for SeqFuture<F> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
while Pin::new(&mut this.inner[this.idx]).poll(cx).is_ready() {
this.idx += 1;
if this.idx == this.inner.len() {
return Poll::Ready(());
}
}
Poll::Pending
}
}
trait RevocableFuture: Future {
fn is_revoked() -> bool;
}
struct NeverRevokedFuture<F> {
inner: F,
}
impl<F: Future> NeverRevokedFuture<F> {
fn new(fut: F) -> Self {
Self { inner: fut }
}
}
impl<T: Future> Future for NeverRevokedFuture<T> {
type Output = T::Output;
#[inline(always)]
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll(cx) }
}
}
impl<T: Future> RevocableFuture for NeverRevokedFuture<T> {
fn is_revoked() -> bool {
false
}
}
struct ConcurrentlyRevocableFuture<F> {
inner: F,
is_revoked: Arc<AtomicBool>,
}