2 个版本
0.3.3 | 2024年3月30日 |
---|---|
0.3.2 | 2024年3月30日 |
0.3.1 |
|
0.3.0 |
|
#645 在 开发工具
11KB
141 行
用法
将此添加到您的 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) 双重许可证。