Skip to content

Commit a54d046

Browse files
committed
Implement Ptr trait for mutable unsafe pointers. Closes #3926.
1 parent 92e3a8c commit a54d046

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Diff for: src/libcore/ptr.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub trait Ptr<T> {
176176
pure fn offset(count: uint) -> self;
177177
}
178178

179-
/// Extension methods for pointers
179+
/// Extension methods for immutable pointers
180180
impl<T> *T: Ptr<T> {
181181
/// Returns true if the pointer is equal to the null pointer.
182182
#[inline(always)]
@@ -191,6 +191,21 @@ impl<T> *T: Ptr<T> {
191191
pure fn offset(count: uint) -> *T { offset(self, count) }
192192
}
193193

194+
/// Extension methods for mutable pointers
195+
impl<T> *mut T: Ptr<T> {
196+
/// Returns true if the pointer is equal to the null pointer.
197+
#[inline(always)]
198+
pure fn is_null() -> bool { is_null(self) }
199+
200+
/// Returns true if the pointer is not equal to the null pointer.
201+
#[inline(always)]
202+
pure fn is_not_null() -> bool { is_not_null(self) }
203+
204+
/// Calculates the offset from a mutable pointer.
205+
#[inline(always)]
206+
pure fn offset(count: uint) -> *mut T { mut_offset(self, count) }
207+
}
208+
194209
// Equality for pointers
195210
impl<T> *const T : Eq {
196211
pure fn eq(other: &*const T) -> bool unsafe {
@@ -311,4 +326,12 @@ pub fn test_is_null() {
311326
let q = ptr::offset(p, 1u);
312327
assert !q.is_null();
313328
assert q.is_not_null();
329+
330+
let mp: *mut int = ptr::mut_null();
331+
assert mp.is_null();
332+
assert !mp.is_not_null();
333+
334+
let mq = mp.offset(1u);
335+
assert !mq.is_null();
336+
assert mq.is_not_null();
314337
}

0 commit comments

Comments
 (0)