Skip to content

clean up some transmutes #13007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/libserialize/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,10 @@ pub mod reader {
}

fn read_f64(&mut self) -> f64 {
let bits = doc_as_u64(self.next_doc(EsF64));
unsafe { transmute(bits) }
doc_as_u64(self.next_doc(EsF64)) as f64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite accurate because u64 as f64 is a floating-point-cast, whereas transmute is just a bit-cast

}
fn read_f32(&mut self) -> f32 {
let bits = doc_as_u32(self.next_doc(EsF32));
unsafe { transmute(bits) }
doc_as_u32(self.next_doc(EsF32)) as f32
}
fn read_char(&mut self) -> char {
char::from_u32(doc_as_u32(self.next_doc(EsChar))).unwrap()
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'a> AnyRefExt<'a> for &'a Any {
let to: TraitObject = transmute_copy(&self);

// Extract the data pointer
Some(transmute(to.data))
Some(transmute::<*(), &'a T>(to.data))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's adding these params about?

Also, I'd prefer these to be

&*(to.data as *T)

rather than transmute.

}
} else {
None
Expand All @@ -109,7 +109,7 @@ impl<'a> AnyMutRefExt<'a> for &'a mut Any {
let to: TraitObject = transmute_copy(&self);

// Extract the data pointer
Some(transmute(to.data))
Some(transmute::<*(), &'a mut T>(to.data))
}
} else {
None
Expand All @@ -136,7 +136,7 @@ impl AnyOwnExt for ~Any {
intrinsics::forget(self);

// Extract the data pointer
Ok(transmute(to.data))
Ok(transmute::<*(), ~T>(to.data))
}
} else {
Err(self)
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ use str::StrSlice;
use str;
use vec::{ImmutableVector, MutableVector};
use vec;
use raw;
use rt::global_heap::malloc_raw;
use raw::Slice;

/// The representation of a C String.
///
Expand Down Expand Up @@ -179,7 +179,7 @@ impl CString {
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
if self.buf.is_null() { fail!("CString is null!"); }
unsafe {
cast::transmute(Slice { data: self.buf, len: self.len() + 1 })
raw::slice_from_buf(self.buf as *u8, self.len() + 1)
}
}

Expand All @@ -193,7 +193,7 @@ impl CString {
pub fn as_bytes_no_nul<'a>(&'a self) -> &'a [u8] {
if self.buf.is_null() { fail!("CString is null!"); }
unsafe {
cast::transmute(Slice { data: self.buf, len: self.len() })
raw::slice_from_buf(self.buf as *u8, self.len())
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/libstd/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
//! handled correctly, i.e. that allocated memory is eventually freed
//! if necessary.

use cast;
use container::Container;
use ptr;
use ptr::RawPtr;
Expand Down Expand Up @@ -101,14 +100,14 @@ impl<T> CVec<T> {
/// View the stored data as a slice.
pub fn as_slice<'a>(&'a self) -> &'a [T] {
unsafe {
cast::transmute(raw::Slice { data: self.base as *T, len: self.len })
raw::slice_from_buf(self.base as *T, self.len)
}
}

/// View the stored data as a mutable slice.
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
unsafe {
cast::transmute(raw::Slice { data: self.base as *T, len: self.len })
raw::mut_slice_from_buf(self.base as *mut T, self.len)
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/libstd/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn from_u32(i: u32) -> Option<char> {
if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) {
None
} else {
Some(unsafe { transmute(i) })
Some(unsafe { transmute::<u32, char>(i) })
}
}

Expand Down Expand Up @@ -277,9 +277,9 @@ pub fn from_digit(num: uint, radix: uint) -> Option<char> {
if num < radix {
unsafe {
if num < 10 {
Some(transmute(('0' as uint + num) as u32))
Some(transmute::<u32, char>(('0' as uint + num) as u32))
} else {
Some(transmute(('a' as uint + num - 10u) as u32))
Some(transmute::<u32, char>(('a' as uint + num - 10u) as u32))
}
}
} else {
Expand All @@ -304,14 +304,14 @@ fn decompose_hangul(s: char, f: |char|) {

let li = si / N_COUNT;
unsafe {
f(transmute((L_BASE + li) as u32));
f(transmute::<u32, char>((L_BASE + li) as u32));

let vi = (si % N_COUNT) / T_COUNT;
f(transmute((V_BASE + vi) as u32));
f(transmute::<u32, char>((V_BASE + vi) as u32));

let ti = si % T_COUNT;
if ti > 0 {
f(transmute((T_BASE + ti) as u32));
f(transmute::<u32, char>((T_BASE + ti) as u32));
}
}
}
Expand Down Expand Up @@ -355,8 +355,8 @@ pub fn escape_unicode(c: char, f: |char|) {
for offset in range_step::<i32>(4 * (pad - 1), -1, -4) {
unsafe {
match ((c as i32) >> offset) & 0xf {
i @ 0 .. 9 => { f(transmute('0' as i32 + i)); }
i => { f(transmute('a' as i32 + (i - 10))); }
i @ 0 .. 9 => { f(transmute::<i32, char>('0' as i32 + i)); }
i => { f(transmute::<i32, char>('a' as i32 + (i - 10))); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could all the transmutes in this module be contained in a helper function?

unsafe fn u32_to_char(i: u32) -> char { cast::transmute(i) }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsafe fn from_u32_unchecked would fit the name of the safe from_u32.

}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ mod tests {
fn test_get_ptr() {
unsafe {
let x = ~0;
let addr_x: *int = ::cast::transmute(&*x);
let addr_x: *int = &*x as *int;
let opt = Some(x);
let y = opt.unwrap();
let addr_y: *int = ::cast::transmute(&*y);
let addr_y: *int = &*y as *int;
assert_eq!(addr_x, addr_y);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/libstd/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ impl<T> Repr<*Box<T>> for @T {}
impl<T> Repr<*Vec<T>> for ~[T] {}
impl Repr<*String> for ~str {}

pub unsafe fn slice_from_buf<'a, T>(data: *T, len: uint) -> &'a [T] {
cast::transmute(Slice { data: data, len: len })
}

pub unsafe fn mut_slice_from_buf<'a, T>(data: *mut T, len: uint) -> &'a mut [T] {
cast::transmute(Slice { data: data as *T, len: len })
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the whole point of these structs were to avoid having helpers like these?

I don't think we're gaining much by just moving the transmutes into this module

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I pushed this a bit prematurely. I had reset most of the
accidental float-related transmutes. The related issue is
#6790 though.

On Tue, Mar 18, 2014 at 5:06 PM, Alex Crichton [email protected]:

In src/libstd/raw.rs:

@@ -82,6 +82,14 @@ impl Repr<_Box> for @t {}
impl Repr<_Vec> for ~[T] {}
impl Repr<*String> for ~str {}

+pub unsafe fn slice_from_buf<'a, T>(data: *T, len: uint) -> &'a [T] {

  • cast::transmute(Slice { data: data, len: len })
    +}

+pub unsafe fn mut_slice_from_buf<'a, T>(data: *mut T, len: uint) -> &'a mut [T] {

  • cast::transmute(Slice { data: data as *T, len: len })
    +}

I thought the whole point of these structs were to avoid having helpers
like these?

I don't think we're gaining much by just moving the transmutes into this
module


Reply to this email directly or view it on GitHubhttps://github.com//pull/13007/files#r10725740
.

http://octayn.net/


#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/unstable/dynamic_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl DynamicLibrary {
// the destructor does not run.
match maybe_symbol_value {
Err(err) => Err(err),
Ok(symbol_value) => Ok(cast::transmute(symbol_value))
Ok(symbol_value) => Ok(cast::transmute::<*u8, T>(symbol_value))
}
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub fn build<A>(size: Option<uint>, builder: |push: |v: A||) -> ~[A] {
*/
pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] {
unsafe {
transmute(Slice { data: s, len: 1 })
::raw::slice_from_buf(s as *A, 1)
}
}

Expand All @@ -225,8 +225,7 @@ pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] {
*/
pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
unsafe {
let ptr: *A = transmute(s);
transmute(Slice { data: ptr, len: 1 })
::raw::mut_slice_from_buf(s as *mut A, 1)
}
}

Expand Down Expand Up @@ -991,10 +990,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
assert!(start <= end);
assert!(end <= self.len());
unsafe {
transmute(Slice {
data: self.as_ptr().offset(start as int),
len: (end - start)
})
::raw::slice_from_buf(self.as_ptr().offset(start as int), end - start)
}
}

Expand Down Expand Up @@ -1473,8 +1469,7 @@ impl<T> OwnedVector<T> for ~[T] {
#[inline]
fn capacity(&self) -> uint {
unsafe {
let repr: **Vec<()> = transmute(self);
(**repr).alloc / mem::nonzero_size_of::<T>()
(*self.repr()).alloc / mem::nonzero_size_of::<T>()
}
}

Expand Down