8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- /*!
12
-
13
- Defines the `Ord` and `Eq` comparison traits.
14
-
15
- This module defines both `Ord` and `Eq` traits which are used by the compiler
16
- to implement comparison operators.
17
- Rust programs may implement `Ord` to overload the `<`, `<=`, `>`, and `>=` operators,
18
- and may implement `Eq` to overload the `==` and `!=` operators.
19
-
20
- For example, to define a type with a customized definition for the Eq operators,
21
- you could do the following:
22
-
23
- ```rust
24
- // Our type.
25
- struct SketchyNum {
26
- num : int
27
- }
28
-
29
- // Our implementation of `Eq` to support `==` and `!=`.
30
- impl Eq for SketchyNum {
31
- // Our custom eq allows numbers which are near eachother to be equal! :D
32
- fn eq(&self, other: &SketchyNum) -> bool {
33
- (self.num - other.num).abs() < 5
34
- }
35
- }
36
-
37
- // Now these binary operators will work when applied!
38
- assert!(SketchyNum {num: 37} == SketchyNum {num: 34});
39
- assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
40
- ```
41
-
42
- */
43
-
44
- /**
45
- * Trait for values that can be compared for equality and inequality.
46
- *
47
- * This trait allows partial equality, where types can be unordered instead of strictly equal or
48
- * unequal. For example, with the built-in floating-point types `a == b` and `a != b` will both
49
- * evaluate to false if either `a` or `b` is NaN (cf. IEEE 754-2008 section 5.11).
50
- *
51
- * Eq only requires the `eq` method to be implemented; `ne` is its negation by default.
52
- *
53
- * Eventually, this will be implemented by default for types that implement `TotalEq`.
54
- */
11
+ //! Defines the `Ord` and `Eq` comparison traits.
12
+ //!
13
+ //! This module defines both `Ord` and `Eq` traits which are used by the
14
+ //! compiler to implement comparison operators. Rust programs may implement
15
+ //!`Ord` to overload the `<`, `<=`, `>`, and `>=` operators, and may implement
16
+ //! `Eq` to overload the `==` and `!=` operators.
17
+ //!
18
+ //! For example, to define a type with a customized definition for the Eq
19
+ //! operators, you could do the following:
20
+ //!
21
+ //! ```rust
22
+ //! // Our type.
23
+ //! struct SketchyNum {
24
+ //! num : int
25
+ //! }
26
+ //!
27
+ //! // Our implementation of `Eq` to support `==` and `!=`.
28
+ //! impl Eq for SketchyNum {
29
+ //! // Our custom eq allows numbers which are near eachother to be equal! :D
30
+ //! fn eq(&self, other: &SketchyNum) -> bool {
31
+ //! (self.num - other.num).abs() < 5
32
+ //! }
33
+ //! }
34
+ //!
35
+ //! // Now these binary operators will work when applied!
36
+ //! assert!(SketchyNum {num: 37} == SketchyNum {num: 34});
37
+ //! assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
38
+ //! ```
39
+
40
+ /// Trait for values that can be compared for equality and inequality.
41
+ ///
42
+ /// This trait allows partial equality, where types can be unordered instead of
43
+ /// strictly equal or unequal. For example, with the built-in floating-point
44
+ /// types `a == b` and `a != b` will both evaluate to false if either `a` or
45
+ /// `b` is NaN (cf. IEEE 754-2008 section 5.11).
46
+ ///
47
+ /// Eq only requires the `eq` method to be implemented; `ne` is its negation by
48
+ /// default.
49
+ ///
50
+ /// Eventually, this will be implemented by default for types that implement
51
+ /// `TotalEq`.
55
52
#[ lang="eq" ]
56
53
pub trait Eq {
57
54
/// This method tests for `self` and `other` values to be equal, and is used by `==`.
@@ -62,7 +59,15 @@ pub trait Eq {
62
59
fn ne ( & self , other : & Self ) -> bool { !self . eq ( other) }
63
60
}
64
61
65
- /// Trait for equality comparisons where `a == b` and `a != b` are strict inverses.
62
+ /// Trait for equality comparisons which are [equivalence relations](
63
+ /// https://en.wikipedia.org/wiki/Equivalence_relation).
64
+ ///
65
+ /// This means, that in addition to `a == b` and `a != b` being strict
66
+ /// inverses, the equality must be (for all `a`, `b` and `c`):
67
+ ///
68
+ /// - reflexive: `a == a`;
69
+ /// - symmetric: `a == b` implies `b == a`; and
70
+ /// - transitive: `a == b` and `b == c` implies `a == c`.
66
71
pub trait TotalEq : Eq {
67
72
// FIXME #13101: this method is used solely by #[deriving] to
68
73
// assert that every component of a type implements #[deriving]
@@ -111,7 +116,15 @@ pub enum Ordering {
111
116
Greater = 1
112
117
}
113
118
114
- /// Trait for types that form a total order.
119
+ /// Trait for types that form a [total order](
120
+ /// https://en.wikipedia.org/wiki/Total_order).
121
+ ///
122
+ /// An order is a total order if it is (for all `a`, `b` and `c`):
123
+ ///
124
+ /// - total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is
125
+ /// true; and
126
+ /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for
127
+ /// both `==` and `>`.
115
128
pub trait TotalOrd : TotalEq + Ord {
116
129
/// This method returns an ordering between `self` and `other` values.
117
130
///
@@ -168,13 +181,11 @@ totalord_impl!(uint)
168
181
169
182
totalord_impl ! ( char )
170
183
171
- /**
172
- * Combine orderings, lexically.
173
- *
174
- * For example for a type `(int, int)`, two comparisons could be done.
175
- * If the first ordering is different, the first ordering is all that must be returned.
176
- * If the first ordering is equal, then second ordering is returned.
177
- */
184
+ /// Combine orderings, lexically.
185
+ ///
186
+ /// For example for a type `(int, int)`, two comparisons could be done.
187
+ /// If the first ordering is different, the first ordering is all that must be returned.
188
+ /// If the first ordering is equal, then second ordering is returned.
178
189
#[ inline]
179
190
pub fn lexical_ordering ( o1 : Ordering , o2 : Ordering ) -> Ordering {
180
191
match o1 {
@@ -183,16 +194,14 @@ pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
183
194
}
184
195
}
185
196
186
- /**
187
- * Trait for values that can be compared for a sort-order.
188
- *
189
- * Ord only requires implementation of the `lt` method,
190
- * with the others generated from default implementations.
191
- *
192
- * However it remains possible to implement the others separately,
193
- * for compatibility with floating-point NaN semantics
194
- * (cf. IEEE 754-2008 section 5.11).
195
- */
197
+ /// Trait for values that can be compared for a sort-order.
198
+ ///
199
+ /// Ord only requires implementation of the `lt` method,
200
+ /// with the others generated from default implementations.
201
+ ///
202
+ /// However it remains possible to implement the others separately,
203
+ /// for compatibility with floating-point NaN semantics
204
+ /// (cf. IEEE 754-2008 section 5.11).
196
205
#[ lang="ord" ]
197
206
pub trait Ord : Eq {
198
207
/// This method tests less than (for `self` and `other`) and is used by the `<` operator.
0 commit comments