ptr: replace unnecessary unsafe code
This commit is contained in:
@@ -72,11 +72,11 @@ pub unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint {
|
||||
|
||||
/// Create an unsafe null pointer
|
||||
#[inline(always)]
|
||||
pub fn null<T>() -> *T { unsafe { cast::transmute(0u) } }
|
||||
pub fn null<T>() -> *T { 0 as *T }
|
||||
|
||||
/// Create an unsafe mutable null pointer
|
||||
#[inline(always)]
|
||||
pub fn mut_null<T>() -> *mut T { unsafe { cast::transmute(0u) } }
|
||||
pub fn mut_null<T>() -> *mut T { 0 as *mut T }
|
||||
|
||||
/// Returns true if the pointer is equal to the null pointer.
|
||||
#[inline(always)]
|
||||
@@ -237,48 +237,28 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
|
||||
src
|
||||
}
|
||||
|
||||
/**
|
||||
Transform a region pointer - &T - to an unsafe pointer - *T.
|
||||
This is safe, but is implemented with an unsafe block due to
|
||||
transmute.
|
||||
*/
|
||||
/// Transform a region pointer - &T - to an unsafe pointer - *T.
|
||||
#[inline(always)]
|
||||
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
|
||||
unsafe { cast::transmute(thing) }
|
||||
thing as *T
|
||||
}
|
||||
|
||||
/**
|
||||
Transform a const region pointer - &const T - to a const unsafe pointer -
|
||||
*const T. This is safe, but is implemented with an unsafe block due to
|
||||
transmute.
|
||||
*/
|
||||
/// Transform a const region pointer - &const T - to a const unsafe pointer - *const T.
|
||||
#[inline(always)]
|
||||
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
|
||||
unsafe { cast::transmute(thing) }
|
||||
thing as *const T
|
||||
}
|
||||
|
||||
/**
|
||||
Transform a mutable region pointer - &mut T - to a mutable unsafe pointer -
|
||||
*mut T. This is safe, but is implemented with an unsafe block due to
|
||||
transmute.
|
||||
*/
|
||||
/// Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T.
|
||||
#[inline(always)]
|
||||
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
|
||||
unsafe { cast::transmute(thing) }
|
||||
thing as *mut T
|
||||
}
|
||||
|
||||
/**
|
||||
Cast a region pointer - &T - to a uint.
|
||||
This is safe, but is implemented with an unsafe block due to
|
||||
transmute.
|
||||
|
||||
(I couldn't think of a cutesy name for this one.)
|
||||
*/
|
||||
/// Cast a region pointer - &T - to a uint.
|
||||
#[inline(always)]
|
||||
pub fn to_uint<T>(thing: &T) -> uint {
|
||||
unsafe {
|
||||
cast::transmute(thing)
|
||||
}
|
||||
thing as *T as uint
|
||||
}
|
||||
|
||||
/// Determine if two borrowed pointers point to the same thing.
|
||||
@@ -404,14 +384,10 @@ impl<T> Ptr<T> for *mut T {
|
||||
impl<T> Eq for *const T {
|
||||
#[inline(always)]
|
||||
fn eq(&self, other: &*const T) -> bool {
|
||||
unsafe {
|
||||
let a: uint = cast::transmute(*self);
|
||||
let b: uint = cast::transmute(*other);
|
||||
return a == b;
|
||||
}
|
||||
(*self as uint) == (*other as uint)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn ne(&self, other: &*const T) -> bool { !(*self).eq(other) }
|
||||
fn ne(&self, other: &*const T) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
// Comparison for pointers
|
||||
@@ -419,35 +395,19 @@ impl<T> Eq for *const T {
|
||||
impl<T> Ord for *const T {
|
||||
#[inline(always)]
|
||||
fn lt(&self, other: &*const T) -> bool {
|
||||
unsafe {
|
||||
let a: uint = cast::transmute(*self);
|
||||
let b: uint = cast::transmute(*other);
|
||||
return a < b;
|
||||
}
|
||||
(*self as uint) < (*other as uint)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn le(&self, other: &*const T) -> bool {
|
||||
unsafe {
|
||||
let a: uint = cast::transmute(*self);
|
||||
let b: uint = cast::transmute(*other);
|
||||
return a <= b;
|
||||
}
|
||||
(*self as uint) <= (*other as uint)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn ge(&self, other: &*const T) -> bool {
|
||||
unsafe {
|
||||
let a: uint = cast::transmute(*self);
|
||||
let b: uint = cast::transmute(*other);
|
||||
return a >= b;
|
||||
}
|
||||
(*self as uint) >= (*other as uint)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn gt(&self, other: &*const T) -> bool {
|
||||
unsafe {
|
||||
let a: uint = cast::transmute(*self);
|
||||
let b: uint = cast::transmute(*other);
|
||||
return a > b;
|
||||
}
|
||||
(*self as uint) > (*other as uint)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,11 +416,11 @@ impl<T> Ord for *const T {
|
||||
impl<'self,T:Eq> Eq for &'self T {
|
||||
#[inline(always)]
|
||||
fn eq(&self, other: & &'self T) -> bool {
|
||||
return *(*self) == *(*other);
|
||||
*(*self) == *(*other)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn ne(&self, other: & &'self T) -> bool {
|
||||
return *(*self) != *(*other);
|
||||
*(*self) != *(*other)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user