Expand description
A more complete and extended version of data type conversion without constraint checks.
§!!! ATTENTION !!!
- When converting types without checking the size of the data, you really need to understand what you are doing.
- You must understand the specifics of the platform you are using.
§Example
use cluFullTransmute::transmute_or_panic;
use core::fmt::Display;
/*
For example, let's write some code with a Drop trait that panics when dropped and
holds some data. We then transmute this data to another similar struct and check
that we have effectively overridden the Drop trait and have a different struct
with some data.
We can also remove the Drop trait altogether or do any number of other things.
*/
/// Struct to panic when dropped
#[derive(Debug)]
#[repr(transparent)]
struct PanicWhenDrop<T>(T);
impl<T> Drop for PanicWhenDrop<T> {
fn drop(&mut self) {
panic!("panic, discovered `drop(PanicWhenDrop);`");
}
}
/// Struct to print value when dropped
#[derive(Debug)]
#[repr(transparent)]
struct PrintlnWhenDrop<T: Display>(T)
where
T: Display;
impl<T> Drop for PrintlnWhenDrop<T>
where
T: Display,
{
fn drop(&mut self) {
println!("println: {}", self.0);
}
}
fn main() {
let a: PanicWhenDrop<u16> = PanicWhenDrop(1024);
println!("in a: {:?}", a);
let b: PrintlnWhenDrop<u16> = unsafe { transmute_or_panic(a as PanicWhenDrop<u16>) };
println!("out b: {:?}", b);
drop(b); // <--- drop, PrintlnWhenDrop!
}
§Library Features
- Casting any type A to any type B with generic data without and with data dimension checking.
- Ability to use transmutation in constant functions in very old versions of rust..
- Possibility of delayed transmutation through contracts.
- Ability to work without the standard library.
Re-exports§
pub use crate::raw::unchecked_transmute;
Modules§
- contract
contract
- Data Transformation Contract.
- err
support_size_check_transmute
- Error structure and error type with a detailed description of the cause.
- mem
compatible_stdapi
- Basic functions for dealing with memory.
- raw
- Reinterprets the bits of a value of one type as another type without checking.
- to
to
- A handy trait for converting any
T
to the desiredTo
without directly calling crate functions.
Functions§
- inline_
transmute_ ⚠or_ errresult inline
andsupport_size_check_transmute
- A inline constant function reinterprets the bits of a value of one type as another type.
- inline_
transmute_ ⚠or_ panic inline
andsupport_size_check_transmute
- A inline constant function reinterprets the bits of a value of one type as another type.
- transmute_
or_ ⚠errresult support_size_check_transmute
- A constant function reinterprets the bits of a value of one type as another type.
- transmute_
or_ ⚠panic support_size_check_transmute
- A constant function reinterprets the bits of a value of one type as another type.