Skip to content

Commit 042618d

Browse files
committed
ptr: replace unnecessary unsafe code
1 parent 29aba80 commit 042618d

File tree

1 file changed

+18
-58
lines changed

1 file changed

+18
-58
lines changed

src/libstd/ptr.rs

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ pub unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint {
7272

7373
/// Create an unsafe null pointer
7474
#[inline(always)]
75-
pub fn null<T>() -> *T { unsafe { cast::transmute(0u) } }
75+
pub fn null<T>() -> *T { 0 as *T }
7676

7777
/// Create an unsafe mutable null pointer
7878
#[inline(always)]
79-
pub fn mut_null<T>() -> *mut T { unsafe { cast::transmute(0u) } }
79+
pub fn mut_null<T>() -> *mut T { 0 as *mut T }
8080

8181
/// Returns true if the pointer is equal to the null pointer.
8282
#[inline(always)]
@@ -237,48 +237,28 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
237237
src
238238
}
239239

240-
/**
241-
Transform a region pointer - &T - to an unsafe pointer - *T.
242-
This is safe, but is implemented with an unsafe block due to
243-
transmute.
244-
*/
240+
/// Transform a region pointer - &T - to an unsafe pointer - *T.
245241
#[inline(always)]
246242
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
247-
unsafe { cast::transmute(thing) }
243+
thing as *T
248244
}
249245

250-
/**
251-
Transform a const region pointer - &const T - to a const unsafe pointer -
252-
*const T. This is safe, but is implemented with an unsafe block due to
253-
transmute.
254-
*/
246+
/// Transform a const region pointer - &const T - to a const unsafe pointer - *const T.
255247
#[inline(always)]
256248
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
257-
unsafe { cast::transmute(thing) }
249+
thing as *const T
258250
}
259251

260-
/**
261-
Transform a mutable region pointer - &mut T - to a mutable unsafe pointer -
262-
*mut T. This is safe, but is implemented with an unsafe block due to
263-
transmute.
264-
*/
252+
/// Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T.
265253
#[inline(always)]
266254
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
267-
unsafe { cast::transmute(thing) }
255+
thing as *mut T
268256
}
269257

270-
/**
271-
Cast a region pointer - &T - to a uint.
272-
This is safe, but is implemented with an unsafe block due to
273-
transmute.
274-
275-
(I couldn't think of a cutesy name for this one.)
276-
*/
258+
/// Cast a region pointer - &T - to a uint.
277259
#[inline(always)]
278260
pub fn to_uint<T>(thing: &T) -> uint {
279-
unsafe {
280-
cast::transmute(thing)
281-
}
261+
thing as *T as uint
282262
}
283263

284264
/// Determine if two borrowed pointers point to the same thing.
@@ -404,50 +384,30 @@ impl<T> Ptr<T> for *mut T {
404384
impl<T> Eq for *const T {
405385
#[inline(always)]
406386
fn eq(&self, other: &*const T) -> bool {
407-
unsafe {
408-
let a: uint = cast::transmute(*self);
409-
let b: uint = cast::transmute(*other);
410-
return a == b;
411-
}
387+
(*self as uint) == (*other as uint)
412388
}
413389
#[inline(always)]
414-
fn ne(&self, other: &*const T) -> bool { !(*self).eq(other) }
390+
fn ne(&self, other: &*const T) -> bool { !self.eq(other) }
415391
}
416392

417393
// Comparison for pointers
418394
#[cfg(not(test))]
419395
impl<T> Ord for *const T {
420396
#[inline(always)]
421397
fn lt(&self, other: &*const T) -> bool {
422-
unsafe {
423-
let a: uint = cast::transmute(*self);
424-
let b: uint = cast::transmute(*other);
425-
return a < b;
426-
}
398+
(*self as uint) < (*other as uint)
427399
}
428400
#[inline(always)]
429401
fn le(&self, other: &*const T) -> bool {
430-
unsafe {
431-
let a: uint = cast::transmute(*self);
432-
let b: uint = cast::transmute(*other);
433-
return a <= b;
434-
}
402+
(*self as uint) <= (*other as uint)
435403
}
436404
#[inline(always)]
437405
fn ge(&self, other: &*const T) -> bool {
438-
unsafe {
439-
let a: uint = cast::transmute(*self);
440-
let b: uint = cast::transmute(*other);
441-
return a >= b;
442-
}
406+
(*self as uint) >= (*other as uint)
443407
}
444408
#[inline(always)]
445409
fn gt(&self, other: &*const T) -> bool {
446-
unsafe {
447-
let a: uint = cast::transmute(*self);
448-
let b: uint = cast::transmute(*other);
449-
return a > b;
450-
}
410+
(*self as uint) > (*other as uint)
451411
}
452412
}
453413

@@ -456,11 +416,11 @@ impl<T> Ord for *const T {
456416
impl<'self,T:Eq> Eq for &'self T {
457417
#[inline(always)]
458418
fn eq(&self, other: & &'self T) -> bool {
459-
return *(*self) == *(*other);
419+
*(*self) == *(*other)
460420
}
461421
#[inline(always)]
462422
fn ne(&self, other: & &'self T) -> bool {
463-
return *(*self) != *(*other);
423+
*(*self) != *(*other)
464424
}
465425
}
466426

0 commit comments

Comments
 (0)