Unbox and pin various either-or futures.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-07-27 20:44:21 +00:00
parent 066097a83d
commit 71ef093edc
3 changed files with 38 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
#![type_length_limit = "16384"] //TODO: reduce me #![type_length_limit = "65536"] //TODO: reduce me
#![allow(clippy::toplevel_ref_arg)] #![allow(clippy::toplevel_ref_arg)]
pub mod client; pub mod client;

View File

@@ -1,6 +1,13 @@
use std::{borrow::Borrow, sync::Arc}; use std::{borrow::Borrow, sync::Arc};
use futures::{FutureExt, Stream, TryFutureExt, TryStreamExt, future::select_ok, pin_mut}; use futures::{
Stream, TryFutureExt, TryStreamExt,
future::{
Either::{Left, Right},
select_ok,
},
pin_mut,
};
use ruma::{CanonicalJsonObject, EventId, OwnedUserId, RoomId, UserId, api::Direction}; use ruma::{CanonicalJsonObject, EventId, OwnedUserId, RoomId, UserId, api::Direction};
use tuwunel_core::{ use tuwunel_core::{
Err, PduCount, PduEvent, Result, at, err, Err, PduCount, PduEvent, Result, at, err,
@@ -81,6 +88,7 @@ impl Data {
} }
/// Returns the `count` of this pdu's id. /// Returns the `count` of this pdu's id.
#[inline]
pub(super) async fn get_pdu_count(&self, event_id: &EventId) -> Result<PduCount> { pub(super) async fn get_pdu_count(&self, event_id: &EventId) -> Result<PduCount> {
self.get_pdu_id(event_id) self.get_pdu_id(event_id)
.await .await
@@ -88,6 +96,7 @@ impl Data {
} }
/// Returns the json of a pdu. /// Returns the json of a pdu.
#[inline]
pub(super) async fn get_outlier_pdu_json( pub(super) async fn get_outlier_pdu_json(
&self, &self,
event_id: &EventId, event_id: &EventId,
@@ -99,14 +108,19 @@ impl Data {
} }
/// Returns the json of a pdu. /// Returns the json of a pdu.
#[inline]
pub(super) async fn get_pdu_json(&self, event_id: &EventId) -> Result<CanonicalJsonObject> { pub(super) async fn get_pdu_json(&self, event_id: &EventId) -> Result<CanonicalJsonObject> {
let accepted = self.get_non_outlier_pdu_json(event_id).boxed(); let accepted = self.get_non_outlier_pdu_json(event_id);
let outlier = self.get_outlier_pdu_json(event_id).boxed(); let outlier = self.get_outlier_pdu_json(event_id);
select_ok([accepted, outlier]).await.map(at!(0)) pin_mut!(accepted, outlier);
select_ok([Left(accepted), Right(outlier)])
.await
.map(at!(0))
} }
/// Returns the json of a pdu. /// Returns the json of a pdu.
#[inline]
pub(super) async fn get_non_outlier_pdu_json( pub(super) async fn get_non_outlier_pdu_json(
&self, &self,
event_id: &EventId, event_id: &EventId,
@@ -126,6 +140,7 @@ impl Data {
} }
/// Returns the pdu directly from `eventid_pduid` only. /// Returns the pdu directly from `eventid_pduid` only.
#[inline]
pub(super) async fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<PduEvent> { pub(super) async fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<PduEvent> {
let pduid = self.get_pdu_id(event_id).await?; let pduid = self.get_pdu_id(event_id).await?;
@@ -134,6 +149,7 @@ impl Data {
/// Like get_non_outlier_pdu(), but without the expense of fetching and /// Like get_non_outlier_pdu(), but without the expense of fetching and
/// parsing the PduEvent /// parsing the PduEvent
#[inline]
pub(super) async fn non_outlier_pdu_exists(&self, event_id: &EventId) -> Result { pub(super) async fn non_outlier_pdu_exists(&self, event_id: &EventId) -> Result {
let pduid = self.get_pdu_id(event_id).await?; let pduid = self.get_pdu_id(event_id).await?;
@@ -143,6 +159,7 @@ impl Data {
/// Returns the pdu. /// Returns the pdu.
/// ///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. /// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
#[inline]
pub(super) async fn get_outlier_pdu(&self, event_id: &EventId) -> Result<PduEvent> { pub(super) async fn get_outlier_pdu(&self, event_id: &EventId) -> Result<PduEvent> {
self.eventid_outlierpdu self.eventid_outlierpdu
.get(event_id) .get(event_id)
@@ -153,11 +170,15 @@ impl Data {
/// Returns the pdu. /// Returns the pdu.
/// ///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. /// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
#[inline]
pub(super) async fn get_pdu(&self, event_id: &EventId) -> Result<PduEvent> { pub(super) async fn get_pdu(&self, event_id: &EventId) -> Result<PduEvent> {
let accepted = self.get_non_outlier_pdu(event_id).boxed(); let accepted = self.get_non_outlier_pdu(event_id);
let outlier = self.get_outlier_pdu(event_id).boxed(); let outlier = self.get_outlier_pdu(event_id);
select_ok([accepted, outlier]).await.map(at!(0)) pin_mut!(accepted, outlier);
select_ok([Left(accepted), Right(outlier)])
.await
.map(at!(0))
} }
/// Like get_non_outlier_pdu(), but without the expense of fetching and /// Like get_non_outlier_pdu(), but without the expense of fetching and
@@ -168,11 +189,13 @@ impl Data {
} }
/// Like get_pdu(), but without the expense of fetching and parsing the data /// Like get_pdu(), but without the expense of fetching and parsing the data
#[inline]
pub(super) async fn pdu_exists(&self, event_id: &EventId) -> Result { pub(super) async fn pdu_exists(&self, event_id: &EventId) -> Result {
let non_outlier = self.non_outlier_pdu_exists(event_id).boxed(); let non_outlier = self.non_outlier_pdu_exists(event_id);
let outlier = self.outlier_pdu_exists(event_id).boxed(); let outlier = self.outlier_pdu_exists(event_id);
select_ok([non_outlier, outlier]) pin_mut!(non_outlier, outlier);
select_ok([Left(non_outlier), Right(outlier)])
.await .await
.map(at!(0)) .map(at!(0))
} }
@@ -180,11 +203,13 @@ impl Data {
/// Returns the pdu. /// Returns the pdu.
/// ///
/// This does __NOT__ check the outliers `Tree`. /// This does __NOT__ check the outliers `Tree`.
#[inline]
pub(super) async fn get_pdu_from_id(&self, pdu_id: &RawPduId) -> Result<PduEvent> { pub(super) async fn get_pdu_from_id(&self, pdu_id: &RawPduId) -> Result<PduEvent> {
self.pduid_pdu.get(pdu_id).await.deserialized() self.pduid_pdu.get(pdu_id).await.deserialized()
} }
/// Returns the pdu as a `BTreeMap<String, CanonicalJsonValue>`. /// Returns the pdu as a `BTreeMap<String, CanonicalJsonValue>`.
#[inline]
pub(super) async fn get_pdu_json_from_id( pub(super) async fn get_pdu_json_from_id(
&self, &self,
pdu_id: &RawPduId, pdu_id: &RawPduId,
@@ -237,6 +262,7 @@ impl Data {
/// Returns an iterator over all events and their tokens in a room that /// Returns an iterator over all events and their tokens in a room that
/// happened before the event with id `until` in reverse-chronological /// happened before the event with id `until` in reverse-chronological
/// order. /// order.
#[inline]
pub(super) fn pdus_rev<'a>( pub(super) fn pdus_rev<'a>(
&'a self, &'a self,
user_id: Option<&'a UserId>, user_id: Option<&'a UserId>,
@@ -254,6 +280,7 @@ impl Data {
.try_flatten_stream() .try_flatten_stream()
} }
#[inline]
pub(super) fn pdus<'a>( pub(super) fn pdus<'a>(
&'a self, &'a self,
user_id: Option<&'a UserId>, user_id: Option<&'a UserId>,

View File

@@ -173,7 +173,6 @@ impl Service {
} }
/// Returns the json of a pdu. /// Returns the json of a pdu.
#[inline]
pub async fn get_non_outlier_pdu_json( pub async fn get_non_outlier_pdu_json(
&self, &self,
event_id: &EventId, event_id: &EventId,
@@ -182,7 +181,6 @@ impl Service {
} }
/// Returns the pdu's id. /// Returns the pdu's id.
#[inline]
pub async fn get_pdu_id(&self, event_id: &EventId) -> Result<RawPduId> { pub async fn get_pdu_id(&self, event_id: &EventId) -> Result<RawPduId> {
self.db.get_pdu_id(event_id).await self.db.get_pdu_id(event_id).await
} }
@@ -190,7 +188,6 @@ impl Service {
/// Returns the pdu. /// Returns the pdu.
/// ///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. /// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
#[inline]
pub async fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<impl Event> { pub async fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<impl Event> {
self.db.get_non_outlier_pdu(event_id).await self.db.get_non_outlier_pdu(event_id).await
} }
@@ -198,7 +195,6 @@ impl Service {
/// Returns the pdu. /// Returns the pdu.
/// ///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. /// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
#[inline]
pub async fn get_pdu(&self, event_id: &EventId) -> Result<PduEvent> { pub async fn get_pdu(&self, event_id: &EventId) -> Result<PduEvent> {
self.db.get_pdu(event_id).await self.db.get_pdu(event_id).await
} }
@@ -206,13 +202,11 @@ impl Service {
/// Returns the pdu. /// Returns the pdu.
/// ///
/// This does __NOT__ check the outliers `Tree`. /// This does __NOT__ check the outliers `Tree`.
#[inline]
pub async fn get_pdu_from_id(&self, pdu_id: &RawPduId) -> Result<PduEvent> { pub async fn get_pdu_from_id(&self, pdu_id: &RawPduId) -> Result<PduEvent> {
self.db.get_pdu_from_id(pdu_id).await self.db.get_pdu_from_id(pdu_id).await
} }
/// Returns the pdu as a `BTreeMap<String, CanonicalJsonValue>`. /// Returns the pdu as a `BTreeMap<String, CanonicalJsonValue>`.
#[inline]
pub async fn get_pdu_json_from_id(&self, pdu_id: &RawPduId) -> Result<CanonicalJsonObject> { pub async fn get_pdu_json_from_id(&self, pdu_id: &RawPduId) -> Result<CanonicalJsonObject> {
self.db.get_pdu_json_from_id(pdu_id).await self.db.get_pdu_json_from_id(pdu_id).await
} }
@@ -220,7 +214,6 @@ impl Service {
/// Checks if pdu exists /// Checks if pdu exists
/// ///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. /// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
#[inline]
pub fn pdu_exists<'a>( pub fn pdu_exists<'a>(
&'a self, &'a self,
event_id: &'a EventId, event_id: &'a EventId,
@@ -236,7 +229,6 @@ impl Service {
/// Returns an iterator over all PDUs in a room. Unknown rooms produce no /// Returns an iterator over all PDUs in a room. Unknown rooms produce no
/// items. /// items.
#[inline]
pub fn all_pdus<'a>( pub fn all_pdus<'a>(
&'a self, &'a self,
user_id: &'a UserId, user_id: &'a UserId,