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
//
// Copyright (c) 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   Pierre Avital, <pierre.avital@me.com>
//

use crate as stabby;
use crate::vtable::*;

/// Indicates that `Self` can be used as a pointer in dynptrs.
pub trait IPtr {
    /// # Safety
    /// This function implies an implicit cast of the reference
    unsafe fn as_ref<U: Sized>(&self) -> &U;
}
/// Indicates that `Self` can be used as an unconditionally mutable pointer in dynptrs.
pub trait IPtrMut: IPtr {
    /// # Safety
    /// This function implies an implicit cast of the reference
    unsafe fn as_mut<U: Sized>(&mut self) -> &mut U;
}
/// Indicates that `Self` can be used as a conditionally mutable pointer in dynptrs.
pub trait IPtrTryAsMut {
    /// # Safety
    /// This function implies an implicit cast of the reference
    unsafe fn try_as_mut<U: Sized>(&mut self) -> Option<&mut U>;
}
impl<T: IPtrMut> IPtrTryAsMut for T {
    unsafe fn try_as_mut<U>(&mut self) -> Option<&mut U> {
        Some(self.as_mut())
    }
}
/// Provides drop support in dynptr for pointers that have at least partial ownership of their pointee.
///
/// `drop` is the drop function of the pointee.
pub trait IPtrOwned {
    /// Called instead of `Drop::drop` when the dynptr is dropped.
    fn drop(this: &mut core::mem::ManuallyDrop<Self>, drop: unsafe extern "C" fn(&mut ()));
}
/// Provides Clone support for smart pointers that allow it.
pub trait IPtrClone: IPtrOwned {
    /// Clone the pointer.
    fn clone(this: &Self) -> Self;
}
impl<'a, T> IPtr for &'a T {
    unsafe fn as_ref<U>(&self) -> &U {
        core::mem::transmute(self)
    }
}
impl<'a, T> IPtr for &'a mut T {
    unsafe fn as_ref<U>(&self) -> &U {
        core::mem::transmute(self)
    }
}
impl<T> IPtrMut for &mut T {
    unsafe fn as_mut<U>(&mut self) -> &mut U {
        core::mem::transmute(self)
    }
}
impl<T> IPtrOwned for &mut T {
    fn drop(_: &mut core::mem::ManuallyDrop<Self>, _: unsafe extern "C" fn(&mut ())) {}
}

/// Used to turn a pointer into a dynamic pointer.
pub trait IntoDyn {
    /// A pointer type with type information discarded.
    type Anonymized;
    /// The original pointee.
    type Target;
    /// Drop the type information from the pointer.
    fn anonimize(self) -> Self::Anonymized;
}
impl<'a, T> IntoDyn for &'a T {
    type Anonymized = &'a ();
    type Target = T;
    fn anonimize(self) -> Self::Anonymized {
        unsafe { core::mem::transmute(self) }
    }
}
impl<'a, T> IntoDyn for &'a mut T {
    type Anonymized = &'a mut ();
    type Target = T;
    fn anonimize(self) -> Self::Anonymized {
        unsafe { core::mem::transmute(self) }
    }
}

#[stabby::stabby]
#[derive(Clone, Copy)]
/// A stable `&'a dyn Traits`
pub struct DynRef<'a, Vt: 'static> {
    ptr: &'a (),
    vtable: &'a Vt,
    unsend: core::marker::PhantomData<*mut ()>,
}

impl<'a, Vt: Copy + 'a> DynRef<'a, Vt> {
    /// Access the data pointer.
    pub const fn ptr(&self) -> &() {
        self.ptr
    }
    /// Access the vtable.
    pub const fn vtable(&self) -> &Vt {
        self.vtable
    }

    /// Allows casting a `dyn A + B` into `dyn A`.
    ///
    /// Note that you can only remove the outermost (rightmost in dyn syntax) trait at a time,
    /// except `Send` and `Sync` that may both be kept, or both be removed.
    pub fn into_super<Super>(self) -> Super
    where
        Self: IntoSuperTrait<Super>,
    {
        IntoSuperTrait::into_super(self)
    }
    /// Downcasts the reference based on vtable equality.
    ///
    /// This implies that this downcast will always yield `None` when attempting to downcast
    /// values constructed accross an FFI.
    ///
    /// # Safety
    /// This may have false positives if all of the following applies:
    /// - `self` was built from `&U`, within the same FFI-boundary,
    /// - `T` and `U` have identical implementations for all methods of the vtable,
    /// - the compiler chose to merge these implementations, making `T` and `U` share
    ///   their function pointers.
    ///
    /// While all of these factors aligning is unlikely, you should be aware of this if you
    /// plan on using methods of `T` that wouldn't be valid for `U`.
    pub unsafe fn downcast<T>(&self) -> Option<&T>
    where
        Vt: PartialEq + IConstConstructor<'a, T>,
    {
        (self.vtable == Vt::VTABLE).then(|| unsafe { self.ptr.as_ref() })
    }
    /// Downcasts the reference based on its reflection report.
    pub fn stable_downcast<T: crate::IStable, Path>(&self) -> Option<&T>
    where
        Vt: TransitiveDeref<crate::vtable::StabbyVtableAny, Path>,
    {
        (self.report() == T::REPORT).then(|| unsafe { self.ptr.as_ref() })
    }
}
#[stabby::stabby]
/// A stable trait object (or a stable `&mut dyn`)
pub struct Dyn<'a, P: IPtrOwned + 'a, Vt: HasDropVt + 'static> {
    pub(crate) ptr: core::mem::ManuallyDrop<P>,
    pub(crate) vtable: &'static Vt,
    pub(crate) unsend: core::marker::PhantomData<&'a P>,
}

/// Allows casting a `dyn A + B` into `dyn A`.
pub trait IntoSuperTrait<Super> {
    /// Cast `dyn A + B` into `dyn A`.
    fn into_super(this: Self) -> Super;
}
macro_rules! impl_super {
    ($from: ty, $to: ty, $($generics: tt)*) => {
        impl<'a, P: IPtrOwned + 'a + Sized, $($generics)*> IntoSuperTrait<Dyn<'a, P, $to>> for Dyn<'a, P, $from>
        {
            fn into_super(this: Self) -> Dyn<'a, P, $to> {
                let ptr = &this as *const _;
                core::mem::forget(this);
                unsafe { core::ptr::read(ptr as *const _) }
            }
        }
        impl<'a,  $($generics)*> IntoSuperTrait<DynRef<'a, $to>> for DynRef<'a, $from>
        {
            fn into_super(this: Self) -> DynRef<'a, $to> {
                let ptr = &this as *const _;
                unsafe { core::ptr::read(ptr as *const _) }
            }
        }
    };
}
impl_super!(VTable<Head, Tail>, Tail, Head, Tail: HasDropVt + 'static);
impl_super!(VtSend<Vt>, Vt, Vt: HasDropVt + 'static);
impl_super!(VtSync<Vt>, Vt, Vt: HasDropVt + 'static);
impl_super!(VtSync<VtSend<Vt>>, Vt, Vt: HasDropVt + 'static);
impl_super!(VtSend<VtSync<Vt>>, Vt, Vt: HasDropVt + 'static);
impl_super!(VtSync<VtSend<Vt>>, VtSync<Vt>, Vt: HasDropVt + 'static);
impl_super!(VtSend<VtSync<Vt>>, VtSend<Vt>, Vt: HasDropVt + 'static);
impl_super!(VtSend<VTable<Head, Tail>>, Tail, Head, Tail: HasDropVt + 'static);
impl_super!(VtSync<VTable<Head, Tail>>, Tail, Head, Tail: HasDropVt + 'static);
impl_super!(VtSync<VtSend<VTable<Head, Tail>>>, Tail, Head, Tail: HasDropVt + 'static);
impl_super!(VtSend<VtSync<VTable<Head, Tail>>>, Tail, Head, Tail: HasDropVt + 'static);
impl_super!(VtSend<VTable<Head, Tail>>, VtSend<Tail>, Head, Tail: HasDropVt + 'static);
impl_super!(VtSync<VTable<Head, Tail>>, VtSync<Tail>, Head, Tail: HasDropVt + 'static);
impl_super!(VtSync<VtSend<VTable<Head, Tail>>>, VtSync<VtSend<Tail>>, Head, Tail: HasDropVt + 'static);
impl_super!(VtSend<VtSync<VTable<Head, Tail>>>, VtSend<VtSync<Tail>>, Head, Tail: HasDropVt + 'static);

impl<'a, P: IPtrOwned + IPtrClone, Vt: HasDropVt + 'a> Clone for Dyn<'a, P, Vt> {
    fn clone(&self) -> Self {
        Self {
            ptr: core::mem::ManuallyDrop::new(IPtrClone::clone(&self.ptr)),
            vtable: self.vtable,
            unsend: self.unsend,
        }
    }
}
impl<'a, P: IPtrOwned + IPtr, Vt: HasDropVt + 'a> Dyn<'a, P, Vt> {
    /// Access the data pointer immutably.
    pub fn ptr(&self) -> &P {
        &self.ptr
    }
    /// Access the data pointer mutably.
    pub fn ptr_mut(&mut self) -> &mut P {
        &mut self.ptr
    }
    /// Access the vtable.
    pub const fn vtable(&self) -> &'a Vt {
        self.vtable
    }
    /// Borrow into an ABI-stable `&dyn Traits`
    pub fn as_ref(&self) -> DynRef<'_, Vt> {
        DynRef {
            ptr: unsafe { self.ptr.as_ref() },
            vtable: self.vtable,
            unsend: core::marker::PhantomData,
        }
    }
    /// Borrow into an ABI-stable `&mut dyn Traits`
    pub fn as_mut(&mut self) -> Dyn<&mut (), Vt>
    where
        P: IPtrMut,
    {
        Dyn {
            ptr: unsafe { core::mem::ManuallyDrop::new(self.ptr.as_mut()) },
            vtable: self.vtable,
            unsend: core::marker::PhantomData,
        }
    }
    /// Attempt to borrow into an ABI-stable `&mut dyn Traits`
    pub fn try_as_mut(&mut self) -> Option<Dyn<&mut (), Vt>>
    where
        P: IPtrTryAsMut,
    {
        Some(Dyn {
            ptr: unsafe { core::mem::ManuallyDrop::new(self.ptr.try_as_mut()?) },
            vtable: self.vtable,
            unsend: core::marker::PhantomData,
        })
    }

    /// Allows casting a `dyn A + B` into `dyn A`.
    ///
    /// Note that you can only remove the outermost (rightmost in dyn syntax) trait at a time,
    /// except `Send` and `Sync` that may both be kept, or both be removed.
    pub fn into_super<Super>(self) -> Super
    where
        Self: IntoSuperTrait<Super>,
    {
        IntoSuperTrait::into_super(self)
    }

    /// Downcasts the reference based on vtable equality.
    ///
    /// This implies that this downcast will always yield `None` when attempting to downcast
    /// values constructed accross an FFI.
    ///
    /// # Safety
    /// This may have false positives if all of the following applies:
    /// - `self` was built from `&U`, within the same FFI-boundary,
    /// - `T` and `U` have identical implementations for all methods of the vtable,
    /// - the compiler chose to merge these implementations, making `T` and `U` share
    ///   their function pointers.
    ///
    /// While all of these factors aligning is unlikely, you should be aware of this if you
    /// plan on using methods of `T` that wouldn't be valid for `U`.
    pub unsafe fn downcast_ref<T>(&self) -> Option<&T>
    where
        Vt: PartialEq + Copy + IConstConstructor<'a, T>,
    {
        (self.vtable == Vt::VTABLE).then(|| unsafe { self.ptr.as_ref() })
    }
    /// Downcasts the reference based on its reflection report.
    pub fn stable_downcast_ref<T: crate::IStable, Path>(&self) -> Option<&T>
    where
        Vt: TransitiveDeref<crate::vtable::StabbyVtableAny, Path> + IConstConstructor<'a, T>,
    {
        (self.report() == T::REPORT).then(|| unsafe { self.ptr.as_ref() })
    }
    /// Downcasts the mutable reference based on vtable equality.
    ///
    /// This implies that this downcast will always yield `None` when attempting to downcast
    /// values constructed accross an FFI.
    ///
    /// # Safety
    /// This may have false positives if all of the following applies:
    /// - `self` was built from `&U`, within the same FFI-boundary,
    /// - `T` and `U` have identical implementations for all methods of the vtable,
    /// - the compiler chose to merge these implementations, making `T` and `U` share
    ///   their function pointers.
    ///
    /// While all of these factors aligning is unlikely, you should be aware of this if you
    /// plan on using methods of `T` that wouldn't be valid for `U`.
    pub unsafe fn downcast_mut<T>(&mut self) -> Option<&mut T>
    where
        Vt: PartialEq + Copy + IConstConstructor<'a, T>,
        P: IPtrMut,
    {
        (self.vtable == Vt::VTABLE).then(|| unsafe { self.ptr.as_mut() })
    }
    /// Downcasts the reference based on its reflection report.
    pub fn stable_downcast_mut<T: crate::IStable, Path>(&mut self) -> Option<&mut T>
    where
        Vt: TransitiveDeref<crate::vtable::StabbyVtableAny, Path> + IConstConstructor<'a, T>,
        P: IPtrMut,
    {
        (self.report() == T::REPORT).then(|| unsafe { self.ptr.as_mut() })
    }
}

impl<
        'a,
        Vt: HasDropVt + Copy + IConstConstructor<'static, P::Target> + 'static,
        P: IntoDyn + 'a,
    > From<P> for Dyn<'a, P::Anonymized, Vt>
where
    P::Anonymized: IPtrOwned,
{
    fn from(value: P) -> Self {
        Self {
            ptr: core::mem::ManuallyDrop::new(value.anonimize()),
            vtable: Vt::VTABLE,
            unsend: core::marker::PhantomData,
        }
    }
}

impl<'a, P: IPtrOwned, Vt: HasDropVt> Drop for Dyn<'a, P, Vt> {
    fn drop(&mut self) {
        P::drop(&mut self.ptr, *unsafe {
            self.vtable.drop_vt().drop.as_ref_unchecked()
        })
    }
}

impl<'a, T, Vt: Copy + IConstConstructor<'a, T>> From<&'a T> for DynRef<'a, Vt> {
    fn from(value: &'a T) -> Self {
        unsafe {
            DynRef {
                ptr: core::mem::transmute(value),
                vtable: Vt::VTABLE,
                unsend: core::marker::PhantomData,
            }
        }
    }
}

unsafe impl<'a, Vt: HasSendVt> Send for DynRef<'a, Vt> {}
unsafe impl<'a, Vt: HasSyncVt> Sync for DynRef<'a, Vt> {}

unsafe impl<'a, P: IPtrOwned + Send, Vt: HasSendVt + HasDropVt> Send for Dyn<'a, P, Vt> {}
unsafe impl<'a, P: IPtrOwned + Sync, Vt: HasSyncVt + HasDropVt> Sync for Dyn<'a, P, Vt> {}