Skip to content

Commit

Permalink
Implement Ptr trait for mutable unsafe pointers. Closes rust-lang#3926.
Browse files Browse the repository at this point in the history
  • Loading branch information
z0w0 committed Nov 7, 2012
1 parent 92e3a8c commit a54d046
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub trait Ptr<T> {
pure fn offset(count: uint) -> self;
}

/// Extension methods for pointers
/// Extension methods for immutable pointers
impl<T> *T: Ptr<T> {
/// Returns true if the pointer is equal to the null pointer.
#[inline(always)]
Expand All @@ -191,6 +191,21 @@ impl<T> *T: Ptr<T> {
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
impl<T> *const T : Eq {
pure fn eq(other: &*const T) -> bool unsafe {
Expand Down Expand Up @@ -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();
}

0 comments on commit a54d046

Please sign in to comment.