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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
This macro allows delegating the implementation of the Structural and field accessor traits.

This macro delegates the implementation of those traits for all fields,
it doesn't provide a way to do so for only a list of fields.

# Safety

The unsafety of implementing GetFieldMut with this macro comes from the methods
used to do multiple mutable borrows.

You must ensure that the variable that you delegate GetField to is the same as the one
you delegate GetFieldMut to,
as well as ensuring that there are no other impls of the GetFieldMut trait 
borrowing from the same variable mutably.


# Example with all syntax

```rust
use structural::delegate_structural_with;

# trait Trait{}
# impl<T> Trait for T{}

struct Foo<T>{
    value:T
}

delegate_structural_with!{
    impl[T] Foo<T>
    // This where clause is required syntax
    where[
        T:Trait,
    ]
    
    // This is the identifier used for `self` in the blocks bellow.
    self_ident=this;
    
    // This is the type of the variable we delegate to,
    // this is required because Rust doesn't have a `typeof`/`decltype` construct.
    field_ty=T;

    // This block of code is used to get the reference to the delegating variable 
    // in GetField and other traits.
    GetField {
        &this.value 
    }
    
    // This block of code is used to get a mutable reference to the delegating variable
    // in GetFieldMut
    //
    // This is `unsafe` because this block must always evaluate to a mutable reference
    // for the same variable,
    // and it must not be the same variable as other implementations of the GetFieldMut trait
    unsafe GetFieldMut 
    where [ 
        // This is an optional where clause
        // The last where predicate must have a trailing comma.
        T:Trait, 
    ]{
        &mut this.value
    }
    
    // This block of code is used to get the delegating variable by value in IntoField.
    IntoField 
    where [
        // This is an optional where clause
        // The last where predicate must have a trailing comma.
        T:Trait,
    ]{
        this.value 
    }
}
```

# Example

This example is of a type wrapping a `ManuallyDrop<T>`,delegating to the `T` inside it.

```rust
use std::{
    fmt::Debug,
    mem::ManuallyDrop,
};

use structural::{GetFieldExt,delegate_structural_with,make_struct,ti};

struct Bar<T>{
    value:ManuallyDrop<T>
}

impl<T> Bar<T>{
    pub fn new(value:T)->Self{
        Self{
            value:ManuallyDrop::new(value)
        }
    }
}


delegate_structural_with!{
    impl[T] Bar<T>
    where[
        // This requires T to implement Clone
        // for `Bar<T>` to implement the accessor traits
        T:Clone 
    ]
    self_ident=this;
    field_ty=T;

    GetField {
#       // This ensures that the `T:Clone` bound is put on the impl block.
#       T::clone;
        &*this.value 
    }

    unsafe GetFieldMut 
    where [T:Debug,]
    {
#       // This ensures that the `T:Clone+Debug` bounds are put on the impl block.
#       T::clone;
#       <T as Debug>::fmt;
        &mut *this.value 
    }

    IntoField 
    where [T:Debug,]
    {
#       // This ensures that the `T:Clone+Debug` bounds are put on the impl block.
#       T::clone;
#       <T as Debug>::fmt;
        ManuallyDrop::into_inner(this.value)
    }
}

{
    let mut bar=Bar::new((2,3,5,8,13));
    assert_eq!(
        bar.fields(ti!(4,3,2,1,0)),
        ( &13, &8, &5, &3, &2 )
    );

    assert_eq!(
        bar.fields_mut(ti!(1,2,3,4)),
        ( &mut 3, &mut 5, &mut 8, &mut 13 )
    );

    assert_eq!(bar.into_field(ti!(1)),3);
}

{
    let mut bar=Bar::new(make_struct!{
        #![derive(Clone,Debug)] //This derives Clone and Debug for the anonymous struct

        a:"hello",
        b:"world",
        c:"tour",
    });
    assert_eq!(
        bar.fields(ti!(a,b,c)),
        ( &"hello", &"world", &"tour" )
    );

    assert_eq!(
        bar.fields_mut(ti!(c,b,a)),
        ( &mut"tour", &mut"world", &mut"hello" )
    );

    assert_eq!( bar.into_field(ti!(c)), "tour" );
}


```


*/
#[macro_export]
macro_rules! delegate_structural_with {
    (
        impl $impl_params:tt $self:ty
        where $where_clause:tt
        self_ident=$this:ident;
        field_ty=$field_ty:ty;

        GetField $get_field_closure:block
        $(
            unsafe GetFieldMut 
            $( where[ $($mut_where_clause:tt)* ] )?
            $unsafe_get_field_mut_closure:block
        )?
        $(
            IntoField 
            $( where[ $($into_where_clause:tt)* ] )?
            $into_field_closure:block
        )?
    ) => (
        
        $crate::delegate_structural_with!{
            inner-structural;
            impl $impl_params $self
            where $where_clause
            self_ident=$this;
            field_ty=$field_ty;
            GetField $get_field_closure
        }

        $crate::delegate_structural_with!{
            inner;
            impl $impl_params $self
            where $where_clause
            self_ident=$this;
            field_ty=$field_ty;
            GetField $get_field_closure
        }

        $(
            $crate::delegate_structural_with!{
                inner;
                impl $impl_params $self
                where $where_clause
                where[ $( $($mut_where_clause)* )? ]
                self_ident=$this;
                field_ty=$field_ty;
                GetField $get_field_closure
                unsafe GetFieldMut $unsafe_get_field_mut_closure
            }
        )?

        $(
            $crate::delegate_structural_with!{
                inner;
                impl $impl_params $self
                where $where_clause
                where [ $( $($into_where_clause)* )? ]
                self_ident=$this;
                field_ty=$field_ty;
                IntoField $into_field_closure
            }
        )?
    );
    (
        inner-structural;
        impl[$($impl_params:tt)*] $self:ty
        where [$($where_clause:tt)*]
        self_ident=$this:ident;
        field_ty=$field_ty:ty;

        GetField $get_field_closure:block
    )=>{
        impl<$($impl_params)*> $crate::Structural for $self 
        where
            $field_ty:$crate::Structural,
            $($where_clause)*
        {
            const FIELDS:&'static[$crate::structural_trait::FieldInfo]=
                <$field_ty as $crate::Structural>::FIELDS;

            type Fields=
                <$field_ty as $crate::Structural>::Fields;
        }

        impl<$($impl_params)*> $crate::StructuralDyn for $self
        where
            $field_ty:$crate::StructuralDyn,
            $($where_clause)*
        {
            fn fields_info(&self)->&'static[$crate::structural_trait::FieldInfo]{
                let $this=self;
                let field:&$field_ty=$get_field_closure;
                $crate::StructuralDyn::fields_info(field)
            }
        }
    };
    (inner;
        impl [$($($impl_params:tt)+)?] $self:ty
        where [$($where_clause:tt)*]
        self_ident=$this:ident;
        field_ty=$field_ty:ty;

        GetField $get_field_closure:block
    )=>{
        impl<$($($impl_params)* , )?__FieldName> 
            $crate::GetField<__FieldName>
            for $self
        where
            $field_ty:$crate::GetField<__FieldName>,
            $($where_clause)*
        {
            type Ty=$crate::GetFieldType<$field_ty,__FieldName>;

            fn get_field_(&self)->&Self::Ty{
                let $this=self;
                let field:&$field_ty=$get_field_closure;
                $crate::GetField::<__FieldName>::get_field_(field)
            }
        }
    };
    (inner;
        impl [$($($impl_params:tt)+)?] $self:ty
        where [$($where_clause:tt)*]
        where [$($mut_where_clause:tt)*]
        self_ident=$this:ident;
        field_ty=$field_ty:ty;

        GetField $get_field_closure:block
        unsafe GetFieldMut $unsafe_get_field_mut_closure:block
    )=>{
        unsafe impl<$( $($impl_params)* , )?__FieldName> 
            $crate::GetFieldMut<__FieldName>
            for $self
        where
            $field_ty:$crate::GetFieldMut<__FieldName>,
            $($mut_where_clause)*
            $($where_clause)*
        {
            fn get_field_mut_(&mut self)->&mut Self::Ty{
                let $this=self;
                let field:&mut $field_ty=$unsafe_get_field_mut_closure;
                <$field_ty as $crate::GetFieldMut<__FieldName>>::get_field_mut_(field)
            }
            unsafe fn get_field_mutref(
                ptr:$crate::mut_ref::MutRef<'_,()>,
                getter:$crate::field_traits::GetFieldMutRefFn<__FieldName,Self::Ty>
            )->&mut Self::Ty
            where 
                Self:Sized
            {
                <$field_ty as 
                    $crate::GetFieldMut<__FieldName>
                >::get_field_mutref(ptr,getter)
            }

            fn as_mutref(&mut self)->$crate::mut_ref::MutRef<'_,()>{
                let $this=self;
                let field:&mut $field_ty=$unsafe_get_field_mut_closure;
                $crate::GetFieldMut::<__FieldName>::as_mutref(field)
            }

            fn get_field_mutref_func(
                &self
            )->$crate::field_traits::GetFieldMutRefFn<__FieldName,Self::Ty>{
                let $this=self;
                let field:&$field_ty=$get_field_closure;
                $crate::GetFieldMut::<__FieldName>::get_field_mutref_func(field)
            }
        }
    };
        (inner;
        impl [$($($impl_params:tt)+)?] $self:ty
        where [$($where_clause:tt)*]
        where [$($into_where_clause:tt)*]
        self_ident=$this:ident;
        field_ty=$field_ty:ty;

        IntoField $into_field_closure:block
    )=>{
        impl<$( $($impl_params)* , )?__FieldName> 
            $crate::IntoField<__FieldName>
            for $self
        where
            $field_ty:$crate::IntoField<__FieldName>,
            $($into_where_clause)*
            $($where_clause)*
        {
            fn into_field_(self)->Self::Ty{
                let $this=self;
                let field:$field_ty=$into_field_closure;
                $crate::IntoField::<__FieldName>::into_field_(field)
            }

            $crate::impl_box_into_field_method!{__FieldName}
        }        
    };
}