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

#![cfg_attr(rustfmt, rustfmt::skip)]
//! See [the dedicated section of the guide](https://getditto.github.io/safer_ffi/dyn_traits/_.html).

use_prelude!();

#[path = "futures/_mod.rs"]
#[cfg(feature = "futures")]
#[cfg_attr(all(docs, feature = "nightly"),
    doc(cfg(feature = "futures"))
)]
pub mod futures;

#[doc(no_inline)]
pub use dyn_drop::*;

pub
mod dyn_drop;

pub use self::ty::{
    Erased as ErasedTy,
};

#[super::derive_ReprC]
#[repr(transparent)]
#[allow(missing_debug_implementations)]
pub
struct ErasedRef<'a> (
    ptr::NonNullRef<ErasedTy>,
    ::core::marker::PhantomData<&'a ()>,
);

pub
trait ReprCTrait {
    type VTable : ConcreteReprC;

    unsafe
    fn drop_ptr (
        ptr: ptr::NonNullOwned<ty::Erased>,
        vtable: &'_ Self::VTable,
    )
    ;
}

mod ty {
    #![allow(warnings)]

    #[super::derive_ReprC]
    #[repr(opaque)]
    pub
    struct Erased(());
}

pub
trait VirtualPtrFrom<T> : ReprCTrait {
    fn into_virtual_ptr (
        this: T,
    ) -> VirtualPtr<Self>
    ;
}

match_! {(
    ['r] &'r T,
    ['r] &'r mut T,
    ['r] ::core::pin::Pin<&'r T>,
    ['r] ::core::pin::Pin<&'r mut T>,
    #[apply(cfg_alloc)]
    [] rust::Box<T>,
    #[apply(cfg_alloc)]
    [] ::core::pin::Pin<rust::Box<T>>,
    #[apply(cfg_alloc)]
    [] ::std::rc::Rc<T>,
    #[apply(cfg_std)]
    [] ::std::sync::Arc<T>,
) {
    (
        $(
            $(#[$cfg:meta])?
            [$($($generics:tt)+)?] $T:ty
        ),* $(,)?
    ) => (
        $(
            $(#[$cfg])?
            impl<$($($generics)+ ,)? T, DynTrait>
                From<$T>
            for
                VirtualPtr<DynTrait>
            where
                DynTrait : ?Sized + VirtualPtrFrom<$T>,
            {
                fn from (it: $T)
                  -> VirtualPtr<DynTrait>
                {
                    DynTrait::into_virtual_ptr(it)
                }
            }
        )*
    )
}}

pub
trait DynClone : ReprCTrait {
    fn dyn_clone (_: &VirtualPtr<Self>)
      -> VirtualPtr<Self>
    ;
}

impl<DynTrait : ?Sized + DynClone> Clone for VirtualPtr<DynTrait> {
    fn clone (self: &'_ VirtualPtr<DynTrait>)
      -> VirtualPtr<DynTrait>
    {
        DynTrait::dyn_clone(self)
    }
}

use hack::VirtualPtr_;
mod hack {
    use ::safer_ffi::layout;

    #[super::derive_ReprC]
    #[repr(C)]
    #[allow(missing_debug_implementations)]
    pub
    struct VirtualPtr_<Ptr, VTable> {
        pub(in super) ptr: Ptr,
        pub(in super) vtable: VTable,
    }

    unsafe
    impl<Ptr, VTable>
        layout::__HasNiche__
    for
        VirtualPtr_<Ptr, VTable>
    where
        Ptr : layout::ConcreteReprC + layout::__HasNiche__,
        VTable : layout::ConcreteReprC,
    {
        fn is_niche (it: &'_ <Self as super::ReprC>::CLayout)
          -> bool
        {
            Ptr::is_niche(&it.ptr)
        }
    }
}

#[derive_ReprC]
#[repr(transparent)]
pub
struct VirtualPtr<DynTrait : ?Sized + ReprCTrait>(
    VirtualPtr_<
        /* ptr: */ ptr::NonNullOwned<ty::Erased>,
        /* vtable: */ DynTrait::VTable,
    >
);

impl<DynTrait : ?Sized + ReprCTrait>
    Drop
for
    VirtualPtr<DynTrait>
{
    fn drop (self: &'_ mut VirtualPtr<DynTrait>)
    {
        unsafe {
            DynTrait::drop_ptr(self.0.ptr.copy(), self.__vtable())
        }
    }
}

impl<DynTrait : ?Sized + ReprCTrait> VirtualPtr<DynTrait> {
    pub
    unsafe
    fn from_raw_parts (
        ptr: ptr::NonNullOwned<ty::Erased>,
        vtable: DynTrait::VTable,
    ) -> VirtualPtr<DynTrait>
    {
        Self(VirtualPtr_ { ptr, vtable })
    }

    pub
    fn __ptr (self: &'_ VirtualPtr<DynTrait>)
      -> ptr::NonNull<ty::Erased>
    {
        self.0.ptr.0
    }

    pub
    fn __vtable<'vtable> (self: &'_ VirtualPtr<DynTrait>)
      -> &'_ DynTrait::VTable
    {
      &self.0.vtable
    }
}

unsafe
impl<DynTrait : ?Sized + ReprCTrait>
    Send
for
    VirtualPtr<DynTrait>
where
    DynTrait : Send,
{}

unsafe
impl<DynTrait : ?Sized + ReprCTrait>
    Sync
for
    VirtualPtr<DynTrait>
where
    DynTrait : Sync,
{}

impl<DynTrait : ?Sized + ReprCTrait>
    ::core::fmt::Debug
for
    VirtualPtr<DynTrait>
{
    fn fmt (
        self: &'_ VirtualPtr<DynTrait>,
        fmt: &'_ mut ::core::fmt::Formatter<'_>,
    ) -> ::core::fmt::Result
    {
        fmt .debug_struct(::core::any::type_name::<Self>())
            .field("ptr", &format_args!("{:p}", self.__ptr()))
            .field("vtable", &format_args!("{:p}", self.__vtable()))
            .finish()
    }
}