I almost got it to work, but field access requires unsafe.
#![feature(untagged_unions)]
use std::ops::Deref;
struct T {
_a: u32,
_b: u32,
}
impl T {
fn new(a: u32, b: u32) -> Self {
T { _a: a, _b: b }
}
}
impl Deref for T {
type Target = U;
fn deref(&self) -> &Self::Target {
unsafe { std::mem::transmute(self) }
}
}
struct A {
a: u32,
b: u32,
}
impl Deref for A {
type Target = u32;
fn deref(&self) -> &Self::Target {
&self.a
}
}
struct B {
a: u32,
b: u32,
}
impl Deref for B {
type Target = u32;
fn deref(&self) -> &Self::Target {
&self.b
}
}
union U {
a: A,
b: B,
}
fn main() {
let t = T::new(1, 2);
unsafe {
println!("{}", *t.a + *t.b);
}
}