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,38 @@ 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
+ fn get_event (
102
+ & self ,
103
+ event_id : & EventId ,
104
+ ) -> impl Future < Output = Result < SyncTimelineEvent , EditError > > {
105
+ async {
106
+ match self . event_cache ( ) . await {
107
+ Ok ( ( event_cache, _drop_handles) ) => {
108
+ if let Some ( event) = event_cache. event ( event_id) . await {
109
+ return Ok ( event) ;
110
+ }
111
+ // Fallthrough: try with /event.
105
112
}
106
- // Fallthrough: try with /event.
107
- }
108
113
109
- Err ( err) => {
110
- debug ! ( "error when getting the event cache: {err}" ) ;
114
+ Err ( err) => {
115
+ debug ! ( "error when getting the event cache: {err}" ) ;
116
+ }
111
117
}
112
- }
113
118
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) ) )
119
+ trace ! ( "trying with /event now" ) ;
120
+ self . event ( event_id, None )
121
+ . await
122
+ . map ( Into :: into)
123
+ . map_err ( |err| EditError :: Fetch ( Box :: new ( err) ) )
124
+ }
119
125
}
120
126
}
121
127
@@ -200,7 +206,10 @@ async fn make_edit_event<S: EventSource>(
200
206
201
207
#[ cfg( test) ]
202
208
mod tests {
203
- use std:: collections:: BTreeMap ;
209
+ use std:: {
210
+ collections:: BTreeMap ,
211
+ future:: { ready, Future } ,
212
+ } ;
204
213
205
214
use assert_matches2:: { assert_let, assert_matches} ;
206
215
use matrix_sdk_base:: deserialized_responses:: SyncTimelineEvent ;
@@ -225,11 +234,12 @@ mod tests {
225
234
events : BTreeMap < OwnedEventId , SyncTimelineEvent > ,
226
235
}
227
236
228
- #[ cfg_attr( target_arch = "wasm32" , async_trait:: async_trait( ?Send ) ) ]
229
- #[ cfg_attr( not( target_arch = "wasm32" ) , async_trait:: async_trait) ]
230
237
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 ( ) )
238
+ fn get_event (
239
+ & self ,
240
+ event_id : & EventId ,
241
+ ) -> impl Future < Output = Result < SyncTimelineEvent , EditError > > {
242
+ ready ( Ok ( self . events . get ( event_id) . unwrap ( ) . clone ( ) ) )
233
243
}
234
244
}
235
245
0 commit comments