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

/*!
Traits for types wrapped in `DynTrait<_>`
*/

use std::{mem,marker::PhantomData};

use crate::{
    sabi_types::{Constructor,VersionStrings},
    std_types::{RBoxError, StaticStr},
};

use super::TypeInfo;

#[allow(unused_imports)]
use crate::type_level::{
    bools::{False, True},
    impl_enum::{Implemented,Unimplemented},
    trait_marker,
};

/**
An `implementation type`,
with an associated `interface type` which describes the traits that 
must be implemented when constructing a `DynTrait` from Self,
using the `from_value` and `from_ptr` constructors,
so as to pass an opaque type across ffi.

To initialize `INFO` you can use the `impl_get_type_info` macro.

# Uniqueness

Users of this trait can't enforce that they are the only ones with the same interface,
therefore they should handle the `Err(..)`s returned
from the `DynTrait::*_unerased` functions whenever
the convert back and forth between `Self` and `Self::Interface`.


*/
pub trait ImplType: Sized  {
    type Interface: InterfaceType;

    const INFO: &'static TypeInfo;
}


macro_rules! declare_InterfaceType {
    (

        $(#[$attrs:meta])*

        assoc_types[ 
            $( 
                $(#[$assoc_attrs:meta])*
                type $trait_:ident ;
            )* 
        ]
    ) => (
        $(#[$attrs])*
        pub trait InterfaceType: Sized {
            $(
                $(#[$assoc_attrs])*
                type $trait_;
            )*

            #[doc(hidden)]
            type define_this_in_the_impl_InterfaceType_macro;
        }


    )
}


declare_InterfaceType!{


/**
Defines the usable/required traits when creating a 
`DynTrait<Pointer<()>,ThisInterfaceType>`
from a type that implements `ImplType<Interface= ThisInterfaceType >` .

This trait can only be implemented using the
`#[derive(StableAbi)]` and the `impl_InterfaceType` macro,
giving a default value to each associated type,
so that adding associated types is not a breaking change.

The value of every associated type can be.

- `Implemented<_>`,the trait would be required by and usable in `DynTrait`.

- `Unimplemented<_>`,the trait would not be required by and not usable in `DynTrait`.

# Example

```

use abi_stable::{
    StableAbi,
    erased_types::InterfaceType,
    type_level::bools::*,
};

#[repr(C)]
#[derive(StableAbi)]
#[sabi(impl_InterfaceType(Clone,Debug))]
pub struct FooInterface;

/*
The `#[sabi(impl_InterfaceType(Clone,Debug))]` helper attribute 
(as part of #[derive(StableAbi)]) above is roughly equivalent to this impl:

impl InterfaceType for FooInterface {
    type Clone= Implemented<trait_marker::Clone>;

    type Debug= Implemented<trait_marker::Debug>;

    /////////////////////////////////////    
    //// defaulted associated types
    /////////////////////////////////////

    // Changing this to require/unrequire in minor versions,is an abi breaking change.
    // type Send= Unimplemented<trait_marker::Send>;

    // Changing this to require/unrequire in minor versions,is an abi breaking change.
    // type Sync= Unimplemented<trait_marker::Sync>;

    // type Iterator= Unimplemented<trait_marker::Iterator>;

    // type DoubleEndedIterator= Unimplemented<trait_marker::DoubleEndedIterator>;

    // type Default= Unimplemented<trait_marker::Default>;

    // type Display= Unimplemented<trait_marker::Display>;

    // type Serialize= Unimplemented<trait_marker::Serialize>;

    // type Eq= Unimplemented<trait_marker::Eq>;

    // type PartialEq= Unimplemented<trait_marker::PartialEq>;

    // type Ord= Unimplemented<trait_marker::Ord>;

    // type PartialOrd= Unimplemented<trait_marker::PartialOrd>;

    // type Hash= Unimplemented<trait_marker::Hash>;

    // type Deserialize= Unimplemented<trait_marker::Deserialize>;

    // type FmtWrite= Unimplemented<trait_marker::FmtWrite>;
    
    // type IoWrite= Unimplemented<trait_marker::IoWrite>;
    
    // type IoSeek= Unimplemented<trait_marker::IoSeek>;
    
    // type IoRead= Unimplemented<trait_marker::IoRead>;

    // type IoBufRead= Unimplemented<trait_marker::IoBufRead>;
    
    // type Error= Unimplemented<trait_marker::Error>;
}
*/

# fn main(){}


```


*/


    assoc_types[
        /// Changing this to require/unrequire in minor versions,is an abi breaking change.
        type Send;

        /// Changing this to require/unrequire in minor versions,is an abi breaking change.
        type Sync;

        type Clone;

        type Default;

        type Display;

        type Debug;

        type Serialize;

        type Eq;

        type PartialEq;

        type Ord;

        type PartialOrd;

        type Hash;

        type Deserialize;

        type Iterator;
        
        type DoubleEndedIterator;

        type FmtWrite;
        
        type IoWrite;
        
        type IoSeek;
        
        type IoRead;

        type IoBufRead;
        
        type Error;
    ]


}



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


/**
Describes how this `implementation type` is serialized.
*/
pub trait SerializeImplType<'s>{
    type Interface:SerializeProxyType<'s>;

    fn serialize_impl(
        &'s self
    ) -> Result<<Self::Interface as SerializeProxyType<'s>>::Proxy, RBoxError>;
}


/**
Gets the intermediate type an ImplType is converted into,to serialize it.
*/
pub trait SerializeProxyType<'borr>:InterfaceType{
    /// The intermediate type an ImplType is converted into,to serialize it
    type Proxy:'borr;
}

#[doc(hidden)]
pub trait GetSerializeProxyType<'borr>:InterfaceType{
    type ProxyType;
}

impl<'borr,I,PT> GetSerializeProxyType<'borr> for I
where
    I:InterfaceType,
    I:GetSerializeProxyTypeHelper<
        'borr,
        <I as InterfaceType>::Serialize,
        ProxyType=PT
    >,
{
    type ProxyType=PT;
}

#[doc(hidden)]
pub trait GetSerializeProxyTypeHelper<'borr,IS>:InterfaceType{
    type ProxyType;
}

impl<'borr,I> GetSerializeProxyTypeHelper<'borr,Implemented<trait_marker::Serialize>> for I
where
    I:SerializeProxyType<'borr>,
{
    type ProxyType=<I as SerializeProxyType<'borr>>::Proxy;
}

impl<'borr,I> GetSerializeProxyTypeHelper<'borr,Unimplemented<trait_marker::Serialize>> for I
where
    I:InterfaceType,
{
    type ProxyType=();
}


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


/**
Describes how `D` is deserialized,using a proxy to do so.

Generally this delegates to a library function,
so that the implementation can be delegated
to the `implementation crate`.

*/
pub trait DeserializeDyn<'borr,D> {
    /// The type that is deserialized and then converted into `D`,
    /// with `DeserializeDyn::deserialize_dyn`.
    type Proxy;

    /// Converts the proxy type into `D`.
    fn deserialize_dyn(s: Self::Proxy) -> Result<D, RBoxError>;
}


#[doc(hidden)]
pub trait GetDeserializeDynProxy<'borr,D>:InterfaceType{
    type ProxyType;
}

impl<'borr,I,D,PT> GetDeserializeDynProxy<'borr,D> for I
where
    I:InterfaceType,
    I:GetDeserializeDynProxyHelper<
        'borr,
        D,
        <I as InterfaceType>::Deserialize,
        ProxyType=PT
    >,
{
    type ProxyType=PT;
}


#[doc(hidden)]
pub trait GetDeserializeDynProxyHelper<'borr,D,IS>:InterfaceType{
    type ProxyType;
}

impl<'borr,I,D> 
    GetDeserializeDynProxyHelper<'borr,D,Implemented<trait_marker::Deserialize>> 
for I
where
    I:InterfaceType,
    I:DeserializeDyn<'borr,D>
{
    type ProxyType=<I as DeserializeDyn<'borr,D>>::Proxy;
}

impl<'borr,I,D> 
    GetDeserializeDynProxyHelper<'borr,D,Unimplemented<trait_marker::Deserialize>> 
for I
where
    I:InterfaceType,
{
    type ProxyType=();
}


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


/// The way to specify the expected Iterator::Item type for an InterfaceType.
///
/// This is a separate trait to allow iterators that yield borrowed elements.
pub trait IteratorItem<'a>:InterfaceType{
    type Item;
}



/// Gets the Item type of an Iterator.
///
/// Used by `DynTrait`'s vtable to give its iter a default type,
/// when `I:InterfaceType<Iterator=Implemented<_>>`.
pub trait IteratorItemOrDefault<'borr>:InterfaceType{
    type Item;
}


impl<'borr,I,Item> IteratorItemOrDefault<'borr> for I
where 
    I:InterfaceType,
    I:IteratorItemOrDefaultHelper<
        'borr,
        <I as InterfaceType>::Iterator,
        Item=Item,
    >
{
    type Item=Item;
}


#[doc(hidden)]
pub trait IteratorItemOrDefaultHelper<'borr,ImplIsRequired>{
    type Item;
}

impl<'borr,I,Item> IteratorItemOrDefaultHelper<'borr,Implemented<trait_marker::Iterator>> for I
where
    I:IteratorItem<'borr,Item=Item>,
{
    type Item=Item;
}


impl<'borr,I> IteratorItemOrDefaultHelper<'borr,Unimplemented<trait_marker::Iterator>> for I{
    type Item=();
}



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


pub use self::interface_for::InterfaceFor;

#[doc(hidden)]
pub mod interface_for{
    use super::*;

    use crate::type_level::unerasability::GetUTID;

    /// Helper struct to get an `ImplType` implementation for any type.
    pub struct InterfaceFor<T,Interface,Unerasability>(
        PhantomData<fn()->(T,Interface,Unerasability)>
    );

    impl<T,Interface,Unerasability> ImplType for InterfaceFor<T,Interface,Unerasability>
    where 
        Interface:InterfaceType,
        Unerasability:GetUTID<T>,
    {
        type Interface=Interface;
        
        /// The `&'static TypeInfo` constant,used when unerasing `DynTrait`s into a type.
        const INFO:&'static TypeInfo=&TypeInfo{
            size:mem::size_of::<T>(),
            alignment:mem::align_of::<T>(),
            _uid:<Unerasability as GetUTID<T>>::UID,
            type_name:Constructor(crate::utils::get_type_name::<T>),
            module:StaticStr::new("<unavailable>"),
            package:StaticStr::new("<unavailable>"),
            package_version:VersionStrings::new("99.99.99"),
            _private_field:(),
        };
    }
}




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

crate::impl_InterfaceType!{
    impl crate::erased_types::InterfaceType for () {
        type Send=True;
        type Sync=True;
    }
}