|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// check that the derived impls for the comparison traits shortcircuit |
| 12 | +// where possible, by having a type that fails when compared as the |
| 13 | +// second element, so this passes iff the instances shortcircuit. |
| 14 | + |
| 15 | +pub struct FailCmp; |
| 16 | +impl Eq for FailCmp { |
| 17 | + fn eq(&self, _: &FailCmp) -> bool { fail!("eq") } |
| 18 | +} |
| 19 | + |
| 20 | +impl Ord for FailCmp { |
| 21 | + fn lt(&self, _: &FailCmp) -> bool { fail!("lt") } |
| 22 | +} |
| 23 | + |
| 24 | +impl TotalEq for FailCmp { |
| 25 | + fn equals(&self, _: &FailCmp) -> bool { fail!("equals") } |
| 26 | +} |
| 27 | + |
| 28 | +impl TotalOrd for FailCmp { |
| 29 | + fn cmp(&self, _: &FailCmp) -> Ordering { fail!("cmp") } |
| 30 | +} |
| 31 | + |
| 32 | +#[deriving(Eq,Ord,TotalEq,TotalOrd)] |
| 33 | +struct ShortCircuit { |
| 34 | + x: int, |
| 35 | + y: FailCmp |
| 36 | +} |
| 37 | + |
| 38 | +fn main() { |
| 39 | + let a = ShortCircuit { x: 1, y: FailCmp }; |
| 40 | + let b = ShortCircuit { x: 2, y: FailCmp }; |
| 41 | + |
| 42 | + assert!(a != b); |
| 43 | + assert!(a < b); |
| 44 | + assert!(!a.equals(&b)); |
| 45 | + assert_eq!(a.cmp(&b), ::std::cmp::Less); |
| 46 | +} |
0 commit comments