@@ -16,14 +16,15 @@ use std::cmp::Ordering;
1616
1717use super :: { RoomListItem , Sorter } ;
1818
19- fn cmp < F > ( are_latest_events_locals : F , left : & RoomListItem , right : & RoomListItem ) -> Ordering
19+ fn cmp < F > ( are_latest_events_unsent : F , left : & RoomListItem , right : & RoomListItem ) -> Ordering
2020where
2121 F : Fn ( & RoomListItem , & RoomListItem ) -> ( bool , bool ) ,
2222{
23- // We want local latest event to come first. When there is a remote latest event
24- // or no latest event, we don't want to sort them.
23+ // We want latest events representing unsent events to come firsts. When
24+ // there is a remote latest event or no latest event, we don't want to sort
25+ // them.
2526 // NOTE: This is the same as a.cmp(b).reverse() for booleans.
26- match are_latest_events_locals ( left, right) {
27+ match are_latest_events_unsent ( left, right) {
2728 // `false` and `false`, i.e.:
2829 // - `None` == `None`.
2930 // - `None` == `Remote`.
@@ -48,22 +49,15 @@ where
4849}
4950
5051/// Create a new sorter that will sort two [`RoomListItem`] by their latest
51- /// events' state: latest events representing a local event
52- /// ([`LatestEventValue::LocalIsSending`] or
53- /// [`LatestEventValue::LocalCannotBeSent`]) come first, and latest event
54- /// representing a remote event ([`LatestEventValue::Remote`]) come last.
55- ///
56- /// [`LatestEventValue::LocalIsSending`]: matrix_sdk_base::latest_event::LatestEventValue::LocalIsSending
57- /// [`LatestEventValue::LocalCannotBeSent`]: matrix_sdk_base::latest_event::LatestEventValue::LocalCannotBeSent
58- /// [`LatestEventValue::Remote`]: matrix_sdk_base::latest_event::LatestEventValue::Remote
52+ /// events' state: latest events representing unsent events come firsts.
5953pub fn new_sorter ( ) -> impl Sorter {
6054 let latest_events = |left : & RoomListItem , right : & RoomListItem | {
61- // Be careful. This method is called **a lot** in the context of a sorter. Using
62- // `Room::latest_event` would be dramatic as it returns a clone of the
63- // `LatestEventValue`. It's better to use the more specific method
64- // `Room::latest_event_is_local `, where the value is cached in this module's
65- // `Room` type .
66- ( left. cached_latest_event_is_local , right. cached_latest_event_is_local )
55+ // Be careful. This method is called **a lot** in the context of a
56+ // sorter. Using `Room::latest_event` would be dramatic as it returns a
57+ // clone of the `LatestEventValue`. It's better to use the more specific
58+ // method `Room::latest_event_is_unsent `, where the value is cached
59+ // in `RoomListItem` .
60+ ( left. cached_latest_event_is_unsent , right. cached_latest_event_is_unsent )
6761 } ;
6862
6963 move |left, right| -> Ordering { cmp ( latest_events, left, right) }
@@ -119,6 +113,16 @@ mod tests {
119113 } )
120114 }
121115
116+ fn local_has_been_sent ( ) -> LatestEventValue {
117+ LatestEventValue :: LocalHasBeenSent ( LocalLatestEventValue {
118+ timestamp : MilliSecondsSinceUnixEpoch ( uint ! ( 42 ) ) ,
119+ content : SerializableEventContent :: new ( & AnyMessageLikeEventContent :: RoomMessage (
120+ RoomMessageEventContent :: text_plain ( "raclette" ) ,
121+ ) )
122+ . unwrap ( ) ,
123+ } )
124+ }
125+
122126 fn local_cannot_be_sent ( ) -> LatestEventValue {
123127 LatestEventValue :: LocalCannotBeSent ( LocalLatestEventValue {
124128 timestamp : MilliSecondsSinceUnixEpoch ( uint ! ( 42 ) ) ,
@@ -129,6 +133,10 @@ mod tests {
129133 } )
130134 }
131135
136+ fn pair ( left : LatestEventValue , right : LatestEventValue ) -> ( bool , bool ) {
137+ ( left. is_unsent ( ) , right. is_unsent ( ) )
138+ }
139+
132140 #[ async_test]
133141 async fn test_none_or_remote_and_none_or_remote ( ) {
134142 let ( client, server) = logged_in_client_with_server ( ) . await ;
@@ -138,34 +146,22 @@ mod tests {
138146
139147 // `None` and `None`.
140148 {
141- assert_eq ! (
142- cmp( |_, _| ( none( ) . is_local( ) , none( ) . is_local( ) ) , & room_a, & room_b) ,
143- Ordering :: Equal
144- ) ;
149+ assert_eq ! ( cmp( |_, _| pair( none( ) , none( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
145150 }
146151
147152 // `None` and `Remote`.
148153 {
149- assert_eq ! (
150- cmp( |_, _| ( none( ) . is_local( ) , remote( ) . is_local( ) ) , & room_a, & room_b) ,
151- Ordering :: Equal
152- ) ;
154+ assert_eq ! ( cmp( |_, _| pair( none( ) , remote( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
153155 }
154156
155157 // `Remote` and `None`.
156158 {
157- assert_eq ! (
158- cmp( |_, _| ( remote( ) . is_local( ) , none( ) . is_local( ) ) , & room_a, & room_b) ,
159- Ordering :: Equal
160- ) ;
159+ assert_eq ! ( cmp( |_, _| pair( remote( ) , none( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
161160 }
162161
163162 // `Remote` and `None`.
164163 {
165- assert_eq ! (
166- cmp( |_, _| ( remote( ) . is_local( ) , remote( ) . is_local( ) ) , & room_a, & room_b) ,
167- Ordering :: Equal
168- ) ;
164+ assert_eq ! ( cmp( |_, _| pair( remote( ) , remote( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
169165 }
170166 }
171167
@@ -179,31 +175,31 @@ mod tests {
179175 // `None` and `Local*`.
180176 {
181177 assert_eq ! (
182- cmp( |_, _| ( none( ) . is_local ( ) , local_is_sending( ) . is_local ( ) ) , & room_a, & room_b) ,
178+ cmp( |_, _| pair ( none( ) , local_is_sending( ) ) , & room_a, & room_b) ,
183179 Ordering :: Greater
184180 ) ;
185181 assert_eq ! (
186- cmp(
187- |_ , _| ( none ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
188- & room_a ,
189- & room_b
190- ) ,
182+ cmp( |_ , _| pair ( none ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
183+ Ordering :: Equal
184+ ) ;
185+ assert_eq ! (
186+ cmp ( |_ , _| pair ( none ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
191187 Ordering :: Greater
192188 ) ;
193189 }
194190
195191 // `Remote` and `Local*`.
196192 {
197193 assert_eq ! (
198- cmp( |_, _| ( remote( ) . is_local ( ) , local_is_sending( ) . is_local ( ) ) , & room_a, & room_b) ,
194+ cmp( |_, _| pair ( remote( ) , local_is_sending( ) ) , & room_a, & room_b) ,
199195 Ordering :: Greater
200196 ) ;
201197 assert_eq ! (
202- cmp(
203- |_ , _| ( remote ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
204- & room_a ,
205- & room_b
206- ) ,
198+ cmp( |_ , _| pair ( remote ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
199+ Ordering :: Equal
200+ ) ;
201+ assert_eq ! (
202+ cmp ( |_ , _| pair ( remote ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
207203 Ordering :: Greater
208204 ) ;
209205 }
@@ -219,31 +215,31 @@ mod tests {
219215 // `Local*` and `None`.
220216 {
221217 assert_eq ! (
222- cmp( |_, _| ( local_is_sending( ) . is_local ( ) , none( ) . is_local ( ) ) , & room_a, & room_b) ,
218+ cmp( |_, _| pair ( local_is_sending( ) , none( ) ) , & room_a, & room_b) ,
223219 Ordering :: Less
224220 ) ;
225221 assert_eq ! (
226- cmp(
227- |_ , _| ( local_cannot_be_sent ( ) . is_local ( ) , none ( ) . is_local ( ) ) ,
228- & room_a ,
229- & room_b
230- ) ,
222+ cmp( |_ , _| pair ( local_has_been_sent ( ) , none ( ) ) , & room_a , & room_b ) ,
223+ Ordering :: Equal
224+ ) ;
225+ assert_eq ! (
226+ cmp ( |_ , _| pair ( local_cannot_be_sent ( ) , none ( ) ) , & room_a , & room_b ) ,
231227 Ordering :: Less
232228 ) ;
233229 }
234230
235231 // `Local*` and `Remote`.
236232 {
237233 assert_eq ! (
238- cmp( |_, _| ( local_is_sending( ) . is_local ( ) , remote( ) . is_local ( ) ) , & room_a, & room_b) ,
234+ cmp( |_, _| pair ( local_is_sending( ) , remote( ) ) , & room_a, & room_b) ,
239235 Ordering :: Less
240236 ) ;
241237 assert_eq ! (
242- cmp(
243- |_ , _| ( local_cannot_be_sent ( ) . is_local ( ) , remote ( ) . is_local ( ) ) ,
244- & room_a ,
245- & room_b
246- ) ,
238+ cmp( |_ , _| pair ( local_has_been_sent ( ) , remote ( ) ) , & room_a , & room_b ) ,
239+ Ordering :: Equal
240+ ) ;
241+ assert_eq ! (
242+ cmp ( |_ , _| pair ( local_cannot_be_sent ( ) , remote ( ) ) , & room_a , & room_b ) ,
247243 Ordering :: Less
248244 ) ;
249245 }
@@ -259,35 +255,41 @@ mod tests {
259255 // `Local*` and `Local*`.
260256 {
261257 assert_eq ! (
262- cmp(
263- |_, _| ( local_is_sending( ) . is_local( ) , local_is_sending( ) . is_local( ) ) ,
264- & room_a,
265- & room_b
266- ) ,
258+ cmp( |_, _| pair( local_is_sending( ) , local_is_sending( ) ) , & room_a, & room_b) ,
267259 Ordering :: Equal
268260 ) ;
269261 assert_eq ! (
270- cmp(
271- |_ , _| ( local_is_sending ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
272- & room_a ,
273- & room_b
274- ) ,
262+ cmp( |_ , _| pair ( local_is_sending ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
263+ Ordering :: Less
264+ ) ;
265+ assert_eq ! (
266+ cmp ( |_ , _| pair ( local_is_sending ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
275267 Ordering :: Equal
276268 ) ;
269+
277270 assert_eq ! (
278- cmp(
279- |_, _| ( local_cannot_be_sent( ) . is_local( ) , local_is_sending( ) . is_local( ) ) ,
280- & room_a,
281- & room_b
282- ) ,
271+ cmp( |_, _| pair( local_has_been_sent( ) , local_is_sending( ) ) , & room_a, & room_b) ,
272+ Ordering :: Greater
273+ ) ;
274+ assert_eq ! (
275+ cmp( |_, _| pair( local_has_been_sent( ) , local_has_been_sent( ) ) , & room_a, & room_b) ,
276+ Ordering :: Equal
277+ ) ;
278+ assert_eq ! (
279+ cmp( |_, _| pair( local_has_been_sent( ) , local_cannot_be_sent( ) ) , & room_a, & room_b) ,
280+ Ordering :: Greater
281+ ) ;
282+
283+ assert_eq ! (
284+ cmp( |_, _| pair( local_cannot_be_sent( ) , local_is_sending( ) ) , & room_a, & room_b) ,
283285 Ordering :: Equal
284286 ) ;
285287 assert_eq ! (
286- cmp(
287- |_ , _| ( local_cannot_be_sent ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
288- & room_a ,
289- & room_b
290- ) ,
288+ cmp( |_ , _| pair ( local_cannot_be_sent ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
289+ Ordering :: Less
290+ ) ;
291+ assert_eq ! (
292+ cmp ( |_ , _| pair ( local_cannot_be_sent ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
291293 Ordering :: Equal
292294 ) ;
293295 }
0 commit comments