]> git.lizzy.rs Git - rust.git/blob - src/libstd/managed.rs
auto merge of #8350 : dim-an/rust/fix-struct-match, r=pcwalton
[rust.git] / src / libstd / managed.rs
1 // Copyright 2012 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 //! Operations on managed box types
12
13 use ptr::to_unsafe_ptr;
14
15 #[cfg(not(test))] use cmp::*;
16
17 pub static RC_MANAGED_UNIQUE : uint = (-2) as uint;
18 pub static RC_IMMORTAL : uint = 0x77777777;
19
20 /// Determine if two shared boxes point to the same object
21 #[inline]
22 pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
23     let (a_ptr, b_ptr): (*T, *T) = (to_unsafe_ptr(&*a), to_unsafe_ptr(&*b));
24     a_ptr == b_ptr
25 }
26
27 /// Determine if two mutable shared boxes point to the same object
28 #[inline]
29 pub fn mut_ptr_eq<T>(a: @mut T, b: @mut T) -> bool {
30     let (a_ptr, b_ptr): (*T, *T) = (to_unsafe_ptr(&*a), to_unsafe_ptr(&*b));
31     a_ptr == b_ptr
32 }
33
34 #[cfg(not(test))]
35 impl<T:Eq> Eq for @T {
36     #[inline]
37     fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
38     #[inline]
39     fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
40 }
41
42 #[cfg(not(test))]
43 impl<T:Eq> Eq for @mut T {
44     #[inline]
45     fn eq(&self, other: &@mut T) -> bool { *(*self) == *(*other) }
46     #[inline]
47     fn ne(&self, other: &@mut T) -> bool { *(*self) != *(*other) }
48 }
49
50 #[cfg(not(test))]
51 impl<T:Ord> Ord for @T {
52     #[inline]
53     fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
54     #[inline]
55     fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
56     #[inline]
57     fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
58     #[inline]
59     fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
60 }
61
62 #[cfg(not(test))]
63 impl<T:Ord> Ord for @mut T {
64     #[inline]
65     fn lt(&self, other: &@mut T) -> bool { *(*self) < *(*other) }
66     #[inline]
67     fn le(&self, other: &@mut T) -> bool { *(*self) <= *(*other) }
68     #[inline]
69     fn ge(&self, other: &@mut T) -> bool { *(*self) >= *(*other) }
70     #[inline]
71     fn gt(&self, other: &@mut T) -> bool { *(*self) > *(*other) }
72 }
73
74 #[cfg(not(test))]
75 impl<T: TotalOrd> TotalOrd for @T {
76     #[inline]
77     fn cmp(&self, other: &@T) -> Ordering { (**self).cmp(*other) }
78 }
79
80 #[cfg(not(test))]
81 impl<T: TotalOrd> TotalOrd for @mut T {
82     #[inline]
83     fn cmp(&self, other: &@mut T) -> Ordering { (**self).cmp(*other) }
84 }
85
86 #[cfg(not(test))]
87 impl<T: TotalEq> TotalEq for @T {
88     #[inline]
89     fn equals(&self, other: &@T) -> bool { (**self).equals(*other) }
90 }
91
92 #[cfg(not(test))]
93 impl<T: TotalEq> TotalEq for @mut T {
94     #[inline]
95     fn equals(&self, other: &@mut T) -> bool { (**self).equals(*other) }
96 }
97 #[test]
98 fn test() {
99     let x = @3;
100     let y = @3;
101     assert!((ptr_eq::<int>(x, x)));
102     assert!((ptr_eq::<int>(y, y)));
103     assert!((!ptr_eq::<int>(x, y)));
104     assert!((!ptr_eq::<int>(y, x)));
105 }