Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(pin)]
- use std::pin::Pin;
- macro_rules! pin_proj {
- (@record({}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- let &mut $st {$($p)*} = unsafe { Pin::get_mut_unchecked($e) };
- $(
- let $binders = unsafe { Pin::new_unchecked($binders) };
- )*
- };
- (@record({..}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- pin_proj! { @record({}, $st, $e, {$($p)* ..}, [$($binders)*]) }
- };
- (@record({$field:ident, $($rem:tt)*}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- pin_proj! { @record({$($rem)*}, $st, $e, {$($p)* ref mut $field,}, [$($binders)* $field]) }
- };
- (@record({$field:ident}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- pin_proj! { @record({}, $st, $e, {$($p)* ref mut $field}, [$($binders)* $field]) }
- };
- (@record({$field:ident : _, $($rem:tt)*}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- pin_proj! { @record({$($rem)*}, $st, $e, {$($p)* $field: _,}, [$($binders)*]) }
- };
- (@record({$field:ident : _}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- pin_proj! { @record({}, $st, $e, {$($p)* $field: _}, [$($binders)*]) }
- };
- (@record({$field:ident : $var:ident, $($rem:tt)*}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- pin_proj! { @record({$($rem)*}, $st, $e, {$($p)* $field: ref mut $var,}, [$($binders)* $var]) }
- };
- (@record({$field:ident : $var:ident}, $st:path, $e:expr, {$($p:tt)*}, [$($binders:ident)*])) => {
- pin_proj! { @record({}, $st, $e, {$($p)* $field: ref mut $var}, [$($binders)* $var]) }
- };
- (let $st:path {$($fields:tt)*} = $e:expr;) => {
- pin_proj! { @record({$($fields)*}, $st, $e, {}, []) }
- };
- }
- pub struct A {}
- pub fn test1(x: Pin<&mut A>) {
- pin_proj! {
- let A {} = x;
- }
- }
- pub struct B {
- x: i32,
- y: i32,
- }
- pub fn test2(x: Pin<&mut B>) {
- pin_proj! {
- let B { x, y: z } = x;
- }
- fn foo(_: Pin<&mut i32>) {}
- foo(x);
- foo(z);
- }
Add Comment
Please, Sign In to add comment