Add map_stream(), trait constraints to OptionExt.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2026-01-21 23:50:52 +00:00
parent 48aa6035f6
commit 948e1681c2
2 changed files with 36 additions and 7 deletions

View File

@@ -33,6 +33,7 @@ pub use self::{
hash::sha256::delimited as calculate_hash,
json::{deserialize_from_str, to_canonical_object},
mutex_map::{Guard as MutexMapGuard, MutexMap},
option::OptionExt,
rand::{shuffle, string as random_string},
stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt},
string::{str_from_bytes, string_from_bytes},

View File

@@ -1,11 +1,39 @@
use futures::future::OptionFuture;
use futures::{FutureExt, Stream, future::OptionFuture};
pub trait OptionExt<T> {
fn map_async<O: Future, F: FnOnce(T) -> O>(self, f: F) -> OptionFuture<O>;
}
use super::IterStream;
impl<T> OptionExt<T> for Option<T> {
fn map_async<O: Future, F: FnOnce(T) -> O>(self, f: F) -> OptionFuture<O> {
OptionFuture::<_>::from(self.map(f))
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()
}
}
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()
}
}