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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*!
Contains the `VirtualWrapper` type,and related traits/type aliases.
*/

use std::{
    ops::DerefMut,
    mem::ManuallyDrop,
    ptr,
};

use serde::{de, ser, Deserialize, Deserializer};

#[allow(unused_imports)]
use core_extensions::{prelude::*, ResultLike};

use crate::{
    pointer_trait::{ErasedStableDeref, StableDeref, TransmuteElement},
    ErasedObject, 
    std_types::{RBox, RCow, RStr},
};

use super::*;
use super::{
    c_functions::adapt_std_fmt,
    trait_objects::*,
    vtable::{GetVtable, VTable},
    traits::InterfaceFor,
};


#[cfg(test)]
mod tests;

mod priv_ {
    use super::*;


    /**

VirtualWrapper implements ffi-safe trait objects,for a selection of traits.

# Passing opaque values around with `VirtualWrapper<_>`

One can pass non-StableAbi types around by using type erasure,using this type.

It generally looks like `VirtualWrapper<Pointer<ZeroSized<Interface>>>`,where:

- Pointer is some `pointer_trait::StableDeref` pointer type.

- ZeroSized is a zero-sized marker type.

- Interface is an `InterfaceType`,which describes what traits are 
    required when constructing the `VirtualWrapper<_>` and which ones it implements.

`trait InterfaceType` allows describing which traits are required 
when constructing a `VirtualWrapper<_>`,and which ones it implements.

### Construction

To construct a `VirtualWrapper<_>` one can use these associated functions:
    
- from_value:
    Can be constructed from the value directly.
    Requires a value that implements ImplType.
    
- from_ptr:
    Can be constructed from a pointer of a value.
    Requires a value that implements ImplType.
    
- from_any_value:
    Can be constructed from the value directly.Requires a `'static` value.
    
- from_any_ptr
    Can be constructed from a pointer of a value.Requires a `'static` value.

### Trait object

`VirtualWrapper<Pointer<ZeroSized< Interface >>>` 
can be used as a trait object for any combination of 
the traits listed bellow.

These are the traits:

- Clone 

- Display 

- Debug 

- Default: Can be called as an inherent method.

- Eq 

- PartialEq 

- Ord 

- PartialOrd 

- Hash 

- serde::Deserialize:
    first deserializes from a string,and then calls the objects' Deserialize impl.

- serde::Serialize:
    first calls the objects' Deserialize impl,then serializes that as a string.

### Deconstruction

`VirtualWrapper<_>` can then be unwrapped into a concrete type,
within the same dynamic library/executable that constructed it,
using these (fallible) conversion methods:

- into_unerased:
    Unwraps into a pointer to `T`.
    Where `VirtualWrapper<P<ZeroSized< Interface >>>`'s 
        Interface must equal `<T as ImplType>::Interface`

- as_unerased:
    Unwraps into a `&T`.
    Where `VirtualWrapper<P<ZeroSized< Interface >>>`'s 
        Interface must equal `<T as ImplType>::Interface`

- as_unerased_mut:
    Unwraps into a `&mut T`.
    Where `VirtualWrapper<P<ZeroSized< Interface >>>`'s 
        Interface must equal `<T as ImplType>::Interface`

- into_mut_unerased:Unwraps into a pointer to `T`.Requires `T:'static`.

- as_mut_unerased:Unwraps into a `&T`.Requires `T:'static`.

- as_mut_unerased_mut:Unwraps into a `&mut T`.Requires `T:'static`.




    
    */
    #[repr(C)]
    #[derive(StableAbi)]
    #[sabi(inside_abi_stable_crate)]
    pub struct VirtualWrapper<P> {
        pub(super) object: ManuallyDrop<P>,
        vtable: *const VTable<P>,
    }

    impl VirtualWrapper<()> {
        /// Constructors the `VirtualWrapper<_>` from an ImplType implementor.
        ///
        /// Use this whenever possible instead of `from_any_value`,
        /// because it produces better error messages when unerasing the `VirtualWrapper<_>`
        pub fn from_value<T>(object: T) -> VirtualWrapper<RBox<ZeroSized<T::Interface>>>
        where
            T: ImplType,
            T: GetVtable<T,RBox<ZeroSized<<T as ImplType>::Interface>>,RBox<T>>,
        {
            let object = RBox::new(object);
            VirtualWrapper::from_ptr(object)
        }

        /// Constructors the `VirtualWrapper<_>` from a pointer to an ImplType implementor.
        ///
        /// Use this whenever possible instead of `from_any_ptr`,
        /// because it produces better error messages when unerasing the `VirtualWrapper<_>`
        pub fn from_ptr<P, T>(object: P) -> VirtualWrapper<P::TransmutedPtr>
        where
            P: StableDeref<Target = T>,
            T: ImplType,
            T: GetVtable<T,P::TransmutedPtr,P>,
            P: ErasedStableDeref<<T as ImplType>::Interface>,
        {
            VirtualWrapper {
                object: ManuallyDrop::new(object.erased(T::Interface::T)),
                vtable: T::get_vtable(),
            }
        }

        /// Constructors the `VirtualWrapper<_>` from a type which doesn't borrow anything.
        pub fn from_any_value<T,I>(object: T,interface:I) -> VirtualWrapper<RBox<ZeroSized<I>>>
        where
            T:'static,
            I:InterfaceType,
            InterfaceFor<T,I> : GetVtable<T,RBox<ZeroSized<I>>,RBox<T>>,
        {
            let object = RBox::new(object);
            VirtualWrapper::from_any_ptr(object,interface)
        }

        /// Constructors the `VirtualWrapper<_>` from a pointer to a 
        /// type which doesn't borrow anything.
        pub fn from_any_ptr<P, T,I>(object: P,_interface:I) -> VirtualWrapper<P::TransmutedPtr>
        where
            I:InterfaceType,
            P: StableDeref<Target = T>,
            T:'static,
            InterfaceFor<T,I>: GetVtable<T,P::TransmutedPtr,P>,
            P: ErasedStableDeref<I>,
        {
            VirtualWrapper {
                object: ManuallyDrop::new(object.erased(I::T)),
                vtable: <InterfaceFor<T,I>>::get_vtable(),
            }
        }
    }

    impl<P> VirtualWrapper<P> {
        pub(super) fn vtable<'a>(&self) -> &'a VTable<P>{
            unsafe {
                &*self.vtable
            }
        }

        /// Allows checking whether 2 `VirtualWrapper<_>`s have a value of the same type.
        ///
        /// Note that types from different dynamic libraries/executables are 
        /// never considered equal.
        pub fn is_same_type<Other>(&self,other:&VirtualWrapper<Other>)->bool{
            self.vtable_address()==other.vtable_address()||
            self.vtable().type_info().is_compatible(other.vtable().type_info())
        }

        pub(super)fn vtable_address(&self) -> usize {
            self.vtable as usize
        }

        pub(super) fn as_abi(&self) -> &ErasedObject
        where
            P: Deref,
        {
            self.object()
        }

        #[allow(dead_code)]
        pub(super) fn as_abi_mut(&mut self) -> &mut ErasedObject
        where
            P: DerefMut,
        {
            self.object_mut()
        }

        /// Returns the address of the wrapped object.
        ///
        /// This will not change between calls for the same `VirtualWrapper<_>`.
        pub fn object_address(&self) -> usize
        where
            P: Deref,
        {
            self.object() as *const ErasedObject as usize
        }

        pub(super) fn object(&self) -> &ErasedObject
        where
            P: Deref,
        {
            unsafe { self.object_as() }
        }
        pub(super) fn object_mut(&mut self) -> &mut ErasedObject
        where
            P: DerefMut,
        {
            unsafe { self.object_as_mut() }
        }

        unsafe fn object_as<T>(&self) -> &T
        where
            P: Deref,
        {
            &*((&**self.object) as *const P::Target as *const T)
        }
        unsafe fn object_as_mut<T>(&mut self) -> &mut T
        where
            P: DerefMut,
        {
            &mut *((&mut **self.object) as *mut P::Target as *mut T)
        }
    }

    impl<P> VirtualWrapper<P> {
        /// The uid in the vtable has to be the same as the one for T,
        /// otherwise it was not created from that T in the library that declared the opaque type.
        pub(super) fn check_same_destructor_opaque<A,T>(&self) -> Result<(), UneraseError>
        where
            P: TransmuteElement<T>,
            A: GetVtable<T,P,P::TransmutedPtr>,
        {
            let t_vtable:&VTable<P> = A::get_vtable();
            if self.vtable_address() == t_vtable as *const _ as usize
                || self.vtable().type_info().is_compatible(t_vtable.type_info())
            {
                Ok(())
            } else {
                Err(UneraseError {
                    expected_vtable_address: t_vtable as *const _ as usize,
                    expected_type_info:t_vtable.type_info(),
                    found_vtable_address: self.vtable as usize,
                    found_type_info:self.vtable().type_info(),
                })
            }
        }

        /// Unwraps the `VirtualWrapper<_>` into a pointer of 
        /// the concrete type that it was constructed with.
        ///
        /// T is required to implement ImplType.
        ///
        /// # Errors
        ///
        /// This will return an error in any of these conditions:
        ///
        /// - It is called in a dynamic library/binary outside
        /// the one from which this `VirtualWrapper<_>` was constructed.
        ///
        /// - `T` is not the concrete type this `VirtualWrapper<_>` was constructed with.
        ///
        pub fn into_unerased<T>(self) -> Result<P::TransmutedPtr, UneraseError>
        where
            P: TransmuteElement<T>,
            P::Target:Sized,
            T: ImplType + GetVtable<T,P,P::TransmutedPtr>,
        {
            self.check_same_destructor_opaque::<T,T>()?;
            unsafe { 
                let this=ManuallyDrop::new(self);
                Ok(ptr::read(&*this.object).transmute_element(T::T)) 
            }
        }

        /// Unwraps the `VirtualWrapper<_>` into a reference of 
        /// the concrete type that it was constructed with.
        ///
        /// T is required to implement ImplType.
        ///
        /// # Errors
        ///
        /// This will return an error in any of these conditions:
        ///
        /// - It is called in a dynamic library/binary outside
        /// the one from which this `VirtualWrapper<_>` was constructed.
        ///
        /// - `T` is not the concrete type this `VirtualWrapper<_>` was constructed with.
        ///
        pub fn as_unerased<T>(&self) -> Result<&T, UneraseError>
        where
            P: Deref + TransmuteElement<T>,
            T: ImplType + GetVtable<T,P,P::TransmutedPtr>,
        {
            self.check_same_destructor_opaque::<T,T>()?;
            unsafe { Ok(self.object_as()) }
        }

        /// Unwraps the `VirtualWrapper<_>` into a mutable reference of 
        /// the concrete type that it was constructed with.
        ///
        /// T is required to implement ImplType.
        ///
        /// # Errors
        ///
        /// This will return an error in any of these conditions:
        ///
        /// - It is called in a dynamic library/binary outside
        /// the one from which this `VirtualWrapper<_>` was constructed.
        ///
        /// - `T` is not the concrete type this `VirtualWrapper<_>` was constructed with.
        ///
        pub fn as_unerased_mut<T>(&mut self) -> Result<&mut T, UneraseError>
        where
            P: DerefMut + TransmuteElement<T>,
            T: ImplType + GetVtable<T,P,P::TransmutedPtr>,
        {
            self.check_same_destructor_opaque::<T,T>()?;
            unsafe { Ok(self.object_as_mut()) }
        }


        /// Unwraps the `VirtualWrapper<_>` into a pointer of 
        /// the concrete type that it was constructed with.
        ///
        /// T is required to not borrows anything.
        ///
        /// # Errors
        ///
        /// This will return an error in any of these conditions:
        ///
        /// - It is called in a dynamic library/binary outside
        /// the one from which this `VirtualWrapper<_>` was constructed.
        ///
        /// - `T` is not the concrete type this `VirtualWrapper<_>` was constructed with.
        ///
        pub fn into_any_unerased<T>(self) -> Result<P::TransmutedPtr, UneraseError>
        where
            P: TransmuteElement<T>,
            P::Target:Sized,
            Self:VirtualWrapperTrait,
            InterfaceFor<T,GetVWInterface<Self>>: GetVtable<T,P,P::TransmutedPtr>,
        {
            self.check_same_destructor_opaque::<InterfaceFor<T,GetVWInterface<Self>>,T>()?;
            unsafe {
                unsafe { 
                    let this=ManuallyDrop::new(self);
                    Ok(ptr::read(&*this.object).transmute_element(T::T)) 
                }
            }
        }

        /// Unwraps the `VirtualWrapper<_>` into a reference of 
        /// the concrete type that it was constructed with.
        ///
        /// T is required to not borrows anything.
        ///
        /// # Errors
        ///
        /// This will return an error in any of these conditions:
        ///
        /// - It is called in a dynamic library/binary outside
        /// the one from which this `VirtualWrapper<_>` was constructed.
        ///
        /// - `T` is not the concrete type this `VirtualWrapper<_>` was constructed with.
        ///
        pub fn as_any_unerased<T>(&self) -> Result<&T, UneraseError>
        where
            P: Deref + TransmuteElement<T>,
            Self:VirtualWrapperTrait,
            InterfaceFor<T,GetVWInterface<Self>>: GetVtable<T,P,P::TransmutedPtr>,
        {
            self.check_same_destructor_opaque::<InterfaceFor<T,GetVWInterface<Self>>,T>()?;
            unsafe { Ok(self.object_as()) }
        }

        /// Unwraps the `VirtualWrapper<_>` into a mutable reference of 
        /// the concrete type that it was constructed with.
        ///
        /// T is required to not borrows anything.
        ///
        /// # Errors
        ///
        /// This will return an error in any of these conditions:
        ///
        /// - It is called in a dynamic library/binary outside
        /// the one from which this `VirtualWrapper<_>` was constructed.
        ///
        /// - `T` is not the concrete type this `VirtualWrapper<_>` was constructed with.
        ///
        pub fn as_any_unerased_mut<T>(&mut self) -> Result<&mut T, UneraseError>
        where
            P: DerefMut + TransmuteElement<T>,
            Self:VirtualWrapperTrait,
            InterfaceFor<T,GetVWInterface<Self>>: GetVtable<T,P,P::TransmutedPtr>,
        {
            self.check_same_destructor_opaque::<InterfaceFor<T,GetVWInterface<Self>>,T>()?;
            unsafe { Ok(self.object_as_mut()) }
        }

    }

    impl<P> VirtualWrapper<P> {
        /// Constructs a VirtualWrapper<P> wrapping a `P`,using the same vtable.
        /// `P` must come from a function in the vtable,
        /// to ensure that it is compatible with the functions in it.
        pub(super) fn from_new_ptr(&self, object: P) -> Self {
            Self {
                object:ManuallyDrop::new(object),
                vtable: self.vtable,
            }
        }

        /// Constructs a `VirtualWrapper<P>` with the default value for `P`.
        pub fn default<I>(&self) -> Self
        where
            P: Deref<Target = ZeroSized<I>>,
            I: InterfaceType<Default = True>,
        {
            let new = self.vtable().default_ptr::<I>()();
            self.from_new_ptr(new)
        }

        /// It serializes a `VirtualWrapper<_>` into a string by using 
        /// `<ConcreteType as SerializeImplType>::serialize_impl`.
        pub fn serialized<'a, I>(&'a self) -> Result<RCow<'a, RStr<'a>>, RBoxError>
        where
            P: Deref<Target = ZeroSized<I>>,
            I: InterfaceType<Serialize = True>,
        {
            self.vtable().serialize::<I>()(self.as_abi()).into_result()
        }

        /// Deserializes a string into a `VirtualWrapper<_>`,by using 
        /// `<I as DeserializeInterfaceType>::deserialize_impl`.
        pub fn deserialize_from_str<'a, I>(s: &'a str) -> Result<Self, RBoxError>
        where
            P: Deref<Target = ZeroSized<I>>,
            I: DeserializeInterfaceType<Deserialize = True, Deserialized = Self>,
        {
            s.piped(RStr::from).piped(I::deserialize_impl)
        }
    }

    impl<P> Drop for VirtualWrapper<P>{
        fn drop(&mut self){
            let vtable=self.vtable();
            unsafe{
                vtable.drop_ptr()(&mut *self.object);
            }
        }
    }

}

pub use self::priv_::VirtualWrapper;

impl<P, I> Clone for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<Clone = True>,
{
    fn clone(&self) -> Self {
        let vtable = self.vtable();
        let new = vtable.clone_ptr::<I>()(&*self.object);
        self.from_new_ptr(new)
    }
}

impl<P, I> Display for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<Display = True>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        adapt_std_fmt::<ErasedObject>(self.object(), self.vtable().display::<I>(), f)
    }
}

impl<P, I> Debug for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<Debug = True>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        adapt_std_fmt::<ErasedObject>(self.object(), self.vtable().debug::<I>(), f)
    }
}

/**
First it serializes a `VirtualWrapper<_>` into a string by using 
<ConcreteType as SerializeImplType>::serialize_impl,
then it serializes the string.

*/
/// ,then it .
impl<P, I> Serialize for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<Serialize = True>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.vtable().serialize::<I>()(self.as_abi())
            .into_result()
            .map_err(ser::Error::custom)?
            .serialize(serializer)
    }
}

/// First it Deserializes a string,then it deserializes into a 
/// `VirtualWrapper<_>`,by using `<I as DeserializeInterfaceType>::deserialize_impl`.
impl<'a, P, I> Deserialize<'a> for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: DeserializeInterfaceType<Deserialize = True, Deserialized = Self>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'a>,
    {
        let s = String::deserialize(deserializer)?;
        I::deserialize_impl(RStr::from(&*s)).map_err(de::Error::custom)
    }
}

impl<P, I> Eq for VirtualWrapper<P>
where
    Self: PartialEq,
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<Eq = True>,
{
}

impl<P, I> PartialEq for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<PartialEq = True>,
{
    fn eq(&self, other: &Self) -> bool {
        // unsafe: must check that the vtable is the same,otherwise return a sensible value.
        if !self.is_same_type(other) {
            return false;
        }

        self.vtable().partial_eq::<I>()(self.as_abi(), other.as_abi())
    }
}

impl<P, I> Ord for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<Ord = True>,
    Self: PartialOrd + Eq,
{
    fn cmp(&self, other: &Self) -> Ordering {
        // unsafe: must check that the vtable is the same,otherwise return a sensible value.
        if !self.is_same_type(other) {
            return self.vtable_address().cmp(&other.vtable_address());
        }

        self.vtable().cmp::<I>()(self.as_abi(), other.as_abi()).into()
    }
}

impl<P, I> PartialOrd for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<PartialOrd = True>,
    Self: PartialEq,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // unsafe: must check that the vtable is the same,otherwise return a sensible value.
        if !self.is_same_type(other) {
            return Some(self.vtable_address().cmp(&other.vtable_address()));
        }

        self.vtable().partial_cmp::<I>()(self.as_abi(), other.as_abi())
            .map(IntoReprRust::into_rust)
            .into()
    }
}

impl<P, I> Hash for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType<Hash = True>,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.vtable().hash::<I>()(self.as_abi(), HasherTraitObject::new(state))
    }
}

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

mod sealed {
    use super::*;
    pub trait Sealed {}
    impl<P> Sealed for VirtualWrapper<P> {}
}
use self::sealed::Sealed;

/// For accessing the Interface of a `VirtualWrapper<Pointer<ZeroSized< Interface >>>`.
pub trait VirtualWrapperTrait: Sealed {
    type Interface: InterfaceType;
}

impl<P, I> VirtualWrapperTrait for VirtualWrapper<P>
where
    P: Deref<Target = ZeroSized<I>>,
    I: InterfaceType,
{
    type Interface = I;
}


/// For accessing the `Interface` in a `VirtualWrapper<Pointer<ZeroSized< Interface >>>`.
pub type GetVWInterface<This>=
    <This as VirtualWrapperTrait>::Interface;


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

/// Error for `VirtualWrapper<_>` being unerased into the wrong type
/// with one of the `*unerased*` methods.
#[derive(Debug,Copy, Clone)]
pub struct UneraseError {
    expected_vtable_address: usize,
    expected_type_info:&'static TypeInfo,
    found_vtable_address: usize,
    found_type_info:&'static TypeInfo,
}


impl fmt::Display for UneraseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl ::std::error::Error for UneraseError {}