Skip to content

Commit 61361bb

Browse files
committed
Use transmute_unchecked and make the types explicit in query type erasure
This doesn't really change anything, but makes the code a bit more explicit/readable.
1 parent a6dfd89 commit 61361bb

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

compiler/rustc_middle/src/query/erase.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::mir;
22
use crate::query::CyclePlaceholder;
33
use crate::traits;
44
use crate::ty::{self, Ty};
5-
use std::mem::{size_of, transmute_copy, MaybeUninit};
5+
use std::mem::{size_of, MaybeUninit};
6+
use std::intrinsics::transmute_unchecked;
67

78
#[derive(Copy, Clone)]
89
pub struct Erased<T: Copy> {
@@ -30,7 +31,7 @@ pub fn erase<T: EraseType>(src: T) -> Erase<T> {
3031

3132
Erased::<<T as EraseType>::Result> {
3233
// SAFETY: It is safe to transmute to MaybeUninit for types with the same sizes.
33-
data: unsafe { transmute_copy(&src) },
34+
data: unsafe { transmute_unchecked::<T, MaybeUninit<T::Result>>(src) },
3435
}
3536
}
3637

@@ -41,7 +42,7 @@ pub fn restore<T: EraseType>(value: Erase<T>) -> T {
4142
// SAFETY: Due to the use of impl Trait in `Erase` the only way to safely create an instance
4243
// of `Erase` is to call `erase`, so we know that `value.data` is a valid instance of `T` of
4344
// the right size.
44-
unsafe { transmute_copy(&value.data) }
45+
unsafe { transmute_unchecked::<MaybeUninit<T::Result>, T>(value.data) }
4546
}
4647

4748
impl<T> EraseType for &'_ T {

0 commit comments

Comments
 (0)