Alex Mikhalev
4 years ago
3 changed files with 40 additions and 44 deletions
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
use pin_project::pin_project; |
||||
use std::{ |
||||
future::Future, |
||||
ops::Deref, |
||||
pin::Pin, |
||||
task::{Context, Poll}, |
||||
}; |
||||
|
||||
#[pin_project] |
||||
#[derive(Debug, Clone)] |
||||
#[must_use = "futures do nothing unless you `.await` or poll them"] |
||||
pub struct OptionFuture<F>(#[pin] Option<F>); |
||||
|
||||
impl<F: Future> Future for OptionFuture<F> { |
||||
type Output = Option<F::Output>; |
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
||||
match self.project().0.as_pin_mut() { |
||||
Some(x) => x.poll(cx).map(Some), |
||||
None => Poll::Ready(None), |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl<F> Deref for OptionFuture<F> { |
||||
type Target = Option<F>; |
||||
|
||||
fn deref(&self) -> &Self::Target { |
||||
&self.0 |
||||
} |
||||
} |
||||
|
||||
impl<T> From<Option<T>> for OptionFuture<T> { |
||||
fn from(option: Option<T>) -> Self { |
||||
OptionFuture(option) |
||||
} |
||||
} |
Loading…
Reference in new issue