Skip to content

[stable] Macro for effortlessly duplicating impl block code across various types in Rust.

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
Apache-2.0
LICENSE_APACHE
MIT
LICENSE_MIT
Notifications You must be signed in to change notification settings

clucompany/copy_impl

[copy_impl]

(Macro for effortlessly duplicating impl block code across various types in Rust.)

Usage:

Add this to your Cargo.toml:

[dependencies]
copy_impl = "0.3.3"

and this to your source code:

use copy_impl::copy_impl;

Example:

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(())
}
See all

License:

This project has a dual license according to (LICENSE-MIT) and (LICENSE-APACHE-2-0).

uproject  Copyright (c) 2021-2024 #UlinProject

 (Denis Kotlyarov).


Apache License:

apache2  Licensed under the Apache License, Version 2.0.



MIT License:

mit  Licensed under the MIT License.



About

[stable] Macro for effortlessly duplicating impl block code across various types in Rust.

Topics

Resources

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
Apache-2.0
LICENSE_APACHE
MIT
LICENSE_MIT

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages