Unsafe pointer utility functions

Trait RawPtr

Method is_null

fn is_null(&self) -> bool

Method is_not_null

fn is_not_null(&self) -> bool

Method to_option

unsafe fn to_option(&self) -> Option<&T>

Method offset

fn offset(&self, count: uint) -> Self

Implementation of RawPtr<T> for *T where <T>

Extension methods for immutable pointers

Method is_null

fn is_null(&self) -> bool

Returns true if the pointer is equal to the null pointer.

Method is_not_null

fn is_not_null(&self) -> bool

Returns true if the pointer is not equal to the null pointer.

Method to_option

unsafe fn to_option(&self) -> Option<&T>

Returns None if the pointer is null, or else returns the value wrapped in Some.

Safety Notes

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.

Method offset

fn offset(&self, count: uint) -> *T

Calculates the offset from a pointer.

Implementation of RawPtr<T> for *mut T where <T>

Extension methods for mutable pointers

Method is_null

fn is_null(&self) -> bool

Returns true if the pointer is equal to the null pointer.

Method is_not_null

fn is_not_null(&self) -> bool

Returns true if the pointer is not equal to the null pointer.

Method to_option

unsafe fn to_option(&self) -> Option<&T>

Returns None if the pointer is null, or else returns the value wrapped in Some.

Safety Notes

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.

Method offset

fn offset(&self, count: uint) -> *mut T

Calculates the offset from a mutable pointer.

Implementation of Eq for *const T where <T>

Method eq

fn eq(&self, other: &*const T) -> bool

Method ne

fn ne(&self, other: &*const T) -> bool

Implementation of Ord for *const T where <T>

Method lt

fn lt(&self, other: &*const T) -> bool

Method le

fn le(&self, other: &*const T) -> bool

Method ge

fn ge(&self, other: &*const T) -> bool

Method gt

fn gt(&self, other: &*const T) -> bool

Function 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-dodgey Pointer Arithmetic. Dragons be here.

Function 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.

Function buf_len

unsafe fn buf_len<T>(buf: **T) -> uint

Return the offset of the first null pointer in buf.

Function const_offset

fn const_offset<T>(ptr: *const T, count: uint) -> *const T

Calculate the offset from a const pointer

Function copy_memory

unsafe fn copy_memory<T>(dst: *mut T, src: *const T, count: uint)

Copies data from one location to another.

Copies count elements (not bytes) from src to dst. The source and destination may overlap.

Function copy_nonoverlapping_memory

unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
                                        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.

Function is_not_null

fn is_not_null<T>(ptr: *const T) -> bool

Returns true if the pointer is not equal to the null pointer.

Function is_null

fn is_null<T>(ptr: *const T) -> bool

Returns true if the pointer is equal to the null pointer.

Function mut_null

fn mut_null<T>() -> *mut T

Create an unsafe mutable null pointer

Function mut_offset

fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T

Calculate the offset from a mut pointer

Function null

fn null<T>() -> *T

Create an unsafe null pointer

Function offset

fn offset<T>(ptr: *T, count: uint) -> *T

Calculate the offset from a pointer

Function position

unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint

Return the first offset i such that f(buf[i]) == true.

Function 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.

Function 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.

Function 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.

Function to_const_unsafe_ptr

fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T

Transform a const region pointer - &const T - to a const unsafe pointer - *const T.

Function 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.

Function to_unsafe_ptr

fn to_unsafe_ptr<T>(thing: &T) -> *T

Transform a region pointer - &T - to an unsafe pointer - *T.