Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. macro_rules! indices {
  2. ([] $($rest:tt)*) =>
  3. { indices!($($rest)* []) };
  4. ([$x1:tt] $($rest:tt)*) =>
  5. { indices!($($rest)* [0]) };
  6. ([$x1:tt $x2:tt] $($rest:tt)*) =>
  7. { indices!($($rest)* [0 1]) };
  8. ([$x1:tt $x2:tt $x3:tt] $($rest:tt)*) =>
  9. { indices!($($rest)* [0 1 2]) };
  10. // etc
  11. ($cb:ident: $($args:tt)*) =>
  12. { $cb!( $($args)* ) };
  13. }
  14. macro_rules! defer {
  15. ($c:expr, $i:ident( $( $x:expr ),* )) => {{
  16. indices!([$(($x))*] defer_aux: $c, $i $( , $x )*; );
  17. }}
  18. }
  19. macro_rules! defer_aux {
  20. ($c:expr, $i:ident $(, $x:expr )*; [$( $xi:tt )*]) => {{
  21. let tup = ( $( $x, )* );
  22. $c.defer(move |c| $i(c $(, tup . $xi)*));
  23. }}
  24. }
  25. struct Core {}
  26. impl Core {
  27. fn defer<F>(&mut self, _f: F) where F: FnOnce(&mut Core) + 'static {}
  28. }
  29. fn test(_core: &mut Core, _v1: i32, _v2: i32) {}
  30. fn main() {
  31. let mut core = Core {};
  32. defer!(&mut core, test(1, 2));
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement