Introduce OptionFuture helpers

Optimize user directory searches
This commit is contained in:
dasha_uwu
2026-01-17 05:38:09 +05:00
committed by Jason Volk
parent 95121ad905
commit e78bf21085
28 changed files with 454 additions and 567 deletions

View File

@@ -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 }

View File

@@ -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,

View File

@@ -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,

View File

@@ -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
View 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))
}
}