Skip to content

Commit 4ca4a2c

Browse files
committed
Wipe the transmutes off the libstd’s face
1 parent b4e96a4 commit 4ca4a2c

File tree

11 files changed

+36
-37
lines changed

11 files changed

+36
-37
lines changed

src/liballoc/sync.rs

+7
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ impl<T: ?Sized> Arc<T> {
517517
unsafe { self.ptr.as_ref() }
518518
}
519519

520+
#[inline]
521+
pub(crate) unsafe fn into_ptr<U>(self) -> NonNull<U> {
522+
let ptr = self.ptr;
523+
mem::forget(self);
524+
ptr.cast()
525+
}
526+
520527
// Non-inlined part of `drop`.
521528
#[inline(never)]
522529
unsafe fn drop_slow(&mut self) {

src/liballoc/task.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ pub use self::if_arc::*;
1919
mod if_arc {
2020
use super::*;
2121
use core::marker::PhantomData;
22-
use core::mem;
23-
use core::ptr::{self, NonNull};
2422
use sync::Arc;
23+
use core::ptr;
2524

2625
/// A way of waking up a specific task.
2726
///
@@ -83,9 +82,9 @@ mod if_arc {
8382
{
8483
fn from(rc: Arc<T>) -> Self {
8584
unsafe {
86-
let ptr = mem::transmute::<Arc<T>, NonNull<ArcWrapped<T>>>(rc);
87-
Waker::new(ptr)
85+
Waker::new(rc.into_ptr::<ArcWrapped<T>>())
8886
}
87+
8988
}
9089
}
9190

@@ -96,8 +95,7 @@ mod if_arc {
9695
/// will call `wake.wake()` if awoken after being converted to a `Waker`.
9796
#[inline]
9897
pub unsafe fn local_waker<W: Wake + 'static>(wake: Arc<W>) -> LocalWaker {
99-
let ptr = mem::transmute::<Arc<W>, NonNull<ArcWrapped<W>>>(wake);
100-
LocalWaker::new(ptr)
98+
LocalWaker::new(wake.into_ptr::<ArcWrapped<W>>())
10199
}
102100

103101
struct NonLocalAsLocal<T>(ArcWrapped<T>);
@@ -133,8 +131,7 @@ mod if_arc {
133131
#[inline]
134132
pub fn local_waker_from_nonlocal<W: Wake + 'static>(wake: Arc<W>) -> LocalWaker {
135133
unsafe {
136-
let ptr = mem::transmute::<Arc<W>, NonNull<NonLocalAsLocal<W>>>(wake);
137-
LocalWaker::new(ptr)
134+
LocalWaker::new(wake.into_ptr::<NonLocalAsLocal<W>>())
138135
}
139136
}
140137
}

src/libcore/str/lossy.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use char;
1212
use str as core_str;
1313
use fmt;
1414
use fmt::Write;
15-
use mem;
1615

1716
/// Lossy UTF-8 string.
1817
#[unstable(feature = "str_internals", issue = "0")]
@@ -26,7 +25,7 @@ impl Utf8Lossy {
2625
}
2726

2827
pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
29-
unsafe { mem::transmute(bytes) }
28+
unsafe { &*(bytes as *const [u8] as *const Utf8Lossy) }
3029
}
3130

3231
pub fn chunks(&self) -> Utf8LossyChunksIter {

src/libpanic_unwind/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern crate unwind;
5151

5252
use alloc::boxed::Box;
5353
use core::intrinsics;
54-
use core::mem;
54+
use core::any::Any;
5555
use core::raw;
5656
use core::panic::BoxMeUp;
5757

@@ -105,7 +105,8 @@ pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
105105
if intrinsics::try(f, data, &mut payload as *mut _ as *mut _) == 0 {
106106
0
107107
} else {
108-
let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
108+
let raw = &Box::into_raw(imp::cleanup(payload));
109+
let obj = *(raw as *const *mut (Any + Send) as *const raw::TraitObject);
109110
*data_ptr = obj.data as usize;
110111
*vtable_ptr = obj.vtable as usize;
111112
1

src/libstd/error.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use cell;
3030
use char;
3131
use core::array;
3232
use fmt::{self, Debug, Display};
33-
use mem::transmute;
3433
use num;
3534
use str;
3635
use string;
@@ -483,7 +482,7 @@ impl Error + Send {
483482
let err: Box<Error> = self;
484483
<Error>::downcast(err).map_err(|s| unsafe {
485484
// reapply the Send marker
486-
transmute::<Box<Error>, Box<Error + Send>>(s)
485+
Box::from_raw(Box::into_raw(s) as *mut (Error + Send))
487486
})
488487
}
489488
}
@@ -497,7 +496,7 @@ impl Error + Send + Sync {
497496
let err: Box<Error> = self;
498497
<Error>::downcast(err).map_err(|s| unsafe {
499498
// reapply the Send+Sync marker
500-
transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
499+
Box::from_raw(Box::into_raw(s) as *mut (Error + Send + Sync))
501500
})
502501
}
503502
}

src/libstd/panicking.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
297297
} else {
298298
update_panic_count(-1);
299299
debug_assert!(update_panic_count(0) == 0);
300-
Err(mem::transmute(raw::TraitObject {
300+
Err(Box::from_raw(*(&raw::TraitObject {
301301
data: any_data as *mut _,
302302
vtable: any_vtable as *mut _,
303-
}))
303+
} as *const _ as *const *mut _)))
304304
};
305305

306306
fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {

src/libstd/sync/mpsc/blocking.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use thread::{self, Thread};
1414
use sync::atomic::{AtomicBool, Ordering};
1515
use sync::Arc;
16-
use mem;
1716
use time::Instant;
1817

1918
struct Inner {
@@ -64,14 +63,14 @@ impl SignalToken {
6463
/// flag.
6564
#[inline]
6665
pub unsafe fn cast_to_usize(self) -> usize {
67-
mem::transmute(self.inner)
66+
Arc::into_raw(self.inner) as usize
6867
}
6968

7069
/// Convert from an unsafe usize value. Useful for retrieving a pipe's state
7170
/// flag.
7271
#[inline]
7372
pub unsafe fn cast_from_usize(signal_ptr: usize) -> SignalToken {
74-
SignalToken { inner: mem::transmute(signal_ptr) }
73+
SignalToken { inner: Arc::from_raw(signal_ptr as *const _) }
7574
}
7675
}
7776

src/libstd/sys/unix/ext/ffi.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#![stable(feature = "rust1", since = "1.0.0")]
1414

1515
use ffi::{OsStr, OsString};
16-
use mem;
1716
use sys::os_str::Buf;
1817
use sys_common::{FromInner, IntoInner, AsInner};
1918

@@ -111,7 +110,7 @@ pub trait OsStrExt {
111110
#[stable(feature = "rust1", since = "1.0.0")]
112111
impl OsStrExt for OsStr {
113112
fn from_bytes(slice: &[u8]) -> &OsStr {
114-
unsafe { mem::transmute(slice) }
113+
unsafe { &*(slice as *const [u8] as *const OsStr) }
115114
}
116115
fn as_bytes(&self) -> &[u8] {
117116
&self.as_inner().inner

src/libstd/sys/unix/ext/net.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl SocketAddr {
211211

212212
fn address<'a>(&'a self) -> AddressKind<'a> {
213213
let len = self.len as usize - sun_path_offset();
214-
let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
214+
let path = unsafe { &*(&self.addr.sun_path as *const [libc::c_char] as *const [u8]) };
215215

216216
// macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
217217
if len == 0

src/libstd/sys/unix/os_str.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use borrow::Cow;
1515
use fmt;
1616
use str;
17-
use mem;
1817
use rc::Rc;
1918
use sync::Arc;
2019
use sys_common::{AsInner, IntoInner};
@@ -110,7 +109,7 @@ impl Buf {
110109
}
111110

112111
pub fn as_slice(&self) -> &Slice {
113-
unsafe { mem::transmute(&*self.inner) }
112+
Slice::from_u8_slice(&*self.inner)
114113
}
115114

116115
pub fn into_string(self) -> Result<String, Buf> {
@@ -123,12 +122,12 @@ impl Buf {
123122

124123
#[inline]
125124
pub fn into_box(self) -> Box<Slice> {
126-
unsafe { mem::transmute(self.inner.into_boxed_slice()) }
125+
unsafe { Box::from_raw(Box::into_raw(self.inner.into_boxed_slice()) as *mut Slice) }
127126
}
128127

129128
#[inline]
130129
pub fn from_box(boxed: Box<Slice>) -> Buf {
131-
let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
130+
let inner: Box<[u8]> = unsafe { Box::from_raw(&mut (*Box::into_raw(boxed)).inner) };
132131
Buf { inner: inner.into_vec() }
133132
}
134133

@@ -145,7 +144,7 @@ impl Buf {
145144

146145
impl Slice {
147146
fn from_u8_slice(s: &[u8]) -> &Slice {
148-
unsafe { mem::transmute(s) }
147+
unsafe { &*(s as *const _ as *const Slice) }
149148
}
150149

151150
pub fn from_str(s: &str) -> &Slice {
@@ -167,12 +166,12 @@ impl Slice {
167166
#[inline]
168167
pub fn into_box(&self) -> Box<Slice> {
169168
let boxed: Box<[u8]> = self.inner.into();
170-
unsafe { mem::transmute(boxed) }
169+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
171170
}
172171

173172
pub fn empty_box() -> Box<Slice> {
174173
let boxed: Box<[u8]> = Default::default();
175-
unsafe { mem::transmute(boxed) }
174+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
176175
}
177176

178177
#[inline]

src/libstd/sys_common/wtf8.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use char;
3232
use fmt;
3333
use hash::{Hash, Hasher};
3434
use iter::FromIterator;
35-
use mem;
3635
use ops;
3736
use rc::Rc;
3837
use slice;
@@ -366,12 +365,12 @@ impl Wtf8Buf {
366365
/// Converts this `Wtf8Buf` into a boxed `Wtf8`.
367366
#[inline]
368367
pub fn into_box(self) -> Box<Wtf8> {
369-
unsafe { mem::transmute(self.bytes.into_boxed_slice()) }
368+
unsafe { Box::from_raw(Box::into_raw(self.bytes.into_boxed_slice()) as *mut Wtf8) }
370369
}
371370

372371
/// Converts a `Box<Wtf8>` into a `Wtf8Buf`.
373372
pub fn from_box(boxed: Box<Wtf8>) -> Wtf8Buf {
374-
let bytes: Box<[u8]> = unsafe { mem::transmute(boxed) };
373+
let bytes: Box<[u8]> = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [u8]) };
375374
Wtf8Buf { bytes: bytes.into_vec() }
376375
}
377376
}
@@ -493,7 +492,7 @@ impl Wtf8 {
493492
/// marked unsafe.
494493
#[inline]
495494
unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 {
496-
mem::transmute(value)
495+
&*(value as *const _ as *const Wtf8)
497496
}
498497

499498
/// Creates a mutable WTF-8 slice from a mutable WTF-8 byte slice.
@@ -502,7 +501,7 @@ impl Wtf8 {
502501
/// marked unsafe.
503502
#[inline]
504503
unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Wtf8 {
505-
mem::transmute(value)
504+
&mut *(value as *mut _ as *mut Wtf8)
506505
}
507506

508507
/// Returns the length, in WTF-8 bytes.
@@ -651,13 +650,13 @@ impl Wtf8 {
651650
#[inline]
652651
pub fn into_box(&self) -> Box<Wtf8> {
653652
let boxed: Box<[u8]> = self.bytes.into();
654-
unsafe { mem::transmute(boxed) }
653+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Wtf8) }
655654
}
656655

657656
/// Creates a boxed, empty `Wtf8`.
658657
pub fn empty_box() -> Box<Wtf8> {
659658
let boxed: Box<[u8]> = Default::default();
660-
unsafe { mem::transmute(boxed) }
659+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Wtf8) }
661660
}
662661

663662
#[inline]

0 commit comments

Comments
 (0)