From a54d04617413322e434f6eb6c0a786d61c2f8329 Mon Sep 17 00:00:00 2001 From: Zack Corr Date: Wed, 7 Nov 2012 16:24:29 +1000 Subject: [PATCH] Implement Ptr trait for mutable unsafe pointers. Closes #3926. --- src/libcore/ptr.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 2753a254c41e8..8767d56d80e9d 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -176,7 +176,7 @@ pub trait Ptr { pure fn offset(count: uint) -> self; } -/// Extension methods for pointers +/// Extension methods for immutable pointers impl *T: Ptr { /// Returns true if the pointer is equal to the null pointer. #[inline(always)] @@ -191,6 +191,21 @@ impl *T: Ptr { pure fn offset(count: uint) -> *T { offset(self, count) } } +/// Extension methods for mutable pointers +impl *mut T: Ptr { + /// 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 impl *const T : Eq { pure fn eq(other: &*const T) -> bool unsafe { @@ -311,4 +326,12 @@ pub fn test_is_null() { let q = ptr::offset(p, 1u); assert !q.is_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(); }