Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. trait TupleAppend<T> {
  2. type ResultType;
  3.  
  4. fn append(self, t: T) -> Self::ResultType;
  5. }
  6.  
  7. macro_rules! impl_tuple_append {
  8. (
  9. // Toss on variadic parameters to slightly reduce boilerplate.
  10. $(
  11. ( $($types:ident),* );
  12. )*
  13. ) => {
  14. $(
  15. impl<$($types,)* T> TupleAppend<T> for ($($types,)*) {
  16. // Note the trailing comma, ensuring we are always dealing
  17. // with a tuple and not a parenthesized type/expr.
  18. type ResultType = ($($types,)* T,);
  19.  
  20. fn append(self, t: T) -> Self::ResultType {
  21. // Reuse the type identifiers to destructure ourselves:
  22. let ($($types,)*) = self;
  23. // Create a new tuple with the original elements, plus the new one:
  24. ($($types,)* t,)
  25. }
  26. }
  27. )*
  28. };
  29. }
  30.  
  31. impl_tuple_append! {
  32. ();
  33. (_1);
  34. (_1, _2);
  35. (_1, _2, _3);
  36. (_1, _2, _3, _4);
  37. (_1, _2, _3, _4, _5);
  38. (_1, _2, _3, _4, _5, _6);
  39. (_1, _2, _3, _4, _5, _6, _7);
  40. (_1, _2, _3, _4, _5, _6, _7, _8);
  41. (_1, _2, _3, _4, _5, _6, _7, _8, _9);
  42. (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10);
  43. // Continue until the largest size you wish to support.
  44. // May be worth adding a build script to automate this.
  45. }
  46.  
  47. fn main() {
  48. let some_tuple: (i32, &str, bool) = (1, "Hello", true);
  49. println!("{:?}", some_tuple);
  50.  
  51. let with_world: (i32, &str, bool, &str) = some_tuple.append("World");
  52. println!("{:?}", with_world);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement