Implement Ptr trait for mutable unsafe pointers. Closes #3926.

This commit is contained in:
Zack Corr 2012-11-07 16:24:29 +10:00
parent 92e3a8c17e
commit a54d046174

View File

@ -176,7 +176,7 @@ pub trait Ptr<T> {
pure fn offset(count: uint) -> self; pure fn offset(count: uint) -> self;
} }
/// Extension methods for pointers /// Extension methods for immutable pointers
impl<T> *T: Ptr<T> { impl<T> *T: Ptr<T> {
/// Returns true if the pointer is equal to the null pointer. /// Returns true if the pointer is equal to the null pointer.
#[inline(always)] #[inline(always)]
@ -191,6 +191,21 @@ impl<T> *T: Ptr<T> {
pure fn offset(count: uint) -> *T { offset(self, count) } pure fn offset(count: uint) -> *T { offset(self, count) }
} }
/// Extension methods for mutable pointers
impl<T> *mut T: Ptr<T> {
/// Returns true if the pointer is equal to the null pointer.
#[inline(always)]
pure fn is_null() -> bool { is_null(self) }
/// Returns true if the pointer is not equal to the null pointer.
#[inline(always)]
pure fn is_not_null() -> bool { is_not_null(self) }
/// Calculates the offset from a mutable pointer.
#[inline(always)]
pure fn offset(count: uint) -> *mut T { mut_offset(self, count) }
}
// Equality for pointers // Equality for pointers
impl<T> *const T : Eq { impl<T> *const T : Eq {
pure fn eq(other: &*const T) -> bool unsafe { pure fn eq(other: &*const T) -> bool unsafe {
@ -311,4 +326,12 @@ pub fn test_is_null() {
let q = ptr::offset(p, 1u); let q = ptr::offset(p, 1u);
assert !q.is_null(); assert !q.is_null();
assert q.is_not_null(); assert q.is_not_null();
let mp: *mut int = ptr::mut_null();
assert mp.is_null();
assert !mp.is_not_null();
let mq = mp.offset(1u);
assert !mq.is_null();
assert mq.is_not_null();
} }