cluFullTransmute

A more complete and advanced version of data transmutation without restrictions.
Opportunities
- Casting any type
A to any type B without checking the dimensionality of the data.
- The ability to use transmutation in constant functions.
- Possibility of delayed transmutation.
- Possibility of work in #![no_std]
Attention!
- This library only works in a nightly compiler.
- You really need to understand what you are doing.
Use
1. GenericType
use cluFullTransmute::mem::full_transmute;
struct A<T>(T);
impl<T> Drop for A<T> {
fn drop(&mut self) {
panic!("Strange behavior of the internal library.");
}
}
struct B<T>(T);
impl<T> B<T> {
pub fn my_fn(&self) {}
}
fn main() {
let data = A(9999usize);
let b: B<usize> = unsafe { full_transmute(data) };
assert_eq!(b.0, 9999usize);
b.my_fn();
}
2. Easy
use cluFullTransmute::mem::full_transmute;
fn main() {
let a: bool = unsafe { full_transmute(1u8) };
assert_eq!(a, true);
let b: bool = unsafe { full_transmute(0u8) };
assert_eq!(b, false);
assert_eq!(std::mem::size_of::<bool>(), 1);
}
3. MaybeTransmute
use cluFullTransmute::mem::MaybeTransmute;
struct MyData {
data: MaybeTransmute<String, Vec<u8>>,
}
impl MyData {
#[inline]
pub fn new<I: Into<String>>(t: I) -> Self {
Self::__new(t.into())
}
#[inline]
const fn __new(t: String) -> Self {
Self {
data: MaybeTransmute::new(t),
}
}
#[inline]
pub fn as_string(&mut self) -> &mut String {
&mut self.data
}
#[inline]
pub fn into(self) -> Vec<u8> {
unsafe { self.data.into() }
}
}
fn main() {
let mut data = MyData::new("Test");
assert_eq!(data.as_string().as_bytes(), b"Test");
assert_eq!(data.as_string(), "Test");
let vec = data.into();
assert_eq!(vec, b"Test");
}
License
Copyright 2019-2020 #UlinProject Denis Kotlyarov (Денис Котляров)
Licensed under the Apache License, Version 2.0