A more complete and extended version of data type conversion without constraint checks.
```rust /* This example is notable because by the Rust standard it does not allow converting common types A to B, since it cannot check their sizes, this example solves this.
Additionally, as an example, we manipulate the Drop::drop function.
*/
use cluFullTransmute::mem::force_transmute;
struct A
impl
impl
impl
}
}
fn main() { let data = A(9999usize); // We expect panic at the death of A.
let b: B<usize> = unsafe { force_transmute(data) }; // type A no longer exists, it is now type B.
assert_eq!(b.0, 9999usize); // Checking the value
b.my_fn();
drop(b);
// That's it, no panic, type B.
} ```
```rust use cluFullTransmute::mem::contract::DataTransmutContract;
/*
For example, we will sign a contract to convert a String to a Vec
Contracts are needed to create more secure APIs using transmutation in
situations where it can't be proven.
*/
///
struct MyData {
data: DataTransmutContract
impl MyData {
#[inline]
pub fn new
#[inline]
const fn __new(data: String) -> Self {
let data = unsafe {
// DataTransmutContract::force_new
//
// The `checksize_new_or_panic` function can only guarantee equality of data
// dimensions, creating a contract is always unsafe, since the transmutation
// of such data types can only be proven orally. But after signing the
// transmutation contract, all functions for working with the transmuted are
// not marked as unsafe.
//
DataTransmutContract::checksize_new_or_panic(data)
};
Self {
data,
}
}
#[inline]
pub fn as_string(&mut self) -> &mut String {
&mut self.data
}
#[inline]
pub fn into(self) -> Vec<u8> {
self.data.into()
}
}
fn main() { // String let mut data = MyData::new("Test"); asserteq!(data.asstring().asbytes(), b"Test"); asserteq!(data.as_string(), "Test"); //
let vec = data.into(); // String -> Vec<u8>
assert_eq!(vec, b"Test");
} ```
Copyright 2022 #UlinProject Denis Kotlyarov (Денис Котляров)
Licensed under the Apache License, Version 2.0