7 stable releases
1.2.0 | Aug 15, 2022 |
---|---|
1.1.0 | Jun 2, 2022 |
1.0.6 | Apr 7, 2022 |
1.0.5 | Nov 15, 2021 |
1.0.1 | Aug 30, 2019 |
#3 in #transmute
102 downloads per month
Used in cluconstdata
19KB
281 lines
cluFullTransmute
A more complete and extended version of data type conversion without constraint checks.
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.
!!! 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.
Use
1. GenericType
use core::fmt::Display;
use cluFullTransmute::transmute::transmute_or_panic;
/// Implementation of a simple transmutation with a generic parameter inside.
#[derive(Debug)]
#[repr(transparent)]
struct A<T> {
#[allow(dead_code)]
data: T
}
impl<T> Drop for A<T> {
fn drop(&mut self) {
panic!("Invalid beh");
}
}
#[derive(Debug)]
#[repr(transparent)]
struct B<T> where T: Display {
data: T,
}
impl<T> Drop for B<T> where T: Display {
fn drop(&mut self) {
println!("{}", self.data);
}
}
fn main() {
let a: A<u16> = A { // original and panic when falling
data: 1024
};
println!("in: {:?}", a);
let b: B<u16> = unsafe { transmute_or_panic(a) };
println!("out: {:?}", b);
drop(b); // <--- println!
}
2. Contract
use cluFullTransmute::contract::Contract;
/*
For example, we will sign a contract to convert a String to a Vec<u8>,
although this may not be exactly the case.
Contracts are needed to create more secure APIs using transmutation in
situations where it can't be proven.
*/
///
struct MyData {
data: Contract<&'static str, &'static [u8]>,
}
impl MyData {
#[inline]
const fn new(data: &'static str) -> Self {
let data = unsafe {
// Contract::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.
//
Contract::checksize_new_or_panic(data)
};
Self {
data,
}
}
#[inline]
pub fn as_data(&self) -> &'static str {
&self.data
}
#[inline]
pub fn as_sliceu8(&self) -> &'static [u8] {
self.data.as_datato()
}
#[inline]
pub fn into(self) -> &'static [u8] {
self.data.into()
}
}
fn main() {
const C_DATA: &'static str = "Test";
// &'static str
let data = MyData::new(C_DATA);
assert_eq!(data.as_data(), C_DATA); // const_readtype: &'static str
assert_eq!(data.as_sliceu8(), C_DATA.as_bytes()); //const_readtype &'static [u8]
//
// &'static u8
let vec = data.into(); // const_transmute: &'static str -> &'static [u8]
assert_eq!(vec, C_DATA.as_bytes());
}
License
Copyright 2022 #UlinProject Denis Kotlyarov (Денис Котляров)
Licensed under the Apache License, Version 2.0
No runtime deps
Features
- compatible_stdapi
- contract
- support_size_check_transmute