Safety of transmute

⚓ Rust    📅 2025-12-18    👤 surdeus    👁️ 1      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Safety of transmute

Is this code safe?

use std::marker::PhantomData;
struct Foo<T> {
    bar: u8,
    baz: PhantomData<T>,
}
impl<T> Foo<T> {
    fn new(bar: u8) -> Self {
        Self { bar, baz: PhantomData }
    }
}

fn main() {
    let v = Foo::<&mut i32>::new(4);
    let x = unsafe {
        // don't focus on i32 and bool, I've chosen two random types
        std::mem::transmute::<Foo<&mut i32>, Foo<&mut bool>()
    };
}

3 posts - 3 participants

Read full topic

🏷️ Rust_feed