diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index 6098f995..780e9474 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -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}, diff --git a/src/core/utils/option.rs b/src/core/utils/option.rs index ce07841b..e3002690 100644 --- a/src/core/utils/option.rs +++ b/src/core/utils/option.rs @@ -1,11 +1,39 @@ -use futures::future::OptionFuture; +use futures::{FutureExt, Stream, future::OptionFuture}; -pub trait OptionExt { - fn map_async O>(self, f: F) -> OptionFuture; -} +use super::IterStream; -impl OptionExt for Option { - fn map_async O>(self, f: F) -> OptionFuture { - OptionFuture::<_>::from(self.map(f)) +pub trait OptionExt +where + Fut: Future + Send, + U: Send, +{ + fn map_async(self, f: F) -> OptionFuture + where + F: FnOnce(T) -> Fut; + + #[inline] + fn map_stream(self, f: F) -> impl Stream + Send + where + F: FnOnce(T) -> Fut, + Self: Sized, + { + self.map_async(f) + .map(Option::into_iter) + .map(IterStream::stream) + .flatten_stream() + } +} + +impl OptionExt for Option +where + Fut: Future + Send, + U: Send, +{ + #[inline] + fn map_async(self, f: F) -> OptionFuture + where + F: FnOnce(T) -> Fut, + { + self.map(f).into() } }