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 /// Determine if two shared boxes point to the same object
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));
27 /// Determine if two mutable shared boxes point to the same object
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));
35 impl<T:Eq> Eq for @T {
37 fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
39 fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
43 impl<T:Eq> Eq for @mut T {
45 fn eq(&self, other: &@mut T) -> bool { *(*self) == *(*other) }
47 fn ne(&self, other: &@mut T) -> bool { *(*self) != *(*other) }
51 impl<T:Ord> Ord for @T {
53 fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
55 fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
57 fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
59 fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
63 impl<T:Ord> Ord for @mut T {
65 fn lt(&self, other: &@mut T) -> bool { *(*self) < *(*other) }
67 fn le(&self, other: &@mut T) -> bool { *(*self) <= *(*other) }
69 fn ge(&self, other: &@mut T) -> bool { *(*self) >= *(*other) }
71 fn gt(&self, other: &@mut T) -> bool { *(*self) > *(*other) }
75 impl<T: TotalOrd> TotalOrd for @T {
77 fn cmp(&self, other: &@T) -> Ordering { (**self).cmp(*other) }
81 impl<T: TotalOrd> TotalOrd for @mut T {
83 fn cmp(&self, other: &@mut T) -> Ordering { (**self).cmp(*other) }
87 impl<T: TotalEq> TotalEq for @T {
89 fn equals(&self, other: &@T) -> bool { (**self).equals(*other) }
93 impl<T: TotalEq> TotalEq for @mut T {
95 fn equals(&self, other: &@mut T) -> bool { (**self).equals(*other) }
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)));