]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2012-2014 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 | ||
85aaf69f | 11 | //! Functionality for ordering and comparison. |
1a4d82fc | 12 | //! |
62682a34 SL |
13 | //! This module defines both `PartialOrd` and `PartialEq` traits which are used |
14 | //! by the compiler to implement comparison operators. Rust programs may | |
15 | //! implement `PartialOrd` to overload the `<`, `<=`, `>`, and `>=` operators, | |
16 | //! and may implement `PartialEq` to overload the `==` and `!=` operators. | |
1a4d82fc | 17 | |
85aaf69f | 18 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
19 | |
20 | use self::Ordering::*; | |
21 | ||
e9174d1e | 22 | use mem; |
1a4d82fc | 23 | use marker::Sized; |
e9174d1e | 24 | use option::Option::{self, Some}; |
1a4d82fc | 25 | |
62682a34 SL |
26 | /// Trait for equality comparisons which are [partial equivalence |
27 | /// relations](http://en.wikipedia.org/wiki/Partial_equivalence_relation). | |
1a4d82fc | 28 | /// |
62682a34 SL |
29 | /// This trait allows for partial equality, for types that do not have a full |
30 | /// equivalence relation. For example, in floating point numbers `NaN != NaN`, | |
31 | /// so floating point types implement `PartialEq` but not `Eq`. | |
1a4d82fc JJ |
32 | /// |
33 | /// Formally, the equality must be (for all `a`, `b` and `c`): | |
34 | /// | |
35 | /// - symmetric: `a == b` implies `b == a`; and | |
36 | /// - transitive: `a == b` and `b == c` implies `a == c`. | |
37 | /// | |
62682a34 SL |
38 | /// Note that these requirements mean that the trait itself must be implemented |
39 | /// symmetrically and transitively: if `T: PartialEq<U>` and `U: PartialEq<V>` | |
40 | /// then `U: PartialEq<T>` and `T: PartialEq<V>`. | |
1a4d82fc | 41 | /// |
62682a34 SL |
42 | /// PartialEq only requires the `eq` method to be implemented; `ne` is defined |
43 | /// in terms of it by default. Any manual implementation of `ne` *must* respect | |
44 | /// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and | |
45 | /// only if `a != b`. | |
92a42be0 SL |
46 | /// |
47 | /// This trait can be used with `#[derive]`. | |
d9579d0f | 48 | #[lang = "eq"] |
85aaf69f | 49 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 50 | pub trait PartialEq<Rhs: ?Sized = Self> { |
62682a34 SL |
51 | /// This method tests for `self` and `other` values to be equal, and is used |
52 | /// by `==`. | |
85aaf69f | 53 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
54 | fn eq(&self, other: &Rhs) -> bool; |
55 | ||
56 | /// This method tests for `!=`. | |
57 | #[inline] | |
85aaf69f | 58 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
59 | fn ne(&self, other: &Rhs) -> bool { !self.eq(other) } |
60 | } | |
61 | ||
62 | /// Trait for equality comparisons which are [equivalence relations]( | |
63 | /// https://en.wikipedia.org/wiki/Equivalence_relation). | |
64 | /// | |
85aaf69f SL |
65 | /// This means, that in addition to `a == b` and `a != b` being strict inverses, the equality must |
66 | /// be (for all `a`, `b` and `c`): | |
1a4d82fc JJ |
67 | /// |
68 | /// - reflexive: `a == a`; | |
69 | /// - symmetric: `a == b` implies `b == a`; and | |
70 | /// - transitive: `a == b` and `b == c` implies `a == c`. | |
85aaf69f SL |
71 | /// |
72 | /// This property cannot be checked by the compiler, and therefore `Eq` implies | |
73 | /// `PartialEq`, and has no extra methods. | |
92a42be0 SL |
74 | /// |
75 | /// This trait can be used with `#[derive]`. | |
85aaf69f | 76 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
77 | pub trait Eq: PartialEq<Self> { |
78 | // FIXME #13101: this method is used solely by #[deriving] to | |
79 | // assert that every component of a type implements #[deriving] | |
80 | // itself, the current deriving infrastructure means doing this | |
81 | // assertion without using a method on this trait is nearly | |
82 | // impossible. | |
83 | // | |
84 | // This should never be implemented by hand. | |
85 | #[doc(hidden)] | |
86 | #[inline(always)] | |
85aaf69f | 87 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
88 | fn assert_receiver_is_total_eq(&self) {} |
89 | } | |
90 | ||
85aaf69f SL |
91 | /// An `Ordering` is the result of a comparison between two values. |
92 | /// | |
93 | /// # Examples | |
94 | /// | |
95 | /// ``` | |
96 | /// use std::cmp::Ordering; | |
97 | /// | |
98 | /// let result = 1.cmp(&2); | |
99 | /// assert_eq!(Ordering::Less, result); | |
100 | /// | |
101 | /// let result = 1.cmp(&1); | |
102 | /// assert_eq!(Ordering::Equal, result); | |
103 | /// | |
104 | /// let result = 2.cmp(&1); | |
105 | /// assert_eq!(Ordering::Greater, result); | |
106 | /// ``` | |
107 | #[derive(Clone, Copy, PartialEq, Debug)] | |
108 | #[stable(feature = "rust1", since = "1.0.0")] | |
1a4d82fc JJ |
109 | pub enum Ordering { |
110 | /// An ordering where a compared value is less [than another]. | |
85aaf69f SL |
111 | #[stable(feature = "rust1", since = "1.0.0")] |
112 | Less = -1, | |
1a4d82fc | 113 | /// An ordering where a compared value is equal [to another]. |
85aaf69f SL |
114 | #[stable(feature = "rust1", since = "1.0.0")] |
115 | Equal = 0, | |
1a4d82fc | 116 | /// An ordering where a compared value is greater [than another]. |
85aaf69f SL |
117 | #[stable(feature = "rust1", since = "1.0.0")] |
118 | Greater = 1, | |
1a4d82fc JJ |
119 | } |
120 | ||
121 | impl Ordering { | |
e9174d1e SL |
122 | unsafe fn from_i8_unchecked(v: i8) -> Ordering { |
123 | mem::transmute(v) | |
124 | } | |
125 | ||
85aaf69f | 126 | /// Reverse the `Ordering`. |
1a4d82fc | 127 | /// |
85aaf69f SL |
128 | /// * `Less` becomes `Greater`. |
129 | /// * `Greater` becomes `Less`. | |
130 | /// * `Equal` becomes `Equal`. | |
1a4d82fc | 131 | /// |
85aaf69f | 132 | /// # Examples |
1a4d82fc | 133 | /// |
85aaf69f | 134 | /// Basic behavior: |
1a4d82fc | 135 | /// |
85aaf69f SL |
136 | /// ``` |
137 | /// use std::cmp::Ordering; | |
138 | /// | |
139 | /// assert_eq!(Ordering::Less.reverse(), Ordering::Greater); | |
140 | /// assert_eq!(Ordering::Equal.reverse(), Ordering::Equal); | |
141 | /// assert_eq!(Ordering::Greater.reverse(), Ordering::Less); | |
142 | /// ``` | |
143 | /// | |
144 | /// This method can be used to reverse a comparison: | |
145 | /// | |
146 | /// ``` | |
85aaf69f | 147 | /// let mut data: &mut [_] = &mut [2, 10, 5, 8]; |
1a4d82fc JJ |
148 | /// |
149 | /// // sort the array from largest to smallest. | |
150 | /// data.sort_by(|a, b| a.cmp(b).reverse()); | |
151 | /// | |
85aaf69f | 152 | /// let b: &mut [_] = &mut [10, 8, 5, 2]; |
1a4d82fc JJ |
153 | /// assert!(data == b); |
154 | /// ``` | |
155 | #[inline] | |
85aaf69f | 156 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
157 | pub fn reverse(self) -> Ordering { |
158 | unsafe { | |
159 | // this compiles really nicely (to a single instruction); | |
160 | // an explicit match has a pile of branches and | |
161 | // comparisons. | |
162 | // | |
163 | // NB. it is safe because of the explicit discriminants | |
164 | // given above. | |
e9174d1e | 165 | Ordering::from_i8_unchecked(-(self as i8)) |
1a4d82fc JJ |
166 | } |
167 | } | |
168 | } | |
169 | ||
85aaf69f | 170 | /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order). |
1a4d82fc JJ |
171 | /// |
172 | /// An order is a total order if it is (for all `a`, `b` and `c`): | |
173 | /// | |
85aaf69f SL |
174 | /// - total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and |
175 | /// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`. | |
c1a9b12d SL |
176 | /// |
177 | /// When this trait is `derive`d, it produces a lexicographic ordering. | |
92a42be0 SL |
178 | /// |
179 | /// This trait can be used with `#[derive]`. | |
85aaf69f | 180 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 181 | pub trait Ord: Eq + PartialOrd<Self> { |
85aaf69f SL |
182 | /// This method returns an `Ordering` between `self` and `other`. |
183 | /// | |
184 | /// By convention, `self.cmp(&other)` returns the ordering matching the expression | |
185 | /// `self <operator> other` if true. | |
1a4d82fc | 186 | /// |
85aaf69f | 187 | /// # Examples |
1a4d82fc JJ |
188 | /// |
189 | /// ``` | |
85aaf69f | 190 | /// use std::cmp::Ordering; |
1a4d82fc | 191 | /// |
85aaf69f SL |
192 | /// assert_eq!(5.cmp(&10), Ordering::Less); |
193 | /// assert_eq!(10.cmp(&5), Ordering::Greater); | |
194 | /// assert_eq!(5.cmp(&5), Ordering::Equal); | |
1a4d82fc | 195 | /// ``` |
85aaf69f | 196 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
197 | fn cmp(&self, other: &Self) -> Ordering; |
198 | } | |
199 | ||
85aaf69f | 200 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
201 | impl Eq for Ordering {} |
202 | ||
85aaf69f | 203 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
204 | impl Ord for Ordering { |
205 | #[inline] | |
1a4d82fc | 206 | fn cmp(&self, other: &Ordering) -> Ordering { |
85aaf69f | 207 | (*self as i32).cmp(&(*other as i32)) |
1a4d82fc JJ |
208 | } |
209 | } | |
210 | ||
85aaf69f | 211 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
212 | impl PartialOrd for Ordering { |
213 | #[inline] | |
1a4d82fc | 214 | fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> { |
85aaf69f | 215 | (*self as i32).partial_cmp(&(*other as i32)) |
1a4d82fc JJ |
216 | } |
217 | } | |
218 | ||
219 | /// Trait for values that can be compared for a sort-order. | |
220 | /// | |
221 | /// The comparison must satisfy, for all `a`, `b` and `c`: | |
222 | /// | |
b039eaaf | 223 | /// - antisymmetry: if `a < b` then `!(a > b)`, as well as `a > b` implying `!(a < b)`; and |
85aaf69f | 224 | /// - transitivity: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`. |
1a4d82fc | 225 | /// |
85aaf69f SL |
226 | /// Note that these requirements mean that the trait itself must be implemented symmetrically and |
227 | /// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T: | |
1a4d82fc JJ |
228 | /// PartialOrd<V>`. |
229 | /// | |
85aaf69f SL |
230 | /// PartialOrd only requires implementation of the `partial_cmp` method, with the others generated |
231 | /// from default implementations. | |
1a4d82fc | 232 | /// |
85aaf69f SL |
233 | /// However it remains possible to implement the others separately for types which do not have a |
234 | /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 == | |
235 | /// false` (cf. IEEE 754-2008 section 5.11). | |
92a42be0 SL |
236 | /// |
237 | /// This trait can be used with `#[derive]`. | |
d9579d0f | 238 | #[lang = "ord"] |
85aaf69f | 239 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 240 | pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> { |
85aaf69f SL |
241 | /// This method returns an ordering between `self` and `other` values if one exists. |
242 | /// | |
243 | /// # Examples | |
244 | /// | |
245 | /// ``` | |
246 | /// use std::cmp::Ordering; | |
247 | /// | |
248 | /// let result = 1.0.partial_cmp(&2.0); | |
249 | /// assert_eq!(result, Some(Ordering::Less)); | |
250 | /// | |
251 | /// let result = 1.0.partial_cmp(&1.0); | |
252 | /// assert_eq!(result, Some(Ordering::Equal)); | |
253 | /// | |
254 | /// let result = 2.0.partial_cmp(&1.0); | |
255 | /// assert_eq!(result, Some(Ordering::Greater)); | |
256 | /// ``` | |
257 | /// | |
258 | /// When comparison is impossible: | |
259 | /// | |
260 | /// ``` | |
261 | /// let result = std::f64::NAN.partial_cmp(&1.0); | |
262 | /// assert_eq!(result, None); | |
263 | /// ``` | |
264 | #[stable(feature = "rust1", since = "1.0.0")] | |
1a4d82fc JJ |
265 | fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>; |
266 | ||
267 | /// This method tests less than (for `self` and `other`) and is used by the `<` operator. | |
85aaf69f SL |
268 | /// |
269 | /// # Examples | |
270 | /// | |
271 | /// ``` | |
85aaf69f SL |
272 | /// let result = 1.0 < 2.0; |
273 | /// assert_eq!(result, true); | |
274 | /// | |
275 | /// let result = 2.0 < 1.0; | |
276 | /// assert_eq!(result, false); | |
277 | /// ``` | |
1a4d82fc | 278 | #[inline] |
85aaf69f | 279 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
280 | fn lt(&self, other: &Rhs) -> bool { |
281 | match self.partial_cmp(other) { | |
282 | Some(Less) => true, | |
283 | _ => false, | |
284 | } | |
285 | } | |
286 | ||
85aaf69f SL |
287 | /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=` |
288 | /// operator. | |
289 | /// | |
290 | /// # Examples | |
291 | /// | |
292 | /// ``` | |
293 | /// let result = 1.0 <= 2.0; | |
294 | /// assert_eq!(result, true); | |
295 | /// | |
296 | /// let result = 2.0 <= 2.0; | |
297 | /// assert_eq!(result, true); | |
298 | /// ``` | |
1a4d82fc | 299 | #[inline] |
85aaf69f | 300 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
301 | fn le(&self, other: &Rhs) -> bool { |
302 | match self.partial_cmp(other) { | |
303 | Some(Less) | Some(Equal) => true, | |
304 | _ => false, | |
305 | } | |
306 | } | |
307 | ||
85aaf69f SL |
308 | /// This method tests greater than (for `self` and `other`) and is used by the `>` operator. |
309 | /// | |
310 | /// # Examples | |
311 | /// | |
312 | /// ``` | |
313 | /// let result = 1.0 > 2.0; | |
314 | /// assert_eq!(result, false); | |
315 | /// | |
316 | /// let result = 2.0 > 2.0; | |
317 | /// assert_eq!(result, false); | |
318 | /// ``` | |
1a4d82fc | 319 | #[inline] |
85aaf69f | 320 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
321 | fn gt(&self, other: &Rhs) -> bool { |
322 | match self.partial_cmp(other) { | |
323 | Some(Greater) => true, | |
324 | _ => false, | |
325 | } | |
326 | } | |
327 | ||
85aaf69f SL |
328 | /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` |
329 | /// operator. | |
330 | /// | |
331 | /// # Examples | |
332 | /// | |
333 | /// ``` | |
334 | /// let result = 2.0 >= 1.0; | |
335 | /// assert_eq!(result, true); | |
336 | /// | |
337 | /// let result = 2.0 >= 2.0; | |
338 | /// assert_eq!(result, true); | |
339 | /// ``` | |
1a4d82fc | 340 | #[inline] |
85aaf69f | 341 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
342 | fn ge(&self, other: &Rhs) -> bool { |
343 | match self.partial_cmp(other) { | |
344 | Some(Greater) | Some(Equal) => true, | |
345 | _ => false, | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | /// Compare and return the minimum of two values. | |
85aaf69f | 351 | /// |
c34b1796 AL |
352 | /// Returns the first argument if the comparison determines them to be equal. |
353 | /// | |
85aaf69f SL |
354 | /// # Examples |
355 | /// | |
356 | /// ``` | |
357 | /// use std::cmp; | |
358 | /// | |
359 | /// assert_eq!(1, cmp::min(1, 2)); | |
360 | /// assert_eq!(2, cmp::min(2, 2)); | |
361 | /// ``` | |
1a4d82fc | 362 | #[inline] |
85aaf69f | 363 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 364 | pub fn min<T: Ord>(v1: T, v2: T) -> T { |
c34b1796 | 365 | if v1 <= v2 { v1 } else { v2 } |
1a4d82fc JJ |
366 | } |
367 | ||
368 | /// Compare and return the maximum of two values. | |
85aaf69f | 369 | /// |
c34b1796 AL |
370 | /// Returns the second argument if the comparison determines them to be equal. |
371 | /// | |
85aaf69f SL |
372 | /// # Examples |
373 | /// | |
374 | /// ``` | |
375 | /// use std::cmp; | |
376 | /// | |
377 | /// assert_eq!(2, cmp::max(1, 2)); | |
378 | /// assert_eq!(2, cmp::max(2, 2)); | |
379 | /// ``` | |
1a4d82fc | 380 | #[inline] |
85aaf69f | 381 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 382 | pub fn max<T: Ord>(v1: T, v2: T) -> T { |
c34b1796 | 383 | if v2 >= v1 { v2 } else { v1 } |
1a4d82fc JJ |
384 | } |
385 | ||
1a4d82fc JJ |
386 | // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types |
387 | mod impls { | |
388 | use cmp::{PartialOrd, Ord, PartialEq, Eq, Ordering}; | |
389 | use cmp::Ordering::{Less, Greater, Equal}; | |
390 | use marker::Sized; | |
391 | use option::Option; | |
392 | use option::Option::{Some, None}; | |
393 | ||
394 | macro_rules! partial_eq_impl { | |
395 | ($($t:ty)*) => ($( | |
85aaf69f | 396 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
397 | impl PartialEq for $t { |
398 | #[inline] | |
399 | fn eq(&self, other: &$t) -> bool { (*self) == (*other) } | |
400 | #[inline] | |
401 | fn ne(&self, other: &$t) -> bool { (*self) != (*other) } | |
402 | } | |
403 | )*) | |
404 | } | |
405 | ||
85aaf69f | 406 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
407 | impl PartialEq for () { |
408 | #[inline] | |
409 | fn eq(&self, _other: &()) -> bool { true } | |
410 | #[inline] | |
411 | fn ne(&self, _other: &()) -> bool { false } | |
412 | } | |
413 | ||
414 | partial_eq_impl! { | |
85aaf69f | 415 | bool char usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 |
1a4d82fc JJ |
416 | } |
417 | ||
418 | macro_rules! eq_impl { | |
419 | ($($t:ty)*) => ($( | |
85aaf69f | 420 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
421 | impl Eq for $t {} |
422 | )*) | |
423 | } | |
424 | ||
85aaf69f | 425 | eq_impl! { () bool char usize u8 u16 u32 u64 isize i8 i16 i32 i64 } |
1a4d82fc JJ |
426 | |
427 | macro_rules! partial_ord_impl { | |
428 | ($($t:ty)*) => ($( | |
85aaf69f | 429 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
430 | impl PartialOrd for $t { |
431 | #[inline] | |
432 | fn partial_cmp(&self, other: &$t) -> Option<Ordering> { | |
433 | match (self <= other, self >= other) { | |
434 | (false, false) => None, | |
435 | (false, true) => Some(Greater), | |
436 | (true, false) => Some(Less), | |
437 | (true, true) => Some(Equal), | |
438 | } | |
439 | } | |
440 | #[inline] | |
441 | fn lt(&self, other: &$t) -> bool { (*self) < (*other) } | |
442 | #[inline] | |
443 | fn le(&self, other: &$t) -> bool { (*self) <= (*other) } | |
444 | #[inline] | |
445 | fn ge(&self, other: &$t) -> bool { (*self) >= (*other) } | |
446 | #[inline] | |
447 | fn gt(&self, other: &$t) -> bool { (*self) > (*other) } | |
448 | } | |
449 | )*) | |
450 | } | |
451 | ||
85aaf69f | 452 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
453 | impl PartialOrd for () { |
454 | #[inline] | |
455 | fn partial_cmp(&self, _: &()) -> Option<Ordering> { | |
456 | Some(Equal) | |
457 | } | |
458 | } | |
459 | ||
85aaf69f | 460 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
461 | impl PartialOrd for bool { |
462 | #[inline] | |
463 | fn partial_cmp(&self, other: &bool) -> Option<Ordering> { | |
464 | (*self as u8).partial_cmp(&(*other as u8)) | |
465 | } | |
466 | } | |
467 | ||
b039eaaf | 468 | partial_ord_impl! { f32 f64 } |
1a4d82fc JJ |
469 | |
470 | macro_rules! ord_impl { | |
471 | ($($t:ty)*) => ($( | |
b039eaaf SL |
472 | #[stable(feature = "rust1", since = "1.0.0")] |
473 | impl PartialOrd for $t { | |
474 | #[inline] | |
475 | fn partial_cmp(&self, other: &$t) -> Option<Ordering> { | |
476 | Some(self.cmp(other)) | |
477 | } | |
478 | #[inline] | |
479 | fn lt(&self, other: &$t) -> bool { (*self) < (*other) } | |
480 | #[inline] | |
481 | fn le(&self, other: &$t) -> bool { (*self) <= (*other) } | |
482 | #[inline] | |
483 | fn ge(&self, other: &$t) -> bool { (*self) >= (*other) } | |
484 | #[inline] | |
485 | fn gt(&self, other: &$t) -> bool { (*self) > (*other) } | |
486 | } | |
487 | ||
85aaf69f | 488 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
489 | impl Ord for $t { |
490 | #[inline] | |
491 | fn cmp(&self, other: &$t) -> Ordering { | |
b039eaaf SL |
492 | if *self == *other { Equal } |
493 | else if *self < *other { Less } | |
494 | else { Greater } | |
1a4d82fc JJ |
495 | } |
496 | } | |
497 | )*) | |
498 | } | |
499 | ||
85aaf69f | 500 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
501 | impl Ord for () { |
502 | #[inline] | |
503 | fn cmp(&self, _other: &()) -> Ordering { Equal } | |
504 | } | |
505 | ||
85aaf69f | 506 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
507 | impl Ord for bool { |
508 | #[inline] | |
509 | fn cmp(&self, other: &bool) -> Ordering { | |
510 | (*self as u8).cmp(&(*other as u8)) | |
511 | } | |
512 | } | |
513 | ||
85aaf69f | 514 | ord_impl! { char usize u8 u16 u32 u64 isize i8 i16 i32 i64 } |
1a4d82fc JJ |
515 | |
516 | // & pointers | |
517 | ||
85aaf69f | 518 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
519 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a A where A: PartialEq<B> { |
520 | #[inline] | |
521 | fn eq(&self, other: & &'b B) -> bool { PartialEq::eq(*self, *other) } | |
522 | #[inline] | |
523 | fn ne(&self, other: & &'b B) -> bool { PartialEq::ne(*self, *other) } | |
524 | } | |
85aaf69f | 525 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
526 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b B> for &'a A where A: PartialOrd<B> { |
527 | #[inline] | |
528 | fn partial_cmp(&self, other: &&'b B) -> Option<Ordering> { | |
529 | PartialOrd::partial_cmp(*self, *other) | |
530 | } | |
531 | #[inline] | |
532 | fn lt(&self, other: & &'b B) -> bool { PartialOrd::lt(*self, *other) } | |
533 | #[inline] | |
534 | fn le(&self, other: & &'b B) -> bool { PartialOrd::le(*self, *other) } | |
535 | #[inline] | |
536 | fn ge(&self, other: & &'b B) -> bool { PartialOrd::ge(*self, *other) } | |
537 | #[inline] | |
538 | fn gt(&self, other: & &'b B) -> bool { PartialOrd::gt(*self, *other) } | |
539 | } | |
85aaf69f | 540 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
541 | impl<'a, A: ?Sized> Ord for &'a A where A: Ord { |
542 | #[inline] | |
543 | fn cmp(&self, other: & &'a A) -> Ordering { Ord::cmp(*self, *other) } | |
544 | } | |
85aaf69f | 545 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
546 | impl<'a, A: ?Sized> Eq for &'a A where A: Eq {} |
547 | ||
548 | // &mut pointers | |
549 | ||
85aaf69f | 550 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
551 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> { |
552 | #[inline] | |
553 | fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) } | |
554 | #[inline] | |
555 | fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) } | |
556 | } | |
85aaf69f | 557 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
558 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b mut B> for &'a mut A where A: PartialOrd<B> { |
559 | #[inline] | |
560 | fn partial_cmp(&self, other: &&'b mut B) -> Option<Ordering> { | |
561 | PartialOrd::partial_cmp(*self, *other) | |
562 | } | |
563 | #[inline] | |
564 | fn lt(&self, other: &&'b mut B) -> bool { PartialOrd::lt(*self, *other) } | |
565 | #[inline] | |
566 | fn le(&self, other: &&'b mut B) -> bool { PartialOrd::le(*self, *other) } | |
567 | #[inline] | |
568 | fn ge(&self, other: &&'b mut B) -> bool { PartialOrd::ge(*self, *other) } | |
569 | #[inline] | |
570 | fn gt(&self, other: &&'b mut B) -> bool { PartialOrd::gt(*self, *other) } | |
571 | } | |
85aaf69f | 572 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
573 | impl<'a, A: ?Sized> Ord for &'a mut A where A: Ord { |
574 | #[inline] | |
575 | fn cmp(&self, other: &&'a mut A) -> Ordering { Ord::cmp(*self, *other) } | |
576 | } | |
85aaf69f | 577 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
578 | impl<'a, A: ?Sized> Eq for &'a mut A where A: Eq {} |
579 | ||
85aaf69f | 580 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
581 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> { |
582 | #[inline] | |
583 | fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) } | |
584 | #[inline] | |
585 | fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) } | |
586 | } | |
587 | ||
85aaf69f | 588 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
589 | impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> { |
590 | #[inline] | |
591 | fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(*self, *other) } | |
592 | #[inline] | |
593 | fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(*self, *other) } | |
594 | } | |
595 | } |