@@ -248,6 +248,80 @@ impl Ordering {
248
248
Greater => Less ,
249
249
}
250
250
}
251
+
252
+ /// Chains two orderings.
253
+ ///
254
+ /// Returns `self` when it's not `Equal`. Otherwise returns `other`.
255
+ /// # Examples
256
+ ///
257
+ /// ```
258
+ /// #![feature(ordering_chaining)]
259
+ ///
260
+ /// use std::cmp::Ordering;
261
+ ///
262
+ /// let result = Ordering::Equal.then(Ordering::Less);
263
+ /// assert_eq!(result, Ordering::Less);
264
+ ///
265
+ /// let result = Ordering::Less.then(Ordering::Equal);
266
+ /// assert_eq!(result, Ordering::Less);
267
+ ///
268
+ /// let result = Ordering::Less.then(Ordering::Greater);
269
+ /// assert_eq!(result, Ordering::Less);
270
+ ///
271
+ /// let result = Ordering::Equal.then(Ordering::Equal);
272
+ /// assert_eq!(result, Ordering::Equal);
273
+ ///
274
+ /// let x: (i64, i64, i64) = (1, 2, 7);
275
+ /// let y: (i64, i64, i64) = (1, 5, 3);
276
+ /// let result = x.0.cmp(&y.0).then(x.1.cmp(&y.1)).then(x.2.cmp(&y.2));
277
+ ///
278
+ /// assert_eq!(result, Ordering::Less);
279
+ /// ```
280
+ #[ unstable( feature = "ordering_chaining" , issue = "37053" ) ]
281
+ pub fn then ( self , other : Ordering ) -> Ordering {
282
+ match self {
283
+ Equal => other,
284
+ _ => self ,
285
+ }
286
+ }
287
+
288
+ /// Chains the ordering with the given function.
289
+ ///
290
+ /// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
291
+ /// the result.
292
+ ///
293
+ /// # Examples
294
+ ///
295
+ /// ```
296
+ /// #![feature(ordering_chaining)]
297
+ ///
298
+ /// use std::cmp::Ordering;
299
+ ///
300
+ /// let result = Ordering::Equal.then_with(|| Ordering::Less);
301
+ /// assert_eq!(result, Ordering::Less);
302
+ ///
303
+ /// let result = Ordering::Less.then_with(|| Ordering::Equal);
304
+ /// assert_eq!(result, Ordering::Less);
305
+ ///
306
+ /// let result = Ordering::Less.then_with(|| Ordering::Greater);
307
+ /// assert_eq!(result, Ordering::Less);
308
+ ///
309
+ /// let result = Ordering::Equal.then_with(|| Ordering::Equal);
310
+ /// assert_eq!(result, Ordering::Equal);
311
+ ///
312
+ /// let x: (i64, i64, i64) = (1, 2, 7);
313
+ /// let y: (i64, i64, i64) = (1, 5, 3);
314
+ /// let result = x.0.cmp(&y.0).then_with(|| x.1.cmp(&y.1)).then_with(|| x.2.cmp(&y.2));
315
+ ///
316
+ /// assert_eq!(result, Ordering::Less);
317
+ /// ```
318
+ #[ unstable( feature = "ordering_chaining" , issue = "37053" ) ]
319
+ pub fn then_with < F : FnOnce ( ) -> Ordering > ( self , f : F ) -> Ordering {
320
+ match self {
321
+ Equal => f ( ) ,
322
+ _ => self ,
323
+ }
324
+ }
251
325
}
252
326
253
327
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
0 commit comments