#data-transformation #transmute #clucompany #no-std #unsafe-transmute

无std cluFullTransmute

具有和没有约束检查的数据转换的更完整和扩展版本

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

Download history 141/week @ 2024-05-11 12/week @ 2024-05-18 1/week @ 2024-05-25 4/week @ 2024-06-01 1/week @ 2024-06-08 29/week @ 2024-06-29 49/week @ 2024-07-27

546 每月下载量
用于 cluconstdata

Apache-2.0

30KB
492

[clufulltransmute]

(无约束检查的数据类型转换的更完整和扩展版本。)

!!! 注意 !!!

  1. 在转换类型时不检查数据大小,你真的需要明白你在做什么。
  2. 你必须了解你使用的平台的具体情况。

库功能

  1. 无数据维度检查地使用泛型数据将任何类型A转换为任何类型B。
  2. 能够在非常旧的Rust版本中的常量函数中使用转换。
  3. 可以通过合约实现延迟转换。
  4. 能够在没有标准库的情况下工作。

用法

将此添加到您的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)。

uproject  版权 (c) 2019-2024 #UlinProject

 (Denis Kotlyarov).


Apache许可证

apache2  根据Apache License, Version 2.0授权。



无运行时依赖

特性