Guest User

Untitled

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