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.
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.
11 //! Operations on managed box types
13 use ptr::to_unsafe_ptr;
15 #[cfg(not(test))] use cmp::*;
17 pub static RC_MANAGED_UNIQUE : uint = (-2) as uint;
18 pub static RC_IMMORTAL : uint = 0x77777777;
20 /// Returns the refcount of a shared box (as just before calling this)
22 pub fn refcount<T>(t: @T) -> uint {
23 use unstable::raw::Repr;
24 unsafe { (*t.repr()).ref_count - 1 }
27 /// Determine if two shared boxes point to the same object
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));
34 /// Determine if two mutable shared boxes point to the same object
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));
42 impl<T:Eq> Eq for @T {
44 fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
46 fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
50 impl<T:Eq> Eq for @mut T {
52 fn eq(&self, other: &@mut T) -> bool { *(*self) == *(*other) }
54 fn ne(&self, other: &@mut T) -> bool { *(*self) != *(*other) }
58 impl<T:Ord> Ord for @T {
60 fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
62 fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
64 fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
66 fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
70 impl<T:Ord> Ord for @mut T {
72 fn lt(&self, other: &@mut T) -> bool { *(*self) < *(*other) }
74 fn le(&self, other: &@mut T) -> bool { *(*self) <= *(*other) }
76 fn ge(&self, other: &@mut T) -> bool { *(*self) >= *(*other) }
78 fn gt(&self, other: &@mut T) -> bool { *(*self) > *(*other) }
82 impl<T: TotalOrd> TotalOrd for @T {
84 fn cmp(&self, other: &@T) -> Ordering { (**self).cmp(*other) }
88 impl<T: TotalOrd> TotalOrd for @mut T {
90 fn cmp(&self, other: &@mut T) -> Ordering { (**self).cmp(*other) }
94 impl<T: TotalEq> TotalEq for @T {
96 fn equals(&self, other: &@T) -> bool { (**self).equals(*other) }
100 impl<T: TotalEq> TotalEq for @mut T {
102 fn equals(&self, other: &@mut T) -> bool { (**self).equals(*other) }
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)));
119 assert_eq!(refcount(x), 1);
121 assert_eq!(refcount(x), 2);
122 assert_eq!(refcount(y), 2);