use std::fmt::Debug;
use std::pin::Pin;
use std::task::{Context, Poll};
#[deprecated(note = "FlexFuture is no longer used in this crate", since = "0.12.0")]
pub enum FlexFuture<T> {
Ready(std::future::Ready<T>),
Pending(std::future::Pending<T>),
Generic(Pin<Box<dyn Future<Output = T>>>),
}
#[allow(deprecated)]
impl<T: Debug> Debug for FlexFuture<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FlexFuture::Ready(ready) => ready.fmt(f),
FlexFuture::Pending(pending) => pending.fmt(f),
FlexFuture::Generic(_) => f.debug_tuple("Generic").finish_non_exhaustive(),
}
}
}
#[allow(deprecated)]
impl<T> From<T> for FlexFuture<T> {
fn from(value: T) -> Self {
FlexFuture::Ready(std::future::ready(value))
}
}
#[allow(deprecated)]
impl<T> From<std::future::Ready<T>> for FlexFuture<T> {
fn from(ready: std::future::Ready<T>) -> Self {
FlexFuture::Ready(ready)
}
}
#[allow(deprecated)]
impl<T> From<std::future::Pending<T>> for FlexFuture<T> {
fn from(pending: std::future::Pending<T>) -> Self {
FlexFuture::Pending(pending)
}
}
#[allow(deprecated)]
impl<T> From<Pin<Box<dyn Future<Output = T>>>> for FlexFuture<T> {
fn from(future: Pin<Box<dyn Future<Output = T>>>) -> Self {
FlexFuture::Generic(future)
}
}
#[allow(deprecated)]
impl<T> From<Box<dyn Future<Output = T>>> for FlexFuture<T> {
fn from(future: Box<dyn Future<Output = T>>) -> Self {
FlexFuture::Generic(Box::into_pin(future))
}
}
#[allow(deprecated)]
impl<T> FlexFuture<T> {
pub fn boxed<F>(f: F) -> Self
where
F: Future<Output = T> + 'static,
{
FlexFuture::Generic(Box::pin(f))
}
pub fn into_boxed(self) -> Pin<Box<dyn Future<Output = T>>>
where
T: 'static,
{
match self {
FlexFuture::Ready(ready) => Box::pin(ready),
FlexFuture::Pending(pending) => Box::pin(pending),
FlexFuture::Generic(generic) => generic,
}
}
}
#[allow(deprecated)]
impl<T: 'static> From<FlexFuture<T>> for Pin<Box<dyn Future<Output = T>>> {
fn from(future: FlexFuture<T>) -> Self {
future.into_boxed()
}
}
#[allow(deprecated)]
impl<T> Future for FlexFuture<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
match self.get_mut() {
FlexFuture::Ready(ready) => Pin::new(ready).poll(cx),
FlexFuture::Pending(pending) => Pin::new(pending).poll(cx),
FlexFuture::Generic(generic) => generic.as_mut().poll(cx),
}
}
}