generalize return value wrapping to not require Arc

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-11-26 00:35:03 +00:00
parent 238523f177
commit f69c596f56
3 changed files with 18 additions and 7 deletions

View file

@ -1,10 +1,11 @@
use std::{fmt::Debug, mem::size_of_val, sync::Arc}; use std::{borrow::Borrow, fmt::Debug, mem::size_of_val, sync::Arc};
pub use conduit::pdu::{ShortEventId, ShortId, ShortRoomId}; pub use conduit::pdu::{ShortEventId, ShortId, ShortRoomId};
use conduit::{err, implement, utils, utils::stream::ReadyExt, Result}; use conduit::{err, implement, utils, utils::stream::ReadyExt, Result};
use database::{Deserialized, Map}; use database::{Deserialized, Map};
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use ruma::{events::StateEventType, EventId, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
use serde::Deserialize;
use crate::{globals, Dep}; use crate::{globals, Dep};
@ -136,7 +137,11 @@ pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &s
} }
#[implement(Service)] #[implement(Service)]
pub async fn get_eventid_from_short(&self, shorteventid: ShortEventId) -> Result<Arc<EventId>> { pub async fn get_eventid_from_short<Id>(&self, shorteventid: ShortEventId) -> Result<Id>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
const BUFSIZE: usize = size_of::<ShortEventId>(); const BUFSIZE: usize = size_of::<ShortEventId>();
self.db self.db
@ -148,8 +153,10 @@ pub async fn get_eventid_from_short(&self, shorteventid: ShortEventId) -> Result
} }
#[implement(Service)] #[implement(Service)]
pub async fn multi_get_eventid_from_short<I>(&self, shorteventid: I) -> Vec<Result<Arc<EventId>>> pub async fn multi_get_eventid_from_short<Id, I>(&self, shorteventid: I) -> Vec<Result<Id>>
where where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
I: Iterator<Item = ShortEventId> + Send, I: Iterator<Item = ShortEventId> + Send,
{ {
const BUFSIZE: usize = size_of::<ShortEventId>(); const BUFSIZE: usize = size_of::<ShortEventId>();

View file

@ -102,7 +102,11 @@ impl Service {
.iter() .iter()
.stream() .stream()
.map(|&new| parse_compressed_state_event(new).1) .map(|&new| parse_compressed_state_event(new).1)
.then(|shorteventid| self.services.short.get_eventid_from_short(shorteventid)) .then(|shorteventid| {
self.services
.short
.get_eventid_from_short::<Box<_>>(shorteventid)
})
.ignore_err(); .ignore_err();
pin_mut!(event_ids); pin_mut!(event_ids);
@ -433,7 +437,7 @@ impl Service {
.await .await
.into_iter() .into_iter()
.stream() .stream()
.and_then(|event_id| async move { self.services.timeline.get_pdu(&event_id).await }) .and_then(|event_id: OwnedEventId| async move { self.services.timeline.get_pdu(&event_id).await })
.collect() .collect()
.await; .await;

View file

@ -7,7 +7,7 @@ use conduit::{
}; };
use database::{Deserialized, Map}; use database::{Deserialized, Map};
use futures::{StreamExt, TryFutureExt}; use futures::{StreamExt, TryFutureExt};
use ruma::{events::StateEventType, EventId, RoomId}; use ruma::{events::StateEventType, EventId, OwnedEventId, RoomId};
use crate::{ use crate::{
rooms, rooms,
@ -74,7 +74,7 @@ impl Data {
.into_iter() .into_iter()
.stream() .stream()
.ready_filter_map(Result::ok) .ready_filter_map(Result::ok)
.filter_map(|event_id| async move { self.services.timeline.get_pdu(&event_id).await.ok() }) .filter_map(|event_id: OwnedEventId| async move { self.services.timeline.get_pdu(&event_id).await.ok() })
.collect() .collect()
.await; .await;