Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #![allow(dead_code)]
  2.  
  3. // A custom trait
  4. trait MyTrait {
  5. fn print();
  6. }
  7.  
  8. macro_rules! impl_my_trait {
  9. ($type:ident, $($tt:ident),*) => {
  10. impl<$type, $($tt),*> MyTrait for ($type, $($tt),*)
  11. where $type: MyTrait, $($tt: MyTrait),* {
  12. fn print() {
  13. $type::print();
  14. $($tt::print();)*
  15. }
  16. }
  17. impl_my_trait!($($tt),*);
  18. };
  19. ( $($tt:tt),* ) => {
  20. impl MyTrait for () {
  21. fn print() {}
  22. }
  23. }
  24. }
  25. // Implement the trait for 0 to 10 elements tuples.
  26. impl_my_trait!(T00, T01, T02, T03, T04, T05, T06, T07, T08, T09);
  27.  
  28. // struct using the trait
  29. macro_rules! impl_traits {
  30. ($type:ident, $($tt:ident),*) => {
  31. struct $type {}
  32. impl MyTrait for $type {
  33. fn print() { print!(stringify!($type)) }
  34. }
  35. impl_traits!($($tt),*);
  36. };
  37. ( $($tt:tt),* ) => { }
  38. }
  39. impl_traits!(T00, T01, T02, T03, T04, T05, T06, T07, T08, T09);
  40.  
  41. fn main() {
  42. <(T00, T01, T02)>::print();
  43. println!("");
  44. println!("----");
  45. <(T00, T01, T00)>::print(); // how to have a compile error here?
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement