Skip to content

Commit aef008a

Browse files
authored
Rollup merge of #110094 - lukas-code:less-transmute, r=thomcc
clean up `transmute`s in `core` * Use `transmute_unchecked` instead of `transmute_copy` for `MaybeUninit::transpose`. * Use manual transmute for `Option<Ordering>` → `i8`.
2 parents 5859771 + 7e3b934 commit aef008a

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

library/core/src/mem/maybe_uninit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ impl<T, const N: usize> MaybeUninit<[T; N]> {
12871287
#[inline]
12881288
pub const fn transpose(self) -> [MaybeUninit<T>; N] {
12891289
// SAFETY: T and MaybeUninit<T> have the same layout
1290-
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
1290+
unsafe { intrinsics::transmute_unchecked(self) }
12911291
}
12921292
}
12931293

@@ -1307,6 +1307,6 @@ impl<T, const N: usize> [MaybeUninit<T>; N] {
13071307
#[inline]
13081308
pub const fn transpose(self) -> MaybeUninit<[T; N]> {
13091309
// SAFETY: T and MaybeUninit<T> have the same layout
1310-
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
1310+
unsafe { intrinsics::transmute_unchecked(self) }
13111311
}
13121312
}

library/core/src/tuple.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// See src/libstd/primitive_docs.rs for documentation.
22

33
use crate::cmp::Ordering::{self, *};
4-
use crate::mem::transmute;
54

65
// Recursive macro for implementing n-ary tuple functions and operations
76
//
@@ -142,16 +141,13 @@ macro_rules! maybe_tuple_doc {
142141
#[inline]
143142
const fn ordering_is_some(c: Option<Ordering>, x: Ordering) -> bool {
144143
// FIXME: Just use `==` once that's const-stable on `Option`s.
145-
// This isn't using `match` because that optimizes worse due to
146-
// making a two-step check (`Some` *then* the inner value).
147-
148-
// SAFETY: There's no public guarantee for `Option<Ordering>`,
149-
// but we're core so we know that it's definitely a byte.
150-
unsafe {
151-
let c: i8 = transmute(c);
152-
let x: i8 = transmute(Some(x));
153-
c == x
154-
}
144+
// This is mapping `None` to 2 and then doing the comparison afterwards
145+
// because it optimizes better (`None::<Ordering>` is represented as 2).
146+
x as i8
147+
== match c {
148+
Some(c) => c as i8,
149+
None => 2,
150+
}
155151
}
156152

157153
// Constructs an expression that performs a lexical ordering using method `$rel`.

0 commit comments

Comments
 (0)