8个稳定版本
1.3.0 | 2024年5月12日 |
---|---|
1.2.0 | 2022年8月15日 |
1.1.0 | 2022年6月2日 |
1.0.6 | 2022年4月7日 |
1.0.1 | 2019年8月30日 |
#2 在 #clucompany
546 每月下载量
用于 cluconstdata
30KB
492 行
!!! 注意 !!!
- 在转换类型时不检查数据大小,你真的需要明白你在做什么。
- 你必须了解你使用的平台的具体情况。
库功能
- 无数据维度检查地使用泛型数据将任何类型A转换为任何类型B。
- 能够在非常旧的Rust版本中的常量函数中使用转换。
- 可以通过合约实现延迟转换。
- 能够在没有标准库的情况下工作。
用法
将此添加到您的Cargo.toml中
[dependencies]
cluFullTransmute = "1.3.0"
并将此添加到您的源代码中
use cluFullTransmute::mem::transmute;
示例
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!
}
查看全部
许可证
本项目有一个单独的许可证(LICENSE-APACHE-2.0)。