typic 0.2.2

Type-safe transmutations between layout-compatible types.
Documentation
typic-0.2.2 has been yanked.

Typic helps you transmute fearlessly. It worries about the subtleties of soundness and safety so you don't have to!

Just import it and replace your #[repr(...)] attributes with #[typic::repr(...)]:

// Import it!
use typic::{self, TransmuteInto};

// Update your attributes!
#[typic::repr(C)]
pub struct Foo(pub u8, pub u16);

// Transmute fearlessly!
let _ : Foo = 64u32.transmute_into(); // Alchemy achieved!
# use typic::{self, TransmuteInto};
# #[typic::repr(C)]
# struct Foo(pub u8, pub u16);
let _ : u32 = Foo(16, 12).transmute_into(); // Compiler Error!

Three Types of Transmutation

Unsound Transmutation

The transmute and transmute_copy intrinsics allow for the unsafe and unsound transmutation between any T and U.

These intrinsics are deeply unsafe. The Rust compiler will accept uses of these intrinsics even when T and U do not have well-defined layouts. Always use a safe transmutation method instead, if possible. If you are unable to use a safe transmutation method, you may be relying on undefined compiler behavior.

Sound Transmutation

The sound_transmute function allows for the unsafe transmutation between T and U, when merely transmuting from T to U will not cause undefined behavior. For the key rules that govern when T is soundly convertible to U, see When is a transmutation sound?.

This operation is unsafe, as it will bypass any user-defined validity restrictions that U places on its fields and enforces with its constructors and methods.

Always use a safe transmutation method instead, if possible. If you are unable to use a safe transmutation method, you may be violating library invariants.

Safe Transmutation

The TransmuteInto<U> trait is implemented for a type T if:

  1. T is soundly transmutable into U, and
  2. T is safely transmutable into U.

If you are unable to use TransmuteInto<U>, you may be attempting a transmutation that is relying unspecified behavior.