enum Value<T> {
Ready(T),
Async(core::pin::Pin<Box<dyn core::future::Future<Output = T> + Send + 'static>>),
}
#[must_use]
pub struct MaybeAsync<T> {
inner: Value<T>,
}
impl<T> MaybeAsync<T> {
pub async fn get(self) -> T {
match self.inner {
Value::Ready(x) => x,
Value::Async(x) => x.await,
}
}
pub fn ready(result: T) -> Self {
MaybeAsync {
inner: Value::Ready(result),
}
}
pub fn asynchronous<F>(result: F) -> Self
where
F: core::future::Future<Output = T> + Send + 'static,
{
MaybeAsync {
inner: Value::Async(Box::pin(result)),
}
}
}