]> git.lizzy.rs Git - rust.git/blob - src/libstd/managed.rs
auto merge of #10977 : brson/rust/androidtest, r=brson
[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 /// Returns the refcount of a shared box (as just before calling this)
21 #[inline]
22 pub fn refcount<T>(t: @T) -> uint {
23     use unstable::raw::Repr;
24     unsafe { (*t.repr()).ref_count - 1 }
25 }
26
27 /// Determine if two shared boxes point to the same object
28 #[inline]
29 pub fn ptr_eq<T>(a: @T, b: @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 /// Determine if two mutable shared boxes point to the same object
35 #[inline]
36 pub fn mut_ptr_eq<T>(a: @mut T, b: @mut T) -> bool {
37     let (a_ptr, b_ptr): (*T, *T) = (to_unsafe_ptr(&*a), to_unsafe_ptr(&*b));
38     a_ptr == b_ptr
39 }
40
41 #[cfg(not(test))]
42 impl<T:Eq> Eq for @T {
43     #[inline]
44     fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
45     #[inline]
46     fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
47 }
48
49 #[cfg(not(test))]
50 impl<T:Eq> Eq for @mut T {
51     #[inline]
52     fn eq(&self, other: &@mut T) -> bool { *(*self) == *(*other) }
53     #[inline]
54     fn ne(&self, other: &@mut T) -> bool { *(*self) != *(*other) }
55 }
56
57 #[cfg(not(test))]
58 impl<T:Ord> Ord for @T {
59     #[inline]
60     fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
61     #[inline]
62     fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
63     #[inline]
64     fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
65     #[inline]
66     fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
67 }
68
69 #[cfg(not(test))]
70 impl<T:Ord> Ord for @mut T {
71     #[inline]
72     fn lt(&self, other: &@mut T) -> bool { *(*self) < *(*other) }
73     #[inline]
74     fn le(&self, other: &@mut T) -> bool { *(*self) <= *(*other) }
75     #[inline]
76     fn ge(&self, other: &@mut T) -> bool { *(*self) >= *(*other) }
77     #[inline]
78     fn gt(&self, other: &@mut T) -> bool { *(*self) > *(*other) }
79 }
80
81 #[cfg(not(test))]
82 impl<T: TotalOrd> TotalOrd for @T {
83     #[inline]
84     fn cmp(&self, other: &@T) -> Ordering { (**self).cmp(*other) }
85 }
86
87 #[cfg(not(test))]
88 impl<T: TotalOrd> TotalOrd for @mut T {
89     #[inline]
90     fn cmp(&self, other: &@mut T) -> Ordering { (**self).cmp(*other) }
91 }
92
93 #[cfg(not(test))]
94 impl<T: TotalEq> TotalEq for @T {
95     #[inline]
96     fn equals(&self, other: &@T) -> bool { (**self).equals(*other) }
97 }
98
99 #[cfg(not(test))]
100 impl<T: TotalEq> TotalEq for @mut T {
101     #[inline]
102     fn equals(&self, other: &@mut T) -> bool { (**self).equals(*other) }
103 }
104 #[test]
105 fn test() {
106     let x = @3;
107     let y = @3;
108     assert!((ptr_eq::<int>(x, x)));
109     assert!((ptr_eq::<int>(y, y)));
110     assert!((!ptr_eq::<int>(x, y)));
111     assert!((!ptr_eq::<int>(y, x)));
112 }
113
114 #[test]
115 fn refcount_test() {
116     use clone::Clone;
117
118     let x = @3;
119     assert_eq!(refcount(x), 1);
120     let y = x.clone();
121     assert_eq!(refcount(x), 2);
122     assert_eq!(refcount(y), 2);
123 }