Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}