Simplify types, mitigate expansion; eliminate unnecessary move.

Further simplify future::BoolExt toward type expansion mitigation.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-10-21 02:00:57 +00:00
parent 2a662445b6
commit aa4486dfdf
7 changed files with 78 additions and 74 deletions

View File

@@ -7,7 +7,7 @@ use tuwunel_core::{
is_equal_to, is_true,
utils::{
BoolExt, FutureBoolExt, IterStream, ReadyExt,
future::{OptionExt, ReadyEqExt},
future::{self, OptionExt, ReadyEqExt},
},
};
@@ -115,19 +115,16 @@ pub(super) async fn filter_room(
})
.into();
match_encrypted
.is_none_or(is_true!())
.and3(
match_invite.is_none_or(is_true!()),
match_direct.is_none_or(is_true!()),
match_direct_member.is_none_or(is_true!()),
)
.and3(
match_space_child.is_none_or(is_true!()),
match_room_type.is_none_or(is_true!()),
match_room_tag.is_none_or(is_true!()),
)
.await
future::and7(
match_invite.is_none_or(is_true!()),
match_encrypted.is_none_or(is_true!()),
match_direct.is_none_or(is_true!()),
match_direct_member.is_none_or(is_true!()),
match_space_child.is_none_or(is_true!()),
match_room_type.is_none_or(is_true!()),
match_room_tag.is_none_or(is_true!()),
)
.await
}
#[tracing::instrument(name = "filter_meta", level = "trace", skip_all)]

View File

@@ -1,4 +1,4 @@
#![type_length_limit = "589824"] //TODO: REDUCE ME
#![type_length_limit = "262144"] //TODO: REDUCE ME
#![allow(clippy::toplevel_ref_arg)]
pub mod client;

View File

@@ -1,4 +1,5 @@
//! Extended external extensions to futures::FutureExt
#![allow(clippy::many_single_char_names, clippy::impl_trait_in_params)]
use std::marker::Unpin;
@@ -16,8 +17,6 @@ pub trait BoolExt
where
Self: Future<Output = bool> + Send,
{
type Result;
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
where
B: Future<Output = bool> + Send + Unpin,
@@ -46,15 +45,11 @@ impl<Fut> BoolExt for Fut
where
Fut: Future<Output = bool> + Send,
{
type Result = crate::Result<(), ()>;
fn or<B>(self, b: B) -> impl Future<Output = bool> + Send
where
B: Future<Output = bool> + Send + Unpin,
Self: Sized + Unpin,
{
let test = |test: bool| test.ok_or(Self::Result::Err(()));
select_ok([Left(self.map(test)), Right(b.map(test))]).map(|res| res.is_ok())
}
@@ -63,8 +58,6 @@ where
B: Future<Output = bool> + Send,
Self: Sized,
{
let test = |test: bool| test.ok_or(Self::Result::Err(()));
try_join(self.map(test), b.map(test)).map(|res| res.is_ok())
}
@@ -74,8 +67,6 @@ where
C: Future<Output = bool> + Send,
Self: Sized,
{
let test = |test: bool| test.ok_or(Self::Result::Err(()));
try_join3(self.map(test), b.map(test), c.map(test)).map(|res| res.is_ok())
}
@@ -86,8 +77,6 @@ where
D: Future<Output = bool> + Send,
Self: Sized,
{
let test = |test: bool| test.ok_or(Self::Result::Err(()));
try_join4(self.map(test), b.map(test), c.map(test), d.map(test)).map(|res| res.is_ok())
}
}
@@ -97,9 +86,7 @@ where
I: Iterator<Item = F> + Send,
F: Future<Output = bool> + Send,
{
type Result = crate::Result<(), ()>;
let args = args.map(|a| a.map(|a| a.ok_or(Result::Err(()))));
let args = args.map(|a| a.map(test));
try_join_all(args).map(|res| res.is_ok())
}
@@ -109,9 +96,51 @@ where
I: Iterator<Item = F> + Send,
F: Future<Output = bool> + Send + Unpin,
{
type Result = crate::Result<(), ()>;
let args = args.map(|a| a.map(|a| a.ok_or(Result::Err(()))));
let args = args.map(|a| a.map(test));
select_ok(args).map(|res| res.is_ok())
}
pub fn and4(
a: impl Future<Output = bool> + Send,
b: impl Future<Output = bool> + Send,
c: impl Future<Output = bool> + Send,
d: impl Future<Output = bool> + Send,
) -> impl Future<Output = bool> + Send {
a.and3(b, c, d)
}
pub fn and5(
a: impl Future<Output = bool> + Send,
b: impl Future<Output = bool> + Send,
c: impl Future<Output = bool> + Send,
d: impl Future<Output = bool> + Send,
e: impl Future<Output = bool> + Send,
) -> impl Future<Output = bool> + Send {
a.and2(b, c).and2(d, e)
}
pub fn and6(
a: impl Future<Output = bool> + Send,
b: impl Future<Output = bool> + Send,
c: impl Future<Output = bool> + Send,
d: impl Future<Output = bool> + Send,
e: impl Future<Output = bool> + Send,
f: impl Future<Output = bool> + Send,
) -> impl Future<Output = bool> + Send {
a.and3(b, c, d).and2(e, f)
}
pub fn and7(
a: impl Future<Output = bool> + Send,
b: impl Future<Output = bool> + Send,
c: impl Future<Output = bool> + Send,
d: impl Future<Output = bool> + Send,
e: impl Future<Output = bool> + Send,
f: impl Future<Output = bool> + Send,
g: impl Future<Output = bool> + Send,
) -> impl Future<Output = bool> + Send {
a.and3(b, c, d).and3(e, f, g)
}
fn test(test: bool) -> crate::Result<(), ()> { test.ok_or(()) }

View File

@@ -5,7 +5,7 @@ mod option_stream;
mod ready_eq_ext;
mod try_ext_ext;
pub use bool_ext::{BoolExt, and, or};
pub use bool_ext::{BoolExt, and, and4, and5, and6, and7, or};
pub use ext_ext::ExtExt;
pub use option_ext::OptionExt;
pub use option_stream::OptionStream;

View File

@@ -22,7 +22,7 @@ use ruma::{
};
use tokio::sync::{Mutex, MutexGuard};
use tuwunel_core::{
Err, Error, Event, PduEvent, Result, implement,
Err, Error, Event, Result, implement,
utils::{
IterStream,
future::{BoolExt, TryExtExt},
@@ -513,12 +513,12 @@ async fn cache_insert(
fn get_space_child_events<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = PduEvent> + Send + 'a {
) -> impl Stream<Item = impl Event> + Send + 'a {
self.services
.state_accessor
.room_state_keys_with_ids(room_id, &StateEventType::SpaceChild)
.ready_filter_map(Result::ok)
.broad_filter_map(async move |(state_key, event_id): (_, OwnedEventId)| {
.broad_filter_map(async |(state_key, event_id): (_, OwnedEventId)| {
self.services
.timeline
.get_pdu(&event_id)
@@ -526,7 +526,7 @@ fn get_space_child_events<'a>(
.ok()
.await
})
.ready_filter_map(move |(state_key, pdu)| {
.ready_filter_map(|(state_key, pdu)| {
if let Ok(content) = pdu.get_content::<SpaceChildEventContent>() {
if content.via.is_empty() {
return None;

View File

@@ -1,7 +1,5 @@
use std::borrow::Borrow;
use futures::{Stream, StreamExt, TryFutureExt};
use ruma::{EventId, RoomId, events::StateEventType};
use ruma::{OwnedEventId, RoomId, events::StateEventType};
use serde::Deserialize;
use tuwunel_core::{
Result, err, implement,
@@ -82,16 +80,12 @@ pub fn room_state_full_pdus<'a>(
/// `state_key`).
#[implement(super::Service)]
#[tracing::instrument(skip(self), level = "debug")]
pub async fn room_state_get_id<Id>(
pub async fn room_state_get_id(
&self,
room_id: &RoomId,
event_type: &StateEventType,
state_key: &str,
) -> Result<Id>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
) -> Result<OwnedEventId> {
self.services
.state
.get_room_shortstatehash(room_id)
@@ -103,15 +97,11 @@ where
/// `event_id` from the current state.
#[implement(super::Service)]
#[tracing::instrument(skip(self), level = "debug")]
pub fn room_state_keys_with_ids<'a, Id>(
pub fn room_state_keys_with_ids<'a>(
&'a self,
room_id: &'a RoomId,
event_type: &'a StateEventType,
) -> impl Stream<Item = Result<(StateKey, Id)>> + Send + 'a
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned + 'a,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
) -> impl Stream<Item = Result<(StateKey, OwnedEventId)>> + Send + 'a {
self.services
.state
.get_room_shortstatehash(room_id)

View File

@@ -1,8 +1,8 @@
use std::{borrow::Borrow, ops::Deref, sync::Arc};
use std::{ops::Deref, sync::Arc};
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, future::try_join, pin_mut};
use ruma::{
EventId, OwnedEventId, UserId,
OwnedEventId, UserId,
events::{
StateEventType,
room::member::{MembershipState, RoomMemberEventContent},
@@ -138,16 +138,12 @@ pub async fn state_get(
/// Returns a single EventId from `room_id` with key (`event_type`,
/// `state_key`).
#[implement(super::Service)]
pub async fn state_get_id<Id>(
pub async fn state_get_id(
&self,
shortstatehash: ShortStateHash,
event_type: &StateEventType,
state_key: &str,
) -> Result<Id>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
) -> Result<OwnedEventId> {
let shorteventid = self
.state_get_shortid(shortstatehash, event_type, state_key)
.await?;
@@ -209,15 +205,11 @@ pub fn state_type_pdus<'a>(
/// Iterates the state_keys for an event_type in the state; current state
/// event_id included.
#[implement(super::Service)]
pub fn state_keys_with_ids<'a, Id>(
pub fn state_keys_with_ids<'a>(
&'a self,
shortstatehash: ShortStateHash,
event_type: &'a StateEventType,
) -> impl Stream<Item = (StateKey, Id)> + Send + 'a
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned + 'a,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
) -> impl Stream<Item = (StateKey, OwnedEventId)> + Send + 'a {
let state_keys_with_short_ids = self
.state_keys_with_shortids(shortstatehash, event_type)
.unzip()
@@ -371,14 +363,10 @@ pub fn state_full_pdus(
/// Builds a StateMap by iterating over all keys that start
/// with state_hash, this gives the full state for the given state_hash.
#[implement(super::Service)]
pub fn state_full_ids<'a, Id>(
&'a self,
pub fn state_full_ids(
&self,
shortstatehash: ShortStateHash,
) -> impl Stream<Item = (ShortStateKey, Id)> + Send + 'a
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned + 'a,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
) -> impl Stream<Item = (ShortStateKey, OwnedEventId)> + Send + '_ {
let shortids = self
.state_full_shortids(shortstatehash)
.ignore_err()