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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*!

Contains `DynTrait<_>`'s vtable,and related types/traits.

*/
use std::{
    fmt::{self, Debug,Write as FmtWrite},
    io,
    marker::PhantomData,
};

use super::{
    *,
    c_functions::*,
    iterator::{
        IteratorFns,MakeIteratorFns,
        DoubleEndedIteratorFns,MakeDoubleEndedIteratorFns,
    },
    traits::{IteratorItemOrDefault},
};

use crate::{
    StableAbi,
    abi_stability::{Tag,SharedStableAbi},
    marker_type::ErasedObject,
    prefix_type::{PrefixTypeTrait,WithMetadata,panic_on_missing_fieldname},
    pointer_trait::GetPointerKind,
    std_types::{Tuple3,RSome,RNone,RIoError,RSeekFrom},
};


use core_extensions::TypeIdentity;





#[doc(hidden)]
/// Returns the vtable used by DynTrait to do dynamic dispatch.
pub trait GetVtable<'borr,This,ErasedPtr,OrigPtr,I:InterfaceBound<'borr>> {
    
    const TMP_VTABLE:VTableVal<'borr,ErasedPtr,I>;

    const GET_VTABLE:*const WithMetadata<VTableVal<'borr,ErasedPtr,I>>=
        &WithMetadata::new(
            PrefixTypeTrait::METADATA,
            Self::TMP_VTABLE
        );


    /// Retrieves the VTable of the type.
    fn get_vtable<'a>() -> &'a VTable<'borr,ErasedPtr,I>
    where
        This: 'a,
    {
        // I am just getting a vtable
        unsafe { (*Self::GET_VTABLE).as_prefix() }
    }
}



macro_rules! declare_meta_vtable {
    (
        interface=$interf:ident;
        value=$value:ident;
        erased_pointer=$erased_ptr:ident;
        original_pointer=$orig_ptr:ident;

        marker_traits[
            $([
                impl $marker_trait:ident where [ $($phantom_where_clause:tt)* ]
            ])*
        ]

        $([
            $( #[$field_attr:meta] )*
            $field:ident : $field_ty:ty ;
            priv $priv_field:ident;
            option=$option_ty:ident,$some_constr:ident,$none_constr:ident;

            $(struct_bound=$struct_bound:expr;)*
            
            impl[$($impl_params:tt)*] VtableFieldValue<$selector:ident>
            where [ $($where_clause:tt)* ]
            { $field_value:expr }
        ])*
    ) => (

        /// This is the vtable for DynTrait<_>,
        ///
        #[repr(C,align(16))]
        #[derive(StableAbi)]
        #[sabi(
            // debug_print,
            inside_abi_stable_crate,
            kind(Prefix(prefix_struct="VTable")),
            missing_field(panic),
            prefix_bound="I:InterfaceBound<'borr>",
            bound="<I as SharedStableAbi>::StaticEquivalent:InterfaceBound<'static>",
            bound="<I as InterfaceBound<'borr>>::IteratorItem:StableAbi",
            $($(bound=$struct_bound,)*)*
        )]
        pub struct VTableVal<'borr,$erased_ptr,$interf>
        where $interf:InterfaceBound<'borr>
        {
            pub type_info:&'static TypeInfo,
            _marker:PhantomData<extern fn()->Tuple3<$erased_ptr,$interf,&'borr()>>,
            pub drop_ptr:unsafe extern "C" fn(&mut $erased_ptr),
            $(
                $( #[$field_attr] )*
                $priv_field:$option_ty<($field_ty)>,
            )*
        }


        impl<'borr,$erased_ptr,$interf> VTable<'borr,$erased_ptr,$interf>
        where   
            $interf:InterfaceBound<'borr>,
        {
            $(
                pub fn $field(&self)->($field_ty)
                where
                    $interf:InterfaceType<$selector=True>,
                {
                    const NAME:&'static &'static str=&stringify!($field);

                    match self.$priv_field().into() {
                        Some(v)=>v,
                        None=>panic_on_missing_fieldname::<
                            VTableVal<'borr,$erased_ptr,$interf>,
                            $field_ty
                        >(
                            NAME,
                            self._prefix_type_layout(),
                        )
                    }
                }
            )*
        }

        /// Returns the type of a vtable field.
        pub type VTableFieldType<'borr,Selector,$value,$erased_ptr,$orig_ptr,$interf>=
            <Selector as VTableFieldType_<'borr,$value,$erased_ptr,$orig_ptr,$interf>>::Field;

        /// Returns the type of a vtable field.
        pub trait VTableFieldType_<'borr,$value,$erased_ptr,$orig_ptr,$interf>{
            type Field;
        }

        /// Returns the value of a vtable field in the current binary
        /// (this can be a different value in a dynamically_linked_library/executable).
        pub trait VTableFieldValue<'borr,Ty,IsImpld,$value,$erased_ptr,$orig_ptr,$interf>{
            const FIELD:Ty;
        }

        pub trait MarkerTrait<'borr,IsImpld,$value,$erased_ptr,$orig_ptr>{}


        $(
            impl<'borr,$value,$erased_ptr,$orig_ptr,$interf> 
                VTableFieldType_<'borr,$value,$erased_ptr,$orig_ptr,$interf> 
            for trait_selector::$selector 
            where 
                $interf:InterfaceBound<'borr>,
            {
                type Field=$field_ty;
            }

            
            impl<'borr,AnyFieldTy,$value,$erased_ptr,$orig_ptr,$interf>
                VTableFieldValue<
                    'borr,$option_ty<AnyFieldTy>,False,$value,$erased_ptr,$orig_ptr,$interf
                >
            for trait_selector::$selector
            {
                const FIELD:$option_ty<AnyFieldTy>=$none_constr;
            }

            impl<'borr,FieldTy,$value,$erased_ptr,$orig_ptr,$interf,$($impl_params)*>
                VTableFieldValue<
                    'borr,$option_ty<FieldTy>,True,$value,$erased_ptr,$orig_ptr,$interf
                >
            for trait_selector::$selector
            where 
                $interf:InterfaceBound<'borr>,
                $field_ty:TypeIdentity<Type=FieldTy>,
                FieldTy:Copy,
                $($where_clause)*
            {
                const FIELD:$option_ty<FieldTy>=
                    $some_constr(type_identity!($field_ty=>FieldTy;$field_value));
            }
        )*



        impl<'borr,Anything,$value,$erased_ptr,$orig_ptr> 
            MarkerTrait<'borr,False,$value,$erased_ptr,$orig_ptr> 
        for Anything
        {}

        $(
            impl<'borr,$value,$erased_ptr,$orig_ptr> 
                MarkerTrait<'borr,True,$value,$erased_ptr,$orig_ptr> 
            for trait_selector::$marker_trait
            where $($phantom_where_clause)*
            {}
        )*

        ///////////////////////////////////////////////////////////

        /// Contains marker types representing traits of the same name.
        pub mod trait_selector{
            $(
                /// Marker type representing the trait of the same name.
                pub struct $marker_trait;
            )*
            $(
                /// Marker type representing the trait of the same name.
                pub struct $selector;
            )*
        }


        impl<'borr,This,$value,$erased_ptr,$orig_ptr,$interf> 
            GetVtable<'borr,$value,$erased_ptr,$orig_ptr,$interf>
        for This
        where
            This:ImplType<Interface=$interf>,
            $interf:InterfaceBound<'borr>,
            $(
                trait_selector::$marker_trait:
                    MarkerTrait<'borr,$interf::$marker_trait,$value,$erased_ptr,$orig_ptr>,
            )*
            $(
                trait_selector::$selector:VTableFieldValue<
                    'borr,
                    $option_ty<$field_ty>,
                    $interf::$selector,
                    $value,
                    $erased_ptr,
                    $orig_ptr,
                    $interf,
                >,
            )*
        {
            const TMP_VTABLE:VTableVal<'borr,$erased_ptr,$interf>=VTableVal{
                type_info:This::INFO,
                drop_ptr:drop_pointer_impl::<$orig_ptr,$erased_ptr>,
                $(
                    $priv_field:
                        <trait_selector::$selector as
                            VTableFieldValue<
                                $option_ty<VTableFieldType<
                                    'borr,
                                    trait_selector::$selector,
                                    $value,
                                    $erased_ptr,
                                    $orig_ptr,
                                    $interf,
                                >>,
                                $interf::$selector,
                                $value,
                                $erased_ptr,
                                $orig_ptr,
                                $interf,
                            >
                        >::FIELD,
                )*
                _marker:PhantomData,
            };

        }



        /// Trait used to capture all the bounds of DynTrait<_>.
        #[allow(non_upper_case_globals)]
        pub trait InterfaceBound<'borr>:InterfaceType {
            #[doc(hidden)]
            // Used to prevent users from implementing this trait.
            const __InterfaceBound_BLANKET_IMPL:PrivStruct<Self>;

            /// The Item type being iterated over,
            /// if `Iterator=False` this is `()`.
            type IteratorItem:'borr;

            /// Describes which traits are implemented,
            /// stored in the layout of the type in StableAbi,
            /// using the `#[sabi(tag="<I as InterfaceBound<'borr>>::TAG")]` attribute
            const TAG:Tag;

            $( 
                /// Used by the `StableAbi` derive macro to determine whether the field 
                /// this is associated with is disabled.
                const $selector:bool; 
            )*

        }   

        #[allow(non_upper_case_globals)]
        impl<'borr,I> InterfaceBound<'borr> for I
        where 
            I:InterfaceType,
            I:IteratorItemOrDefault<'borr,<I as InterfaceType>::Iterator>,
            $( I::$marker_trait:Boolean, )*
            $( I::$selector:Boolean, )*
        {
            type IteratorItem=
                <I as IteratorItemOrDefault<'borr,<I as InterfaceType>::Iterator>>::Item ;


            const TAG:Tag={
                const fn str_if(cond:bool,s:&'static str)->Tag{
                    // nulls are stripped in Tag collection variants.
                    //
                    // I'm using null here because using Vec<_> in constants isn't possible.
                    [ Tag::null(), Tag::str(s) ][cond as usize]
                }

                tag!{{
                    // Auto traits have to be equivalent in every linked library,
                    // this is why this is an array,it must match exactly.
                    "auto traits"=>tag![[
                        $(
                            str_if(
                                <I::$marker_trait as Boolean>::VALUE,
                                stringify!($marker_trait)
                            ),
                        )*
                    ]],
                    // These traits can be a superset of the interface in the loaded library,
                    // that is why it uses a map.
                    "required traits"=>tag!{{
                        $(
                            str_if(
                                <I::$selector as Boolean>::VALUE,
                                stringify!($selector)
                            ),
                        )*
                    }}
                }}
            };

            $( 
                const $selector:bool=<I::$selector as Boolean>::VALUE;
            )*
            
            const __InterfaceBound_BLANKET_IMPL:PrivStruct<Self>=
                PrivStruct(PhantomData);
        }


        impl<'borr,$erased_ptr,$interf> Debug for VTable<'borr,$erased_ptr,$interf> 
        where
            $interf:InterfaceBound<'borr>,
        {
            fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result {
                f.debug_struct("VTable")
                    .field("type_info",&self.type_info())
                    // $(
                    //     .field(
                    //         stringify!($field),
                    //         &format_args!("{:x}",self.$priv_field().map_or(0,|x|x as usize))
                    //     )
                    // )*
                    .finish()
            }
        }

    )
}

declare_meta_vtable! {
    interface=I;
    value  =T;
    erased_pointer=ErasedPtr;
    original_pointer=OrigP;

    marker_traits[
        [
            impl Send where [OrigP:Send,T:Send]
        ]
        [
            impl Sync where [OrigP:Sync,T:Sync]
        ]
    ]

    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Clone")]
        clone_ptr:    extern "C" fn(&ErasedPtr)->ErasedPtr;
        priv _clone_ptr;
        option=Option,Some,None;
        
        impl[] VtableFieldValue<Clone>
        where [OrigP:Clone]
        {
            clone_pointer_impl::<OrigP,ErasedPtr>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Default")]
        default_ptr: extern "C" fn()->ErasedPtr ;
        priv _default_ptr;
        option=Option,Some,None;
        impl[] VtableFieldValue<Default>
        where [
            OrigP:GetPointerKind,
            OrigP:DefaultImpl<<OrigP as GetPointerKind>::Kind>,
        ]{
            default_pointer_impl::<OrigP,ErasedPtr>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Display")]
        display:    extern "C" fn(&ErasedObject,FormattingMode,&mut RString)->RResult<(),()>;
        priv _display;
        option=Option,Some,None;
        impl[] VtableFieldValue<Display>
        where [T:Display]
        {
            display_impl::<T>
        }
    ]
    [
    #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Debug")]
        debug:      extern "C" fn(&ErasedObject,FormattingMode,&mut RString)->RResult<(),()>;
        priv _debug;
        option=Option,Some,None;
        impl[] VtableFieldValue<Debug>
        where [T:Debug]
        {
            debug_impl::<T>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Serialize")]
        serialize:  extern "C" fn(&ErasedObject)->RResult<RCow<'_,str>,RBoxError>;
        priv _serialize;
        option=Option,Some,None;
        impl[] VtableFieldValue<Serialize>
        where [ T:SerializeImplType, ]{
            serialize_impl::<T>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::PartialEq")]
        partial_eq: extern "C" fn(&ErasedObject,&ErasedObject)->bool;
        priv _partial_eq;
        option=Option,Some,None;
        impl[] VtableFieldValue<PartialEq>
        where [T:PartialEq,]
        {
            partial_eq_impl::<T>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Ord")]
        cmp:        extern "C" fn(&ErasedObject,&ErasedObject)->RCmpOrdering;
        priv _cmp;
        option=Option,Some,None;
        impl[] VtableFieldValue<Ord>
        where [T:Ord,]
        {
            cmp_ord::<T>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::PartialOrd")]
        partial_cmp:extern "C" fn(&ErasedObject,&ErasedObject)->ROption<RCmpOrdering>;
        priv _partial_cmp;
        option=Option,Some,None;
        impl[] VtableFieldValue<PartialOrd>
        where [T:PartialOrd,]
        {
            partial_cmp_ord::<T>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Hash")]
        hash:extern "C" fn(&ErasedObject,trait_objects::HasherObject<'_>);
        priv _hash;
        option=Option,Some,None;
        impl[] VtableFieldValue<Hash>
        where [T:Hash]
        {
            hash_Hash::<T>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::Iterator")]
        iter:IteratorFns< <I as InterfaceBound<'borr>>::IteratorItem >;
        priv _iter;
        option=ROption,RSome,RNone;
        impl[] VtableFieldValue<Iterator>
        where [
            T:Iterator,
            T::Item:'borr,
            I:InterfaceBound<'borr,IteratorItem=<T as Iterator>::Item>,
        ]{
            MakeIteratorFns::<T>::NEW
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::DoubleEndedIterator")]
        back_iter:DoubleEndedIteratorFns< <I as InterfaceBound<'borr>>::IteratorItem >;
        priv _back_iter;
        option=ROption,RSome,RNone;
        impl[] VtableFieldValue<DoubleEndedIterator>
        where [
            T:DoubleEndedIterator,
            T::Item:'borr,
            I:InterfaceBound<'borr,Iterator=True,IteratorItem=<T as Iterator>::Item>,
        ]{
            MakeDoubleEndedIteratorFns::<T>::NEW
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::FmtWrite")]
        fmt_write_str:extern "C" fn(&mut ErasedObject,RStr<'_>)->RResult<(),()>;
        priv _fmt_write_str;
        option=Option,Some,None;
        impl[] VtableFieldValue<FmtWrite>
        where [ T:FmtWrite ]
        {
            write_str_fmt_write::<T>
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::IoWrite")]
        io_write:IoWriteFns;
        priv _io_write;
        option=ROption,RSome,RNone;
        impl[] VtableFieldValue<IoWrite>
        where [ T:io::Write ]
        {
            MakeIoWriteFns::<T>::NEW
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::IoRead")]
        io_read:IoReadFns;
        priv _io_read;
        option=ROption,RSome,RNone;
        impl[] VtableFieldValue<IoRead>
        where [ T:io::Read ]
        {
            MakeIoReadFns::<T>::NEW
        }
    ]
    [
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::IoBufRead")]
        io_bufread:IoBufReadFns;
        priv _io_bufread;
        option=ROption,RSome,RNone;
        impl[] VtableFieldValue<IoBufRead>
        where [ 
            T:io::BufRead,
            I:InterfaceType<IoRead=True>
        ]{
            MakeIoBufReadFns::<T>::NEW
        }
    ]
    [
        #[sabi(last_prefix_field)]
        #[sabi(accessible_if="<I as InterfaceBound<'borr>>::IoSeek")]
        io_seek:extern "C" fn(&mut ErasedObject,RSeekFrom)->RResult<u64,RIoError>;
        priv _io_seek;
        option=Option,Some,None;
        impl[] VtableFieldValue<IoSeek>
        where [ T:io::Seek ]
        {
            io_Seek_seek::<T>
        }
    ]
}

//////////////


/// Used to prevent InterfaceBound being implemented outside this module,
/// since it is only constructed in the impl of InterfaceBound in this module.
#[doc(hidden)]
pub struct PrivStruct<T>(PhantomData<T>);