1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use std:: cmp:: Ordering ;
15+ use std:: { cmp:: Ordering , ops :: Not } ;
1616
1717use super :: { RoomListItem , Sorter } ;
1818
19- fn cmp < F > ( are_latest_events_locals : F , left : & RoomListItem , right : & RoomListItem ) -> Ordering
19+ fn cmp < F > (
20+ are_latest_events_not_remote_like : F ,
21+ left : & RoomListItem ,
22+ right : & RoomListItem ,
23+ ) -> Ordering
2024where
2125 F : Fn ( & RoomListItem , & RoomListItem ) -> ( bool , bool ) ,
2226{
2327 // We want local latest event to come first. When there is a remote latest event
2428 // or no latest event, we don't want to sort them.
2529 // NOTE: This is the same as a.cmp(b).reverse() for booleans.
26- match are_latest_events_locals ( left, right) {
30+ match are_latest_events_not_remote_like ( left, right) {
2731 // `false` and `false`, i.e.:
2832 // - `None` == `None`.
2933 // - `None` == `Remote`.
@@ -61,9 +65,12 @@ pub fn new_sorter() -> impl Sorter {
6165 // Be careful. This method is called **a lot** in the context of a sorter. Using
6266 // `Room::latest_event` would be dramatic as it returns a clone of the
6367 // `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 )
68+ // `Room::latest_event_is_remote_like`, where the value is cached in
69+ // `RoomListItem`.
70+ (
71+ left. cached_latest_event_is_remote_like . not ( ) ,
72+ right. cached_latest_event_is_remote_like . not ( ) ,
73+ )
6774 } ;
6875
6976 move |left, right| -> Ordering { cmp ( latest_events, left, right) }
@@ -119,6 +126,16 @@ mod tests {
119126 } )
120127 }
121128
129+ fn local_has_been_sent ( ) -> LatestEventValue {
130+ LatestEventValue :: LocalHasBeenSent ( LocalLatestEventValue {
131+ timestamp : MilliSecondsSinceUnixEpoch ( uint ! ( 42 ) ) ,
132+ content : SerializableEventContent :: new ( & AnyMessageLikeEventContent :: RoomMessage (
133+ RoomMessageEventContent :: text_plain ( "raclette" ) ,
134+ ) )
135+ . unwrap ( ) ,
136+ } )
137+ }
138+
122139 fn local_cannot_be_sent ( ) -> LatestEventValue {
123140 LatestEventValue :: LocalCannotBeSent ( LocalLatestEventValue {
124141 timestamp : MilliSecondsSinceUnixEpoch ( uint ! ( 42 ) ) ,
@@ -129,6 +146,10 @@ mod tests {
129146 } )
130147 }
131148
149+ fn pair ( left : LatestEventValue , right : LatestEventValue ) -> ( bool , bool ) {
150+ ( left. is_remote_like ( ) . not ( ) , right. is_remote_like ( ) . not ( ) )
151+ }
152+
132153 #[ async_test]
133154 async fn test_none_or_remote_and_none_or_remote ( ) {
134155 let ( client, server) = logged_in_client_with_server ( ) . await ;
@@ -138,34 +159,22 @@ mod tests {
138159
139160 // `None` and `None`.
140161 {
141- assert_eq ! (
142- cmp( |_, _| ( none( ) . is_local( ) , none( ) . is_local( ) ) , & room_a, & room_b) ,
143- Ordering :: Equal
144- ) ;
162+ assert_eq ! ( cmp( |_, _| pair( none( ) , none( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
145163 }
146164
147165 // `None` and `Remote`.
148166 {
149- assert_eq ! (
150- cmp( |_, _| ( none( ) . is_local( ) , remote( ) . is_local( ) ) , & room_a, & room_b) ,
151- Ordering :: Equal
152- ) ;
167+ assert_eq ! ( cmp( |_, _| pair( none( ) , remote( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
153168 }
154169
155170 // `Remote` and `None`.
156171 {
157- assert_eq ! (
158- cmp( |_, _| ( remote( ) . is_local( ) , none( ) . is_local( ) ) , & room_a, & room_b) ,
159- Ordering :: Equal
160- ) ;
172+ assert_eq ! ( cmp( |_, _| pair( remote( ) , none( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
161173 }
162174
163175 // `Remote` and `None`.
164176 {
165- assert_eq ! (
166- cmp( |_, _| ( remote( ) . is_local( ) , remote( ) . is_local( ) ) , & room_a, & room_b) ,
167- Ordering :: Equal
168- ) ;
177+ assert_eq ! ( cmp( |_, _| pair( remote( ) , remote( ) ) , & room_a, & room_b) , Ordering :: Equal ) ;
169178 }
170179 }
171180
@@ -179,31 +188,31 @@ mod tests {
179188 // `None` and `Local*`.
180189 {
181190 assert_eq ! (
182- cmp( |_, _| ( none( ) . is_local ( ) , local_is_sending( ) . is_local ( ) ) , & room_a, & room_b) ,
191+ cmp( |_, _| pair ( none( ) , local_is_sending( ) ) , & room_a, & room_b) ,
183192 Ordering :: Greater
184193 ) ;
185194 assert_eq ! (
186- cmp(
187- |_ , _| ( none ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
188- & room_a ,
189- & room_b
190- ) ,
195+ cmp( |_ , _| pair ( none ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
196+ Ordering :: Equal
197+ ) ;
198+ assert_eq ! (
199+ cmp ( |_ , _| pair ( none ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
191200 Ordering :: Greater
192201 ) ;
193202 }
194203
195204 // `Remote` and `Local*`.
196205 {
197206 assert_eq ! (
198- cmp( |_, _| ( remote( ) . is_local ( ) , local_is_sending( ) . is_local ( ) ) , & room_a, & room_b) ,
207+ cmp( |_, _| pair ( remote( ) , local_is_sending( ) ) , & room_a, & room_b) ,
199208 Ordering :: Greater
200209 ) ;
201210 assert_eq ! (
202- cmp(
203- |_ , _| ( remote ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
204- & room_a ,
205- & room_b
206- ) ,
211+ cmp( |_ , _| pair ( remote ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
212+ Ordering :: Equal
213+ ) ;
214+ assert_eq ! (
215+ cmp ( |_ , _| pair ( remote ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
207216 Ordering :: Greater
208217 ) ;
209218 }
@@ -219,31 +228,31 @@ mod tests {
219228 // `Local*` and `None`.
220229 {
221230 assert_eq ! (
222- cmp( |_, _| ( local_is_sending( ) . is_local ( ) , none( ) . is_local ( ) ) , & room_a, & room_b) ,
231+ cmp( |_, _| pair ( local_is_sending( ) , none( ) ) , & room_a, & room_b) ,
223232 Ordering :: Less
224233 ) ;
225234 assert_eq ! (
226- cmp(
227- |_ , _| ( local_cannot_be_sent ( ) . is_local ( ) , none ( ) . is_local ( ) ) ,
228- & room_a ,
229- & room_b
230- ) ,
235+ cmp( |_ , _| pair ( local_has_been_sent ( ) , none ( ) ) , & room_a , & room_b ) ,
236+ Ordering :: Equal
237+ ) ;
238+ assert_eq ! (
239+ cmp ( |_ , _| pair ( local_cannot_be_sent ( ) , none ( ) ) , & room_a , & room_b ) ,
231240 Ordering :: Less
232241 ) ;
233242 }
234243
235244 // `Local*` and `Remote`.
236245 {
237246 assert_eq ! (
238- cmp( |_, _| ( local_is_sending( ) . is_local ( ) , remote( ) . is_local ( ) ) , & room_a, & room_b) ,
247+ cmp( |_, _| pair ( local_is_sending( ) , remote( ) ) , & room_a, & room_b) ,
239248 Ordering :: Less
240249 ) ;
241250 assert_eq ! (
242- cmp(
243- |_ , _| ( local_cannot_be_sent ( ) . is_local ( ) , remote ( ) . is_local ( ) ) ,
244- & room_a ,
245- & room_b
246- ) ,
251+ cmp( |_ , _| pair ( local_has_been_sent ( ) , remote ( ) ) , & room_a , & room_b ) ,
252+ Ordering :: Equal
253+ ) ;
254+ assert_eq ! (
255+ cmp ( |_ , _| pair ( local_cannot_be_sent ( ) , remote ( ) ) , & room_a , & room_b ) ,
247256 Ordering :: Less
248257 ) ;
249258 }
@@ -259,35 +268,41 @@ mod tests {
259268 // `Local*` and `Local*`.
260269 {
261270 assert_eq ! (
262- cmp(
263- |_, _| ( local_is_sending( ) . is_local( ) , local_is_sending( ) . is_local( ) ) ,
264- & room_a,
265- & room_b
266- ) ,
271+ cmp( |_, _| pair( local_is_sending( ) , local_is_sending( ) ) , & room_a, & room_b) ,
267272 Ordering :: Equal
268273 ) ;
269274 assert_eq ! (
270- cmp(
271- |_ , _| ( local_is_sending ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
272- & room_a ,
273- & room_b
274- ) ,
275+ cmp( |_ , _| pair ( local_is_sending ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
276+ Ordering :: Less
277+ ) ;
278+ assert_eq ! (
279+ cmp ( |_ , _| pair ( local_is_sending ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
275280 Ordering :: Equal
276281 ) ;
282+
277283 assert_eq ! (
278- cmp(
279- |_, _| ( local_cannot_be_sent( ) . is_local( ) , local_is_sending( ) . is_local( ) ) ,
280- & room_a,
281- & room_b
282- ) ,
284+ cmp( |_, _| pair( local_has_been_sent( ) , local_is_sending( ) ) , & room_a, & room_b) ,
285+ Ordering :: Greater
286+ ) ;
287+ assert_eq ! (
288+ cmp( |_, _| pair( local_has_been_sent( ) , local_has_been_sent( ) ) , & room_a, & room_b) ,
289+ Ordering :: Equal
290+ ) ;
291+ assert_eq ! (
292+ cmp( |_, _| pair( local_has_been_sent( ) , local_cannot_be_sent( ) ) , & room_a, & room_b) ,
293+ Ordering :: Greater
294+ ) ;
295+
296+ assert_eq ! (
297+ cmp( |_, _| pair( local_cannot_be_sent( ) , local_is_sending( ) ) , & room_a, & room_b) ,
283298 Ordering :: Equal
284299 ) ;
285300 assert_eq ! (
286- cmp(
287- |_ , _| ( local_cannot_be_sent ( ) . is_local ( ) , local_cannot_be_sent ( ) . is_local ( ) ) ,
288- & room_a ,
289- & room_b
290- ) ,
301+ cmp( |_ , _| pair ( local_cannot_be_sent ( ) , local_has_been_sent ( ) ) , & room_a , & room_b ) ,
302+ Ordering :: Less
303+ ) ;
304+ assert_eq ! (
305+ cmp ( |_ , _| pair ( local_cannot_be_sent ( ) , local_cannot_be_sent ( ) ) , & room_a , & room_b ) ,
291306 Ordering :: Equal
292307 ) ;
293308 }
0 commit comments