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

@@ -29,8 +29,7 @@ pub use self::{check::check, manager::Manager};
use crate::{
Result, err,
error::Error,
utils,
utils::{string::EMPTY, sys},
utils::{self, option::OptionExt, string::EMPTY, sys},
};
/// All the config options for tuwunel.
@@ -2663,14 +2662,12 @@ impl IdentityProvider {
return Ok(client_secret.clone());
}
futures::future::OptionFuture::from(
self.client_secret_file
.as_ref()
.map(tokio::fs::read_to_string),
)
.await
.transpose()?
.ok_or_else(|| err!("No client secret or client secret file configured"))
self.client_secret_file
.as_ref()
.map_async(tokio::fs::read_to_string)
.await
.transpose()?
.ok_or_else(|| err!("No client secret or client secret file configured"))
}
}

View File

@@ -11,7 +11,7 @@ mod topological_sort;
use std::collections::{BTreeMap, BTreeSet};
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, future::OptionFuture};
use futures::{FutureExt, Stream, StreamExt, TryFutureExt};
use ruma::{OwnedEventId, events::StateEventType, room_version_rules::RoomVersionRules};
pub use self::topological_sort::topological_sort;
@@ -26,7 +26,11 @@ use crate::{
Result, debug,
matrix::{Event, TypeStateKey},
trace,
utils::stream::{BroadbandExt, IterStream},
utils::{
BoolExt,
option::OptionExt,
stream::{BroadbandExt, IterStream},
},
};
/// ConflictMap of OwnedEventId specifically.
@@ -102,10 +106,9 @@ where
|| backport_css;
// Since `org.matrix.hydra.11`, fetch the conflicted state subgraph.
let conflicted_subgraph: OptionFuture<_> = consider_conflicted_subgraph
let conflicted_subgraph = consider_conflicted_subgraph
.then(|| conflicted_states.clone().into_values().flatten())
.map(async |ids| conflicted_subgraph_dfs(ids.stream(), fetch))
.into();
.map_async(async |ids| conflicted_subgraph_dfs(ids.stream(), fetch));
let conflicted_subgraph = conflicted_subgraph
.await
@@ -180,9 +183,8 @@ where
// 3. Take all remaining events that werent picked in step 1 and order them by
// the mainline ordering based on the power level in the partially resolved
// state obtained in step 2.
let sorted_remaining_events: OptionFuture<_> = have_remaining_events
.then(move || mainline_sort(power_event.cloned(), remaining_events, fetch))
.into();
let sorted_remaining_events = have_remaining_events
.then_async(move || mainline_sort(power_event.cloned(), remaining_events, fetch));
let sorted_remaining_events = sorted_remaining_events
.await

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