2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-20 17:33:52 -05:00
|
|
|
//! Operations on managed box types
|
2012-03-06 21:09:32 -06:00
|
|
|
|
2013-04-22 16:27:30 -05:00
|
|
|
use ptr::to_unsafe_ptr;
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-07-27 10:25:30 -05:00
|
|
|
#[cfg(not(test))] use cmp::*;
|
2013-02-20 17:02:21 -06:00
|
|
|
|
2013-10-17 23:13:56 -05:00
|
|
|
/// Returns the refcount of a shared box (as just before calling this)
|
|
|
|
#[inline]
|
|
|
|
pub fn refcount<T>(t: @T) -> uint {
|
|
|
|
use unstable::raw::Repr;
|
2013-10-18 11:22:03 -05:00
|
|
|
unsafe { (*t.repr()).ref_count - 1 }
|
2013-10-17 23:13:56 -05:00
|
|
|
}
|
|
|
|
|
2013-05-18 01:50:23 -05:00
|
|
|
/// Determine if two shared boxes point to the same object
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
|
2013-06-04 23:43:41 -05:00
|
|
|
let (a_ptr, b_ptr): (*T, *T) = (to_unsafe_ptr(&*a), to_unsafe_ptr(&*b));
|
2013-04-22 16:27:30 -05:00
|
|
|
a_ptr == b_ptr
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2013-05-08 06:11:23 -05:00
|
|
|
#[cfg(not(test))]
|
2013-03-21 17:39:17 -05:00
|
|
|
impl<T:Eq> Eq for @T {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-05-08 06:11:23 -05:00
|
|
|
#[cfg(not(test))]
|
2013-03-21 17:39:17 -05:00
|
|
|
impl<T:Ord> Ord for @T {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
|
2013-03-21 17:39:17 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 10:25:30 -05:00
|
|
|
#[cfg(not(test))]
|
|
|
|
impl<T: TotalOrd> TotalOrd for @T {
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &@T) -> Ordering { (**self).cmp(*other) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
impl<T: TotalEq> TotalEq for @T {
|
|
|
|
#[inline]
|
|
|
|
fn equals(&self, other: &@T) -> bool { (**self).equals(*other) }
|
|
|
|
}
|
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
let x = @3;
|
|
|
|
let y = @3;
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((ptr_eq::<int>(x, x)));
|
|
|
|
assert!((ptr_eq::<int>(y, y)));
|
|
|
|
assert!((!ptr_eq::<int>(x, y)));
|
|
|
|
assert!((!ptr_eq::<int>(y, x)));
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
2013-10-18 11:22:03 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn refcount_test() {
|
|
|
|
use clone::Clone;
|
|
|
|
|
|
|
|
let x = @3;
|
|
|
|
assert_eq!(refcount(x), 1);
|
|
|
|
let y = x.clone();
|
|
|
|
assert_eq!(refcount(x), 2);
|
|
|
|
assert_eq!(refcount(y), 2);
|
|
|
|
}
|