Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- // A custom trait
- trait MyTrait {
- fn print();
- }
- macro_rules! impl_my_trait {
- ($type:ident, $($tt:ident),*) => {
- impl<$type, $($tt),*> MyTrait for ($type, $($tt),*)
- where $type: MyTrait, $($tt: MyTrait),* {
- fn print() {
- $type::print();
- $($tt::print();)*
- }
- }
- impl_my_trait!($($tt),*);
- };
- ( $($tt:tt),* ) => {
- impl MyTrait for () {
- fn print() {}
- }
- }
- }
- // Implement the trait for 0 to 10 elements tuples.
- impl_my_trait!(T00, T01, T02, T03, T04, T05, T06, T07, T08, T09);
- // struct using the trait
- macro_rules! impl_traits {
- ($type:ident, $($tt:ident),*) => {
- struct $type {}
- impl MyTrait for $type {
- fn print() { print!(stringify!($type)) }
- }
- impl_traits!($($tt),*);
- };
- ( $($tt:tt),* ) => { }
- }
- impl_traits!(T00, T01, T02, T03, T04, T05, T06, T07, T08, T09);
- fn main() {
- <(T00, T01, T02)>::print();
- println!("");
- println!("----");
- <(T00, T01, T00)>::print(); // how to have a compile error here?
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement