jswrenn / typic

Type-safe transmutations between layout-compatible types.
https://crates.io/crates/typic
Apache License 2.0
121 stars 4 forks source link

Fix Transparency #3

Closed jswrenn closed 4 years ago

jswrenn commented 4 years ago

A struct should not be Transparent if it has a field that is also not Transparent:

#[typic::repr(C)]
pub struct A(u8);

assert_not_impl_any!(A: Transparent);

#[typic::repr(C)]
pub struct B(pub A);

let _: B = 0u8.transmute_into(); // This *should* fail.

However, the correct behavior isn't quite as simple as usual transitive trait derive (e.g., derive(Clone) and derive(Copy)). For instance:

#[typic::repr(C, u8)]
enum Foo {
  A(pub u8),
  B(pub u8),
}

#[typic::repr(C, u8)]
enum Bar {
  A(pub u8),
  B(pub u8),
  C(u8)
}

let _ : Bar = Foo::A(42).transmute_into(); // This *should* work.

To me, this implies that transparency should be part of the layout information at both low and high levels.