Guest User

Untitled

a guest
Jun 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. macro_rules! destructure_fields{
  2. ($matched:expr; $($variant:path),+ => $value:ident) => (
  3. match $matched {
  4. $($variant($value))|+ => $value
  5. }
  6. );
  7. ($matched:expr; $($variant:path),+ => $($values:ident),+) => (
  8. destructure_fields!(@create_match $matched, $($variant),+ => ($($values),+))
  9. );
  10. (@create_tuple ($($values:ident),+)) => {
  11. ($($values),+)
  12. };
  13. (@create_pattern $variant:path, ($($values:ident),+)) => {
  14. $variant($($values),+)
  15. };
  16. (@create_match $matched:expr, $($variant:path),+ => $value_tuple:tt) => (
  17. match $matched {
  18. $(destructure_fields!(@create_pattern $variant, $value_tuple) => (destructure_fields!(@create_tuple $value_tuple))),+
  19. }
  20. );
  21. }
  22.  
  23. enum Test<'a> {
  24. A(&'a str, usize),
  25. B(&'a str, usize),
  26. C(&'a str, usize),
  27. D(&'a str, usize)
  28. }
  29.  
  30. impl<'a> Test<'a> {
  31. fn get_content(&self) -> (&str, &usize) {
  32. destructure_fields!(self; Test::A, Test::B, Test::C, Test::D => string, number)
  33. }
  34. }
  35.  
  36. fn main() {
  37. let (string, number) = Test::B("This is a test!", 5).get_content();
  38. println!("{}{}", string, number);
  39. }
Add Comment
Please, Sign In to add comment