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
|
/// Create an unsafe null pointer
|
||||||
#[inline(always)]
|
#[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
|
/// Create an unsafe mutable null pointer
|
||||||
#[inline(always)]
|
#[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.
|
/// Returns true if the pointer is equal to the null pointer.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
@@ -237,48 +237,28 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
|
|||||||
src
|
src
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Transform a region pointer - &T - to an unsafe pointer - *T.
|
||||||
Transform a region pointer - &T - to an unsafe pointer - *T.
|
|
||||||
This is safe, but is implemented with an unsafe block due to
|
|
||||||
transmute.
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
|
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.
|
||||||
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.
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
|
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.
|
||||||
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.
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
|
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.
|
||||||
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.)
|
|
||||||
*/
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_uint<T>(thing: &T) -> uint {
|
pub fn to_uint<T>(thing: &T) -> uint {
|
||||||
unsafe {
|
thing as *T as uint
|
||||||
cast::transmute(thing)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine if two borrowed pointers point to the same thing.
|
/// 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 {
|
impl<T> Eq for *const T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn eq(&self, other: &*const T) -> bool {
|
fn eq(&self, other: &*const T) -> bool {
|
||||||
unsafe {
|
(*self as uint) == (*other as uint)
|
||||||
let a: uint = cast::transmute(*self);
|
|
||||||
let b: uint = cast::transmute(*other);
|
|
||||||
return a == b;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[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
|
// Comparison for pointers
|
||||||
@@ -419,35 +395,19 @@ impl<T> Eq for *const T {
|
|||||||
impl<T> Ord for *const T {
|
impl<T> Ord for *const T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn lt(&self, other: &*const T) -> bool {
|
fn lt(&self, other: &*const T) -> bool {
|
||||||
unsafe {
|
(*self as uint) < (*other as uint)
|
||||||
let a: uint = cast::transmute(*self);
|
|
||||||
let b: uint = cast::transmute(*other);
|
|
||||||
return a < b;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn le(&self, other: &*const T) -> bool {
|
fn le(&self, other: &*const T) -> bool {
|
||||||
unsafe {
|
(*self as uint) <= (*other as uint)
|
||||||
let a: uint = cast::transmute(*self);
|
|
||||||
let b: uint = cast::transmute(*other);
|
|
||||||
return a <= b;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn ge(&self, other: &*const T) -> bool {
|
fn ge(&self, other: &*const T) -> bool {
|
||||||
unsafe {
|
(*self as uint) >= (*other as uint)
|
||||||
let a: uint = cast::transmute(*self);
|
|
||||||
let b: uint = cast::transmute(*other);
|
|
||||||
return a >= b;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn gt(&self, other: &*const T) -> bool {
|
fn gt(&self, other: &*const T) -> bool {
|
||||||
unsafe {
|
(*self as uint) > (*other as uint)
|
||||||
let a: uint = cast::transmute(*self);
|
|
||||||
let b: uint = cast::transmute(*other);
|
|
||||||
return a > b;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,11 +416,11 @@ impl<T> Ord for *const T {
|
|||||||
impl<'self,T:Eq> Eq for &'self T {
|
impl<'self,T:Eq> Eq for &'self T {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn eq(&self, other: & &'self T) -> bool {
|
fn eq(&self, other: & &'self T) -> bool {
|
||||||
return *(*self) == *(*other);
|
*(*self) == *(*other)
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn ne(&self, other: & &'self T) -> bool {
|
fn ne(&self, other: & &'self T) -> bool {
|
||||||
return *(*self) != *(*other);
|
*(*self) != *(*other)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user