Skip to content

Commit

Permalink
make transmute_copy use memcpy, and inline it
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed May 26, 2013
1 parent 58d6864 commit b25c520
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/libstd/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use sys;
use unstable::intrinsics;

/// Casts the value at `src` to U. The two types must have the same length.
#[cfg(stage0)]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
let mut dest: U = intrinsics::uninit();
{
Expand All @@ -26,6 +27,26 @@ pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
dest
}

#[cfg(target_word_size = "32", not(stage0))]
#[inline(always)]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
let mut dest: U = intrinsics::uninit();
let dest_ptr: *mut u8 = transmute(&mut dest);
let src_ptr: *u8 = transmute(src);
intrinsics::memcpy32(dest_ptr, src_ptr, sys::size_of::<U>() as u64);
dest
}

#[cfg(target_word_size = "64", not(stage0))]
#[inline(always)]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
let mut dest: U = intrinsics::uninit();
let dest_ptr: *mut u8 = transmute(&mut dest);
let src_ptr: *u8 = transmute(src);
intrinsics::memcpy64(dest_ptr, src_ptr, sys::size_of::<U>() as u64);
dest
}

/**
* Move a thing into the void
*
Expand Down

0 comments on commit b25c520

Please sign in to comment.