copy_impl

Macro copy_impl 

Source
macro_rules! copy_impl {
    [ /* COLD_START */
		impl $(<$($p_impl:tt)*>)? ($($impl:tt)*) $(where ($($where:tt)*))?
		
		$($all:tt)*
	] => { ... };
    [ /* HOT_CONTINUE */
		[
			[
				/* COPY_IMPL_DATA */
				$($all_copy_impl_data:tt)+
			]
		]
		
		, impl $(<$($p_impl:tt)*>)? ($($impl:tt)*) $(where ($($where:tt)*))?
		
		$($all:tt)*
	] => { ... };
    [ /* skip (,) */
		[
			[
				/* COPY_IMPL_DATA */
				$($all_copy_impl_data:tt)*
			]
		]
		,
		
		$($all:tt)*
	] => { ... };
    [ // END HEADERS(IMPL), the CODE!
		[
			[ /* COPY_IMPL_DATA */
				$([
					[ $($p_impl:tt)* ]
					[ $($impl:tt)* ]
					[ $($where:tt)* ]
				])+
			]
		]
		{ $($code:tt)* }
		
		$(
			; $($all:tt)*
		)?
	] => { ... };
    [ $(;)? ] => { ... };
    [ /* UNK */ $($all:tt)+ ] => { ... };
}
Expand description

Macro for easily copying impl block code for different types.

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)
		}
	}
}