Guest User

Untitled

a guest
Dec 10th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #![feature(pin)]
  2.  
  3. use std::pin::Pin;
  4.  
  5. macro_rules! pin_proj {
  6. (@record({}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  7. let &mut $st {$($p)*} = unsafe { Pin::get_mut_unchecked($e) };
  8. $(
  9. let $binders = unsafe { Pin::new_unchecked($binders) };
  10. )*
  11. };
  12. (@record({..}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  13. pin_proj! { @record({}, $st, $e, {$($p)* ..}, [$($binders)*]) }
  14. };
  15. (@record({$field:ident, $($rem:tt)*}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  16. pin_proj! { @record({$($rem)*}, $st, $e, {$($p)* ref mut $field,}, [$($binders)* $field]) }
  17. };
  18. (@record({$field:ident}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  19. pin_proj! { @record({}, $st, $e, {$($p)* ref mut $field}, [$($binders)* $field]) }
  20. };
  21. (@record({$field:ident : _, $($rem:tt)*}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  22. pin_proj! { @record({$($rem)*}, $st, $e, {$($p)* $field: _,}, [$($binders)*]) }
  23. };
  24. (@record({$field:ident : _}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  25. pin_proj! { @record({}, $st, $e, {$($p)* $field: _}, [$($binders)*]) }
  26. };
  27. (@record({$field:ident : $var:ident, $($rem:tt)*}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  28. pin_proj! { @record({$($rem)*}, $st, $e, {$($p)* $field: ref mut $var,}, [$($binders)* $var]) }
  29. };
  30. (@record({$field:ident : $var:ident}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
  31. pin_proj! { @record({}, $st, $e, {$($p)* $field: ref mut $var}, [$($binders)* $var]) }
  32. };
  33. (let $st:path {$($fields:tt)*} = $e:expr;) => {
  34. pin_proj! { @record({$($fields)*}, $st, $e, {}, []) }
  35. };
  36. }
  37.  
  38. pub struct A {}
  39.  
  40. pub fn test1(x: Pin<&mut A>) {
  41. pin_proj! {
  42. let A {} = x;
  43. }
  44. }
  45.  
  46. pub struct B {
  47. x: i32,
  48. y: i32,
  49. }
  50.  
  51. pub fn test2(x: Pin<&mut B>) {
  52. pin_proj! {
  53. let B { x, y: z } = x;
  54. }
  55. fn foo(_: Pin<&mut i32>) {}
  56. foo(x);
  57. foo(z);
  58. }
Add Comment
Please, Sign In to add comment