#Transmuting between identical enums returns unit?

1 messages · Page 1 of 1 (latest)

willow dove
#
use std::mem;

#[derive(Debug)]
#[repr(C)]
enum Foo {
    A(u32),
    B(u32),
}

#[derive(Debug)]
#[repr(C)]
enum Bar {
    A(u32),
    B(u32),
}

fn main() {
    println!("value = {:?}", unsafe {
        mem::transmute::<Bar, Foo>(Bar::A(5));
    });
}
#

this outputs

#
value = ()
#

and also passes miri

#

?miri

use std::mem;

#[derive(Debug)]
#[repr(C)]
enum Foo {
    A(u32),
    B(u32),
}

#[derive(Debug)]
#[repr(C)]
enum Bar {
    A(u32),
    B(u32),
}

fn main() {
    println!("value = {:?}", unsafe {
        mem::transmute::<Bar, Foo>(Bar::A(5));
    });
}
oblique carbonBOT
#
warning: variants `A` and `B` are never constructed
 --> src/main.rs:6:5
  |
5 | enum Foo {
  |      --- variants in this enum
6 |     A(u32),
  |     ^
7 |     B(u32),
  |     ^
  |
  = note: `Foo` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

warning: variant `B` is never constructed
  --> src/main.rs:14:5
   |
12 | enum Bar {
   |      --- variant in this enum
13 |     A(u32),
14 |     B(u32),
   |     ^
   |
   = note: `Bar` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

value = ()
willow dove
#

so.... why!!??

drifting arrow
#

you have a semicolon after the transmute, so the value is discarded

willow dove