Introduce OptionFuture helpers
Optimize user directory searches
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
//! Trait BoolExt
|
||||
|
||||
use futures::future::OptionFuture;
|
||||
|
||||
/// Boolean extensions and chain.starters
|
||||
pub trait BoolExt {
|
||||
fn and<T>(self, t: Option<T>) -> Option<T>;
|
||||
@@ -50,6 +52,8 @@ pub trait BoolExt {
|
||||
|
||||
fn or_some<T>(self, t: T) -> Option<T>;
|
||||
|
||||
fn then_async<O: Future, F: FnOnce() -> O>(self, f: F) -> OptionFuture<O>;
|
||||
|
||||
fn then_none<T>(self) -> Option<T>;
|
||||
|
||||
fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E>;
|
||||
@@ -126,6 +130,11 @@ impl BoolExt for bool {
|
||||
#[inline]
|
||||
fn or_some<T>(self, t: T) -> Option<T> { self.is_false().then_some(t) }
|
||||
|
||||
#[inline]
|
||||
fn then_async<O: Future, F: FnOnce() -> O>(self, f: F) -> OptionFuture<O> {
|
||||
OptionFuture::<_>::from(self.then(f))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn then_none<T>(self) -> Option<T> { Option::<T>::None }
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ mod try_ext_ext;
|
||||
pub use self::{
|
||||
bool_ext::{BoolExt, and, and4, and5, and6, and7, or},
|
||||
ext_ext::ExtExt,
|
||||
option_ext::OptionExt,
|
||||
option_ext::OptionFutureExt,
|
||||
option_stream::OptionStream,
|
||||
ready_bool_ext::ReadyBoolExt,
|
||||
ready_eq_ext::ReadyEqExt,
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use futures::{Future, FutureExt, future::OptionFuture};
|
||||
|
||||
pub trait OptionExt<T> {
|
||||
pub trait OptionFutureExt<T> {
|
||||
fn is_none_or(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future<Output = bool> + Send;
|
||||
|
||||
fn is_some_and(self, f: impl FnOnce(&T) -> bool + Send) -> impl Future<Output = bool> + Send;
|
||||
@@ -16,7 +16,7 @@ pub trait OptionExt<T> {
|
||||
T: Default;
|
||||
}
|
||||
|
||||
impl<T, Fut> OptionExt<T> for OptionFuture<Fut>
|
||||
impl<T, Fut> OptionFutureExt<T> for OptionFuture<Fut>
|
||||
where
|
||||
Fut: Future<Output = T> + Send,
|
||||
T: Send,
|
||||
|
||||
@@ -9,6 +9,7 @@ pub mod hash;
|
||||
pub mod json;
|
||||
pub mod math;
|
||||
pub mod mutex_map;
|
||||
pub mod option;
|
||||
pub mod rand;
|
||||
pub mod result;
|
||||
pub mod set;
|
||||
|
||||
11
src/core/utils/option.rs
Normal file
11
src/core/utils/option.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use futures::future::OptionFuture;
|
||||
|
||||
pub trait OptionExt<T> {
|
||||
fn map_async<O: Future, F: FnOnce(T) -> O>(self, f: F) -> OptionFuture<O>;
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user