2026-01-21 23:50:52 +00:00
|
|
|
use futures::{FutureExt, Stream, future::OptionFuture};
|
2026-01-17 05:38:09 +05:00
|
|
|
|
2026-01-21 23:50:52 +00:00
|
|
|
use super::IterStream;
|
|
|
|
|
|
|
|
|
|
pub trait OptionExt<Fut, T, U>
|
|
|
|
|
where
|
|
|
|
|
Fut: Future<Output = U> + Send,
|
|
|
|
|
U: Send,
|
|
|
|
|
{
|
|
|
|
|
fn map_async<F>(self, f: F) -> OptionFuture<Fut>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(T) -> Fut;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn map_stream<F>(self, f: F) -> impl Stream<Item = U> + Send
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(T) -> Fut,
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
self.map_async(f)
|
|
|
|
|
.map(Option::into_iter)
|
|
|
|
|
.map(IterStream::stream)
|
|
|
|
|
.flatten_stream()
|
|
|
|
|
}
|
2026-01-17 05:38:09 +05:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 23:50:52 +00:00
|
|
|
impl<Fut, T, U> OptionExt<Fut, T, U> for Option<T>
|
|
|
|
|
where
|
|
|
|
|
Fut: Future<Output = U> + Send,
|
|
|
|
|
U: Send,
|
|
|
|
|
{
|
|
|
|
|
#[inline]
|
|
|
|
|
fn map_async<F>(self, f: F) -> OptionFuture<Fut>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(T) -> Fut,
|
|
|
|
|
{
|
|
|
|
|
self.map(f).into()
|
2026-01-17 05:38:09 +05:00
|
|
|
}
|
|
|
|
|
}
|