Unsafe pointer utility functions
RawPtr
of Clone for *T where <T>
of RawPtr<T> for *T where <T>
- Extension methods for immutable pointersof RawPtr<T> for *mut T where <T>
- Extension methods for mutable pointersof Eq for *T where <T>
of Eq for *mut T where <T>
of Equiv<*mut T> for *T where <T>
of Equiv<*T> for *mut T where <T>
of Ord for *T where <T>
of Ord for *mut T where <T>
array_each
array_each_with_len
buf_len
- Return the offset of the first null pointer in buf
.copy_memory
- Copies data from one location to another.copy_nonoverlapping_memory
- Copies data from one location to another.is_not_null
- Returns true if the pointer is not equal to the null pointer.is_null
- Returns true if the pointer is equal to the null pointer.mut_null
- Create an unsafe mutable null pointermut_offset
- Calculate the offset from a mut pointernull
- Create an unsafe null pointeroffset
- Calculate the offset from a pointerposition
- Return the first offset i
such that f(buf[i]) == true
.read_and_zero_ptr
- Reads the value from *src
and nulls it outread_ptr
- Reads the value from *src
and returns itreplace_ptr
set_memory
- Invokes memset on the specified pointer, setting count * size_of::<T>()
bytes of memory starting at dst
to c
.swap_ptr
- Swap the values at two mutable locations of the same type, without deinitialising or copying either one.to_mut_unsafe_ptr
- Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T.to_unsafe_ptr
- Transform a region pointer - &T - to an unsafe pointer - *T.zero_memory
- Zeroes out count * size_of::<T>
bytes of memory at dst
RawPtr
null
fn null() -> Self
is_null
fn is_null(&self) -> bool
is_not_null
fn is_not_null(&self) -> bool
to_uint
fn to_uint(&self) -> uint
to_option
unsafe fn to_option(&self) -> Option<&T>
offset
unsafe fn offset(self, count: int) -> Self
Clone
for *T
where <T>
clone
fn clone(&self) -> *T
RawPtr<T>
for *T
where <T>
Extension methods for immutable pointers
null
fn null() -> *T
Returns the null pointer.
is_null
fn is_null(&self) -> bool
Returns true if the pointer is equal to the null pointer.
is_not_null
fn is_not_null(&self) -> bool
Returns true if the pointer is not equal to the null pointer.
to_uint
fn to_uint(&self) -> uint
Returns the address of this pointer.
to_option
unsafe fn to_option(&self) -> Option<&T>
Returns None
if the pointer is null, or else returns the value wrapped in Some
.
While this method is useful for null-safety, it is important to note that this is still an unsafe operation because the returned value could be pointing to invalid memory.
offset
unsafe fn offset(self, count: int) -> *T
Calculates the offset from a pointer. The offset must be in-bounds of the object, or one-byte-past-the-end.
RawPtr<T>
for *mut T
where <T>
Extension methods for mutable pointers
null
fn null() -> *mut T
Returns the null pointer.
is_null
fn is_null(&self) -> bool
Returns true if the pointer is equal to the null pointer.
is_not_null
fn is_not_null(&self) -> bool
Returns true if the pointer is not equal to the null pointer.
to_uint
fn to_uint(&self) -> uint
Returns the address of this pointer.
to_option
unsafe fn to_option(&self) -> Option<&T>
Returns None
if the pointer is null, or else returns the value wrapped in Some
.
While this method is useful for null-safety, it is important to note that this is still an unsafe operation because the returned value could be pointing to invalid memory.
offset
unsafe fn offset(self, count: int) -> *mut T
Calculates the offset from a pointer. The offset must be in-bounds of the object, or one-byte-past-the-end. An arithmetic overflow is also undefined behaviour.
This method should be preferred over offset
when the guarantee can be satisfied, to enable better optimization.
Eq
for *T
where <T>
eq
fn eq(&self, other: &*T) -> bool
ne
fn ne(&self, other: &*T) -> bool
Eq
for *mut T
where <T>
eq
fn eq(&self, other: &*mut T) -> bool
ne
fn ne(&self, other: &*mut T) -> bool
Equiv<*mut T>
for *T
where <T>
equiv
fn equiv(&self, other: &*mut T) -> bool
Equiv<*T>
for *mut T
where <T>
equiv
fn equiv(&self, other: &*T) -> bool
Ord
for *T
where <T>
lt
fn lt(&self, other: &*T) -> bool
le
fn le(&self, other: &*T) -> bool
ge
fn ge(&self, other: &*T) -> bool
gt
fn gt(&self, other: &*T) -> bool
Ord
for *mut T
where <T>
lt
fn lt(&self, other: &*mut T) -> bool
le
fn le(&self, other: &*mut T) -> bool
ge
fn ge(&self, other: &*mut T) -> bool
gt
fn gt(&self, other: &*mut T) -> bool
array_each
unsafe fn array_each<T>(arr: **T, cb: &fn(*T))
Given a null-pointer-terminated **T (pointer to an array of pointers), iterate through each *T, passing to the provided callback function
SAFETY NOTE: This will only work with a null-terminated pointer array. Barely less-dodgy Pointer Arithmetic. Dragons be here.
array_each_with_len
unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: &fn(*T))
Given a **T (pointer to an array of pointers), iterate through each *T, up to the provided len
, passing to the provided callback function
SAFETY NOTE: Pointer-arithmetic. Dragons be here.
buf_len
unsafe fn buf_len<T>(buf: **T) -> uint
Return the offset of the first null pointer in buf
.
copy_memory
unsafe fn copy_memory<T, P: RawPtr<T>>(dst: *mut T, src: P, count: uint)
Copies data from one location to another.
Copies count
elements (not bytes) from src
to dst
. The source and destination may overlap.
copy_nonoverlapping_memory
unsafe fn copy_nonoverlapping_memory<T,
P: RawPtr<T>>(dst: *mut T, src: P,
count: uint)
Copies data from one location to another.
Copies count
elements (not bytes) from src
to dst
. The source and destination may not overlap.
is_not_null
fn is_not_null<T, P: RawPtr<T>>(ptr: P) -> bool
Returns true if the pointer is not equal to the null pointer.
is_null
fn is_null<T, P: RawPtr<T>>(ptr: P) -> bool
Returns true if the pointer is equal to the null pointer.
mut_null
fn mut_null<T>() -> *mut T
Create an unsafe mutable null pointer
mut_offset
unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T
Calculate the offset from a mut pointer. The count must be in bounds or otherwise the loads of this address are undefined.
null
fn null<T>() -> *T
Create an unsafe null pointer
offset
unsafe fn offset<T>(ptr: *T, count: int) -> *T
Calculate the offset from a pointer
position
unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint
Return the first offset i
such that f(buf[i]) == true
.
read_and_zero_ptr
unsafe fn read_and_zero_ptr<T>(dest: *mut T) -> T
Reads the value from *src
and nulls it out. This currently prevents destructors from executing.
read_ptr
unsafe fn read_ptr<T>(src: *mut T) -> T
Reads the value from *src
and returns it. Does not copy *src
.
replace_ptr
unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T
Replace the value at a mutable location with a new one, returning the old value, without deinitialising or copying either one.
set_memory
unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint)
Invokes memset on the specified pointer, setting count * size_of::<T>()
bytes of memory starting at dst
to c
.
swap_ptr
unsafe fn swap_ptr<T>(x: *mut T, y: *mut T)
Swap the values at two mutable locations of the same type, without deinitialising or copying either one.
to_mut_unsafe_ptr
fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T
Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T.
to_unsafe_ptr
fn to_unsafe_ptr<T>(thing: &T) -> *T
Transform a region pointer - &T - to an unsafe pointer - *T.
zero_memory
unsafe fn zero_memory<T>(dst: *mut T, count: uint)
Zeroes out count * size_of::<T>
bytes of memory at dst