14
14
15
15
//! Facilities to edit existing events.
16
16
17
+ use std:: future:: Future ;
18
+
17
19
use matrix_sdk_base:: deserialized_responses:: SyncTimelineEvent ;
18
20
use ruma:: {
19
21
events:: {
@@ -88,34 +90,42 @@ impl Room {
88
90
}
89
91
}
90
92
91
- #[ cfg_attr( target_arch = "wasm32" , async_trait:: async_trait( ?Send ) ) ]
92
- #[ cfg_attr( not( target_arch = "wasm32" ) , async_trait:: async_trait) ]
93
93
trait EventSource {
94
- async fn get_event ( & self , event_id : & EventId ) -> Result < SyncTimelineEvent , EditError > ;
94
+ fn get_event (
95
+ & self ,
96
+ event_id : & EventId ,
97
+ ) -> impl Future < Output = Result < SyncTimelineEvent , EditError > > ;
95
98
}
96
99
97
- #[ cfg_attr( target_arch = "wasm32" , async_trait:: async_trait( ?Send ) ) ]
98
- #[ cfg_attr( not( target_arch = "wasm32" ) , async_trait:: async_trait) ]
99
100
impl < ' a > EventSource for & ' a Room {
100
- async fn get_event ( & self , event_id : & EventId ) -> Result < SyncTimelineEvent , EditError > {
101
- match self . event_cache ( ) . await {
102
- Ok ( ( event_cache, _drop_handles) ) => {
103
- if let Some ( event) = event_cache. event ( event_id) . await {
104
- return Ok ( event) ;
101
+ // This is written as a function returning a Future to avoid a performance
102
+ // pitfall of rustc when using async_trait. See https://github.com/matrix-org/matrix-rust-sdk/pull/3880 for
103
+ // details.
104
+ #[ allow( clippy:: manual_async_fn) ]
105
+ fn get_event (
106
+ & self ,
107
+ event_id : & EventId ,
108
+ ) -> impl Future < Output = Result < SyncTimelineEvent , EditError > > {
109
+ async {
110
+ match self . event_cache ( ) . await {
111
+ Ok ( ( event_cache, _drop_handles) ) => {
112
+ if let Some ( event) = event_cache. event ( event_id) . await {
113
+ return Ok ( event) ;
114
+ }
115
+ // Fallthrough: try with /event.
105
116
}
106
- // Fallthrough: try with /event.
107
- }
108
117
109
- Err ( err) => {
110
- debug ! ( "error when getting the event cache: {err}" ) ;
118
+ Err ( err) => {
119
+ debug ! ( "error when getting the event cache: {err}" ) ;
120
+ }
111
121
}
112
- }
113
122
114
- trace ! ( "trying with /event now" ) ;
115
- self . event ( event_id, None )
116
- . await
117
- . map ( Into :: into)
118
- . map_err ( |err| EditError :: Fetch ( Box :: new ( err) ) )
123
+ trace ! ( "trying with /event now" ) ;
124
+ self . event ( event_id, None )
125
+ . await
126
+ . map ( Into :: into)
127
+ . map_err ( |err| EditError :: Fetch ( Box :: new ( err) ) )
128
+ }
119
129
}
120
130
}
121
131
@@ -200,7 +210,10 @@ async fn make_edit_event<S: EventSource>(
200
210
201
211
#[ cfg( test) ]
202
212
mod tests {
203
- use std:: collections:: BTreeMap ;
213
+ use std:: {
214
+ collections:: BTreeMap ,
215
+ future:: { ready, Future } ,
216
+ } ;
204
217
205
218
use assert_matches2:: { assert_let, assert_matches} ;
206
219
use matrix_sdk_base:: deserialized_responses:: SyncTimelineEvent ;
@@ -225,11 +238,12 @@ mod tests {
225
238
events : BTreeMap < OwnedEventId , SyncTimelineEvent > ,
226
239
}
227
240
228
- #[ cfg_attr( target_arch = "wasm32" , async_trait:: async_trait( ?Send ) ) ]
229
- #[ cfg_attr( not( target_arch = "wasm32" ) , async_trait:: async_trait) ]
230
241
impl EventSource for TestEventCache {
231
- async fn get_event ( & self , event_id : & EventId ) -> Result < SyncTimelineEvent , EditError > {
232
- Ok ( self . events . get ( event_id) . unwrap ( ) . clone ( ) )
242
+ fn get_event (
243
+ & self ,
244
+ event_id : & EventId ,
245
+ ) -> impl Future < Output = Result < SyncTimelineEvent , EditError > > {
246
+ ready ( Ok ( self . events . get ( event_id) . unwrap ( ) . clone ( ) ) )
233
247
}
234
248
}
235
249
0 commit comments