-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
clean up some transmutes #13007
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} else { | ||
None | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) }) | ||
} | ||
} | ||
|
||
|
@@ -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 { | ||
|
@@ -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)); | ||
} | ||
} | ||
} | ||
|
@@ -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))); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 On Tue, Mar 18, 2014 at 5:06 PM, Alex Crichton [email protected]:
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
There was a problem hiding this comment.
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, whereastransmute
is just a bit-cast