1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#[macro_export]
macro_rules! context {
    (
        mod $name:ident
        $(< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ $(,)?>)?
        {
            $($(
                $field:ident $(/ $field_mut:ident)? : $field_mod:ident $field_ty:ty
            ),+ $(,)?)?
        }
    ) => {
        mod $name {
            #[allow(unused_imports)]
            use super::*;

            context! {
                @impl Context
                [ $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?] [ $(< $( $lt ),+ >)?]
                {} {} {} {} { $($($field $(/ $field_mut)? : $field_mod $field_ty),+)? }
            }
        }
    };
    (
        @impl $name:ident
        [$($g:tt)*] [$($r:tt)*]
        {$({$($f:tt)*})*}
        {$({$($p:tt)*})*}
        {$({$($a:tt)*})*}
        {$({$($b:tt)*})*}
        {$field:ident : ref $ty:ty $(, $($other_fields:tt)+)?}
    ) => {
        context! {
            @impl $name
            [$($g)*] [$($r)*]
            {
                $({$($f)*})*
                {$field : *const $ty}
            }
            {
                $({$($p)*})*
                {$field : &$ty}
            }
            {
                $({$($a)*})*
                {$field : $field as *const $ty}
            }
            {
                $({$($b)*})*
                {
                    pub fn $field (&self) -> &$ty { unsafe { &*self.$field } }
                }
            }
            {$($($other_fields)+)?}
        }
    };
    (
        @impl $name:ident
        [$($g:tt)*] [$($r:tt)*]
        {$({$($f:tt)*})*}
        {$({$($p:tt)*})*}
        {$({$($a:tt)*})*}
        {$({$($b:tt)*})*}
        {$field:ident / $field_mut:ident : mut $ty:ty $(, $($other_fields:tt)+)?}
    ) => {
        context! {
            @impl $name
            [$($g)*] [$($r)*]
            {
                $({$($f)*})*
                {$field : *mut $ty}
            }
            {
                $({$($p)*})*
                {$field : &mut $ty}
            }
            {
                $({$($a)*})*
                {$field : $field as *mut $ty}
            }
            {
                $({$($b)*})*
                {
                    #[allow(dead_code)]
                    pub fn $field (&self) -> &$ty { unsafe { &*self.$field } }
                    #[allow(dead_code)]
                    pub fn $field_mut (&mut self) -> &mut $ty { unsafe { &mut *self.$field } }
                }
            }
            {$($($other_fields)+)?}
        }
    };
    (
        @impl $name:ident
        [$($g:tt)*] [$($r:tt)*]
        {$({$($f:tt)*})*}
        {$({$($p:tt)*})*}
        {$({$($a:tt)*})*}
        {$({$($b:tt)*})*}
        {$field:ident : const $ty:ty $(, $($other_fields:tt)+)?}
    ) => {
        context! {
            @impl $name
            [$($g)*] [$($r)*]
            {
                $({$($f)*})*
                {$field : $ty}
            }
            {
                $({$($p)*})*
                {$field : $ty}
            }
            {
                $({$($a)*})*
                {$field}
            }
            {
                $({$($b)*})*
                {
                    pub fn $field (&self) -> $ty { self.$field }
                }
            }
            {$($($other_fields)+)?}
        }
    };
    (
        @impl $name:ident
        [$($g:tt)*] [$($r:tt)*]
        {$({$($f:tt)*})*} {$({$($p:tt)*})*} {$({$($a:tt)*})*} {$({$($b:tt)*})*} {}
    ) => {
        pub struct $name $($g)* {
            $($($f)*),*
        }

        impl $($g)* $name $($r)* {
            pub fn call<ContextCallReturnType>(
                $($($p)*),*,
                f: impl $crate::std_ops_FnOnce(&mut Self) -> ContextCallReturnType 
            ) -> ContextCallReturnType {
                let mut context = Self {
                    $($($a)*),*
                };
                f(&mut context)
            }

            $($($b)*)*
        }

        unsafe impl $($g)* Send for $name $($r)* { }
        unsafe impl $($g)* Sync for $name $($r)* { }
    };
}

#[cfg(docsrs)]
pub mod example {
    use core::fmt::Display;

    pub struct Data {
        pub x: i16,
        pub y: i16
    }

    context! {
        mod example_context {
            data/data_mut: mut Data,
            display: ref dyn Display,
            id: const usize,
        }
    }

    pub use example_context::Context as ExampleContext;
}

#[cfg(test)]
mod test {
    use core::mem::replace;

    context! {
        mod context_1 {
            a: const u8,
            b: ref u16,
            c/c_mut: mut u32,
        }
    }

    type Context1 = context_1::Context;

    #[test]
    fn test_context_1() {
        let mut x = 3;
        let res = Context1::call(1, &2, &mut x, |context| {
            assert_eq!(context.a(), 1u8);
            assert_eq!(context.b(), &2u16);
            assert_eq!(replace(context.c_mut(), 12), 3u32);
            "res"
        });
        assert_eq!(res, "res");
        assert_eq!(x, 12);
    }

    context! {
        mod context_2 {
            a: const u8,
            b: ref u16,
            c/c_mut: mut u32,
        }
    }

    pub type Context2 = context_2::Context;

    #[test]
    fn test_context_2() {
        let mut x = 3;
        let res = Context2::call(1, &2, &mut x, |context| {
            assert_eq!(context.a(), 1u8);
            assert_eq!(context.b(), &2u16);
            assert_eq!(replace(context.c_mut(), 12), 3u32);
            "res"
        });
        assert_eq!(res, "res");
        assert_eq!(x, 12);
    }
}