Skip to content

Commit 02c9c94

Browse files
committed
[std::cmp] add missing docs and provide an example
1 parent 7056f97 commit 02c9c94

File tree

1 file changed

+91
-17
lines changed

1 file changed

+91
-17
lines changed

src/libstd/cmp.rs

+91-17
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,36 @@
1010

1111
/*!
1212
13-
The `Ord` and `Eq` comparison traits
13+
Defines the `Ord` and `Eq` comparison traits.
1414
15-
This module contains the definition of both `Ord` and `Eq` which define
16-
the common interfaces for doing comparison. Both are language items
17-
that the compiler uses to implement the comparison operators. Rust code
18-
may implement `Ord` to overload the `<`, `<=`, `>`, and `>=` operators,
19-
and `Eq` to overload the `==` and `!=` operators.
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.
2019
21-
*/
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+
```
2241
23-
#![allow(missing_doc)]
42+
*/
2443

2544
/**
2645
* Trait for values that can be compared for equality and inequality.
@@ -35,8 +54,10 @@ and `Eq` to overload the `==` and `!=` operators.
3554
*/
3655
#[lang="eq"]
3756
pub trait Eq {
57+
/// This method tests for `self` and `other` values to be equal, and is used by `==`.
3858
fn eq(&self, other: &Self) -> bool;
3959

60+
/// This method tests for `!=`.
4061
#[inline]
4162
fn ne(&self, other: &Self) -> bool { !self.eq(other) }
4263
}
@@ -55,6 +76,7 @@ pub trait TotalEq: Eq {
5576
fn assert_receiver_is_total_eq(&self) {}
5677
}
5778

79+
/// A macro which defines an implementation of TotalEq for a given type.
5880
macro_rules! totaleq_impl(
5981
($t:ty) => {
6082
impl TotalEq for $t {}
@@ -78,11 +100,29 @@ totaleq_impl!(uint)
78100

79101
totaleq_impl!(char)
80102

103+
/// An ordering is, e.g, a result of a comparison between two values.
81104
#[deriving(Clone, Eq, Show)]
82-
pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
105+
pub enum Ordering {
106+
/// An ordering where a compared value is less [than another].
107+
Less = -1,
108+
/// An ordering where a compared value is equal [to another].
109+
Equal = 0,
110+
/// An ordering where a compared value is greater [than another].
111+
Greater = 1
112+
}
83113

84-
/// Trait for types that form a total order
114+
/// Trait for types that form a total order.
85115
pub trait TotalOrd: TotalEq + Ord {
116+
/// This method returns an ordering between `self` and `other` values.
117+
///
118+
/// By convention, `self.cmp(&other)` returns the ordering matching
119+
/// the expression `self <operator> other` if true. For example:
120+
///
121+
/// ```
122+
/// assert_eq!( 5u.cmp(&10), Less); // because 5 < 10
123+
/// assert_eq!(10u.cmp(&5), Greater); // because 10 > 5
124+
/// assert_eq!( 5u.cmp(&5), Equal); // because 5 == 5
125+
/// ```
86126
fn cmp(&self, other: &Self) -> Ordering;
87127
}
88128

@@ -99,6 +139,7 @@ impl Ord for Ordering {
99139
fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) }
100140
}
101141

142+
/// A macro which defines an implementation of TotalOrd for a given type.
102143
macro_rules! totalord_impl(
103144
($t:ty) => {
104145
impl TotalOrd for $t {
@@ -128,8 +169,11 @@ totalord_impl!(uint)
128169
totalord_impl!(char)
129170

130171
/**
131-
Return `o1` if it is not `Equal`, otherwise `o2`. Simulates the
132-
lexical ordering on a type `(int, int)`.
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.
133177
*/
134178
#[inline]
135179
pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
@@ -151,11 +195,18 @@ pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
151195
*/
152196
#[lang="ord"]
153197
pub trait Ord: Eq {
198+
/// This method tests less than (for `self` and `other`) and is used by the `<` operator.
154199
fn lt(&self, other: &Self) -> bool;
200+
201+
/// This method tests less than or equal to (`<=`).
155202
#[inline]
156203
fn le(&self, other: &Self) -> bool { !other.lt(self) }
204+
205+
/// This method tests greater than (`>`).
157206
#[inline]
158207
fn gt(&self, other: &Self) -> bool { other.lt(self) }
208+
209+
/// This method tests greater than or equal to (`>=`).
159210
#[inline]
160211
fn ge(&self, other: &Self) -> bool { !self.lt(other) }
161212
}
@@ -165,14 +216,17 @@ pub trait Ord: Eq {
165216
/// container types; e.g. it is often desirable to be able to use `&str`
166217
/// values to look up entries in a container with `~str` keys.
167218
pub trait Equiv<T> {
219+
/// Implement this function to decide equivalent values.
168220
fn equiv(&self, other: &T) -> bool;
169221
}
170222

223+
/// Compare and return the minimum of two values.
171224
#[inline]
172225
pub fn min<T: TotalOrd>(v1: T, v2: T) -> T {
173226
if v1 < v2 { v1 } else { v2 }
174227
}
175228

229+
/// Compare and return the maximum of two values.
176230
#[inline]
177231
pub fn max<T: TotalOrd>(v1: T, v2: T) -> T {
178232
if v1 > v2 { v1 } else { v2 }
@@ -184,11 +238,11 @@ mod test {
184238

185239
#[test]
186240
fn test_int_totalord() {
187-
assert_eq!(5.cmp(&10), Less);
188-
assert_eq!(10.cmp(&5), Greater);
189-
assert_eq!(5.cmp(&5), Equal);
190-
assert_eq!((-5).cmp(&12), Less);
191-
assert_eq!(12.cmp(-5), Greater);
241+
assert_eq!(5u.cmp(&10), Less);
242+
assert_eq!(10u.cmp(&5), Greater);
243+
assert_eq!(5u.cmp(&5), Equal);
244+
assert_eq!((-5u).cmp(&12), Less);
245+
assert_eq!(12u.cmp(-5), Greater);
192246
}
193247

194248
#[test]
@@ -210,4 +264,24 @@ mod test {
210264
t(Greater, o, Greater);
211265
}
212266
}
267+
268+
#[test]
269+
fn test_user_defined_eq() {
270+
// Our type.
271+
struct SketchyNum {
272+
num : int
273+
}
274+
275+
// Our implementation of `Eq` to support `==` and `!=`.
276+
impl Eq for SketchyNum {
277+
// Our custom eq allows numbers which are near eachother to be equal! :D
278+
fn eq(&self, other: &SketchyNum) -> bool {
279+
(self.num - other.num).abs() < 5
280+
}
281+
}
282+
283+
// Now these binary operators will work when applied!
284+
assert!(SketchyNum {num: 37} == SketchyNum {num: 34});
285+
assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
286+
}
213287
}

0 commit comments

Comments
 (0)