-
Notifications
You must be signed in to change notification settings - Fork 118
Closed as not planned
Description
Seems to work fine without Box as both types are sized (which requires alloc):
fn transmute<A, B>(obj: A) -> B {
use std::hint::black_box;
// The layout of `DummyEnum` is approximately
// DummyEnum {
// is_a_or_b: u8,
// data: usize,
// }
// Note that `data` is shared between `DummyEnum::A` and `DummyEnum::B`.
// This should hopefully be more reliable than spamming the stack with a value and hoping the memory
// is placed correctly by the compiler.
enum DummyEnum<A, B> {
A(Option<A>),
B(Option<B>),
}
#[inline(never)]
fn transmute_inner<A, B>(dummy: &mut DummyEnum<A, B>, obj: A) -> B {
let DummyEnum::B(ref_to_b) = dummy else {
unreachable!()
};
let ref_to_b = expand_mut(ref_to_b);
*dummy = DummyEnum::A(Some(obj));
black_box(dummy);
ref_to_b.take().unwrap()
}
transmute_inner(black_box(&mut DummyEnum::B(None)), obj)
}
Metadata
Metadata
Assignees
Labels
No labels