(index<- ) ./libstd/borrow.rs
1 // Copyright 2012-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 //! Borrowed pointer utilities
12
13 #[cfg(not(test))]
14 use prelude::*;
15
16 /// Cast a region pointer - &T - to a uint.
17 #[inline]
18 pub fn to_uint<T>(thing: &T) -> uint {
19 thing as *T as uint
20 }
21
22 /// Determine if two borrowed pointers point to the same thing.
23 #[inline]
24 pub fn ref_eq<'a, 'b, T>(thing: &'a T, other: &'b T) -> bool {
25 (thing as *T) == (other as *T)
26 }
27
28 // Equality for region pointers
29 #[cfg(not(test))]
30 impl<'self, T: Eq> Eq for &'self T {
31 #[inline]
32 fn eq(&self, other: & &'self T) -> bool {
33 *(*self) == *(*other)
34 }
35 #[inline]
36 fn ne(&self, other: & &'self T) -> bool {
37 *(*self) != *(*other)
38 }
39 }
40
41 // Comparison for region pointers
42 #[cfg(not(test))]
43 impl<'self, T: Ord> Ord for &'self T {
44 #[inline]
45 fn lt(&self, other: & &'self T) -> bool {
46 *(*self) < *(*other)
47 }
48 #[inline]
49 fn le(&self, other: & &'self T) -> bool {
50 *(*self) <= *(*other)
51 }
52 #[inline]
53 fn ge(&self, other: & &'self T) -> bool {
54 *(*self) >= *(*other)
55 }
56 #[inline]
57 fn gt(&self, other: & &'self T) -> bool {
58 *(*self) > *(*other)
59 }
60 }
61
62 #[cfg(not(test))]
63 impl<'self, T: TotalOrd> TotalOrd for &'self T {
64 #[inline]
65 fn cmp(&self, other: & &'self T) -> Ordering { (**self).cmp(*other) }
66 }
67
68 #[cfg(not(test))]
69 impl<'self, T: TotalEq> TotalEq for &'self T {
70 #[inline]
71 fn equals(&self, other: & &'self T) -> bool { (**self).equals(*other) }
72 }
73
74 #[cfg(test)]
75 mod tests {
76 use super::ref_eq;
77
78 #[test]
79 fn test_ref_eq() {
80 let x = 1;
81 let y = 1;
82
83 assert!(ref_eq(&x, &x));
84 assert!(!ref_eq(&x, &y));
85 }
86 }