[−][src]Crate typic
Typic helps you transmute more safely. 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 = u32::default().transmute_into(); // Alchemy achieved!
let _ : u32 = Foo::default().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:
If you are unable to use TransmuteInto<U>, you may be attempting a
transmutation that is relying unspecified behavior.
Modules
| safe | Guidance and tools for safe transmutation. |
| sound | Guidance and tools for sound transmutation. |
Traits
| TransmuteFrom | A safe and sound value-to-value conversion.
The opposite of |
| TransmuteInto | A safe and sound value-to-value conversion.
The opposite of |
| Transparent | Indicates a type has no internal validity requirements. |
Functions
| transmute_safe | A safe and sound value-to-value conversion. |
| transmute_sound⚠ | A sound value-to-value conversion. |
Attribute Macros
| repr | Use |