#impl-block #macro #clucompany #code-transformation #no-std

no-std copy_impl

一个宏,用于在 Rust 中轻松复制 impl 块代码到不同类型

2 个版本

0.3.3 2024年3月30日
0.3.2 2024年3月30日
0.3.1 2024年3月30日
0.3.0 2024年3月30日

#645开发工具

MIT/Apache

11KB
141

[copy_impl]

(一个宏,用于在 Rust 中轻松复制 impl 块代码到不同类型。)

用法

将此添加到您的 Cargo.toml 中

[dependencies]
copy_impl = "0.3.3"

并将此添加到您的源代码中

use copy_impl::copy_impl;

示例

use std::{error::Error, fmt::Write};
use copy_impl::copy_impl;

struct CustomNum<T>(T);
struct UncheckedCustomNum<T>(T);

copy_impl! {
	impl (CustomNum<i8>),
	impl (CustomNum<i16>),
	impl (CustomNum<i32>),
	impl (UncheckedCustomNum<i8>),
	impl (UncheckedCustomNum<i16>) {
		pub fn write_to(&self, mut w: impl Write) -> Result<(), std::fmt::Error> {
			write!(w, "{}", self.0)
		}
	}
}

fn main() -> Result<(), Box<dyn Error>> {
	let mut tbuff = String::new();
	CustomNum(1i8).write_to(&mut tbuff)?;
	CustomNum(2i16).write_to(&mut tbuff)?;
	CustomNum(3i32).write_to(&mut tbuff)?;
	
	UncheckedCustomNum(4i8).write_to(&mut tbuff)?;
	UncheckedCustomNum(5i16).write_to(&mut tbuff)?;
	// UncheckedCustomNum(6i32).write_to(&mut tbuff)?;
	/*
		no method named `write_to` found for struct `UncheckedCustomNum<i32>` in the current scope
		the method was found for
		- `UncheckedCustomNum<i8>`
		- `UncheckedCustomNum<i16>`
	*/
	
	assert_eq!(tbuff, "12345");
	
	Ok(())
}
查看所有

许可证

本项目根据 (LICENSE-MIT) 和 (LICENSE-APACHE-2-0) 双重许可证。

uproject  版权所有 (c) 2021-2024 #UlinProject (Denis Kotlyarov)。



Apache 许可证

apache2  根据 Apache 许可证,版本 2.0 许可。



MIT 许可证

mit  根据 MIT 许可证许可。



无运行时依赖