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
use std::{
    cmp::{Ord, PartialEq, PartialOrd},
    fmt::{Debug, Display},
    hash::Hash,
    marker::PhantomData,
};

use crate::{
    erased_types::{c_functions, trait_objects, FormattingMode, InterfaceType},
    inline_storage::InlineStorage,
    marker_type::{ErasedObject, UnsafeIgnoredType},
    nonexhaustive_enum::{
        alt_c_functions, EnumInfo, GetEnumInfo, GetSerializeEnumProxy, NonExhaustive, SerializeEnum,
    },
    prefix_type::{panic_on_missing_fieldname, WithMetadata},
    sabi_types::{RMut, RRef},
    std_types::{RBoxError, RCmpOrdering, ROption, RResult, RString},
    type_level::{
        impl_enum::{Implemented, Unimplemented},
        trait_marker,
    },
    utils::Transmuter,
    StableAbi,
};

#[doc(hidden)]
pub struct Private<T: ?Sized, S: ?Sized, I: ?Sized>(
    PhantomData<(PhantomData<T>, PhantomData<S>, PhantomData<I>)>,
);

/// Gets the vtable of `NonExhaustive<Self,S,I>`.
///
/// This trait is only exposed for use in bounds,
/// and cannot be implemented outside of `abi_stable`.
pub trait GetVTable<S, I>: Sized {
    // Using privacy to make it impossible to implement this trait outside this module.
    #[doc(hidden)]
    const __HIDDEN_10341423423__: Private<Self, S, I>;

    #[doc(hidden)]
    const VTABLE_VAL: NonExhaustiveVtable<Self, S, I>;

    staticref! {
        #[doc(hidden)]
        const VTABLE_WM: WithMetadata<NonExhaustiveVtable<Self,S,I>> =
            WithMetadata::new(Self::VTABLE_VAL)
    }

    /// The vtable
    const VTABLE: NonExhaustiveVtable_Ref<Self, S, I> =
        NonExhaustiveVtable_Ref(Self::VTABLE_WM.as_prefix());
}

/// The vtable for NonExhaustive.
#[repr(C)]
#[derive(StableAbi)]
#[sabi(
    bound(I: GetSerializeEnumProxy<E>),
    bound(<I as GetSerializeEnumProxy<E>>::ProxyType: StableAbi),
    not_stableabi(E,S,I),
    missing_field(default),
    kind(Prefix(prefix_ref_docs = "\
        A reference to the vtable of a non-exhaustive enum,
    ")),
    with_field_indices,
    //debug_print,
)]
pub struct NonExhaustiveVtable<E, S, I> {
    pub(crate) _sabi_tys: UnsafeIgnoredType<(E, S, I)>,

    /// The `EnumInfo` for the enum.
    pub enum_info: &'static EnumInfo,

    pub(crate) _sabi_drop: unsafe extern "C" fn(this: RMut<'_, ErasedObject>),

    #[sabi(unsafe_opaque_field)]
    pub(crate) _sabi_clone: Option<
        unsafe extern "C" fn(
            RRef<'_, ErasedObject>,
            NonExhaustiveVtable_Ref<E, S, I>,
        ) -> NonExhaustive<E, S, I>,
    >,

    pub(crate) _sabi_debug: Option<
        unsafe extern "C" fn(
            RRef<'_, ErasedObject>,
            FormattingMode,
            &mut RString,
        ) -> RResult<(), ()>,
    >,
    pub(crate) _sabi_display: Option<
        unsafe extern "C" fn(
            RRef<'_, ErasedObject>,
            FormattingMode,
            &mut RString,
        ) -> RResult<(), ()>,
    >,
    #[sabi(unsafe_change_type =
        unsafe extern "C" fn(
            RRef<'_, ErasedObject>
        )->RResult< <I as GetSerializeEnumProxy<E>>::ProxyType, RBoxError>
    )]
    pub(crate) erased_sabi_serialize:
        Option<unsafe extern "C" fn(RRef<'_, ErasedObject>) -> RResult<ErasedObject, RBoxError>>,
    pub(crate) _sabi_partial_eq:
        Option<unsafe extern "C" fn(RRef<'_, ErasedObject>, RRef<'_, ErasedObject>) -> bool>,
    pub(crate) _sabi_cmp: Option<
        unsafe extern "C" fn(RRef<'_, ErasedObject>, RRef<'_, ErasedObject>) -> RCmpOrdering,
    >,
    pub(crate) _sabi_partial_cmp: Option<
        unsafe extern "C" fn(
            RRef<'_, ErasedObject>,
            RRef<'_, ErasedObject>,
        ) -> ROption<RCmpOrdering>,
    >,
    #[sabi(last_prefix_field)]
    pub(crate) _sabi_hash:
        Option<unsafe extern "C" fn(RRef<'_, ErasedObject>, trait_objects::HasherObject<'_>)>,
}

unsafe impl<E, S, I> Sync for NonExhaustiveVtable<E, S, I> {}
unsafe impl<E, S, I> Send for NonExhaustiveVtable<E, S, I> {}

impl<E, S, I> GetVTable<S, I> for E
where
    S: InlineStorage,
    I: InterfaceType,
    E: GetEnumInfo,
    I::Sync: RequiresSync<E, S, I>,
    I::Send: RequiresSend<E, S, I>,
    I::Clone: InitCloneField<E, S, I>,
    I::Debug: InitDebugField<E, S, I>,
    I::Display: InitDisplayField<E, S, I>,
    I::Serialize: InitSerializeField<E, S, I>,
    I::PartialEq: InitPartialEqField<E, S, I>,
    I::PartialOrd: InitPartialOrdField<E, S, I>,
    I::Ord: InitOrdField<E, S, I>,
    I::Hash: InitHashField<E, S, I>,
{
    const __HIDDEN_10341423423__: Private<Self, S, I> = Private(PhantomData);

    #[doc(hidden)]
    const VTABLE_VAL: NonExhaustiveVtable<E, S, I> = NonExhaustiveVtable {
        _sabi_tys: UnsafeIgnoredType::DEFAULT,
        enum_info: E::ENUM_INFO,
        _sabi_drop: alt_c_functions::drop_impl::<E>,
        _sabi_clone: <I::Clone as InitCloneField<E, S, I>>::VALUE,
        _sabi_debug: <I::Debug as InitDebugField<E, S, I>>::VALUE,
        _sabi_display: <I::Display as InitDisplayField<E, S, I>>::VALUE,
        erased_sabi_serialize: <I::Serialize as InitSerializeField<E, S, I>>::VALUE,
        _sabi_partial_eq: <I::PartialEq as InitPartialEqField<E, S, I>>::VALUE,
        _sabi_partial_cmp: <I::PartialOrd as InitPartialOrdField<E, S, I>>::VALUE,
        _sabi_cmp: <I::Ord as InitOrdField<E, S, I>>::VALUE,
        _sabi_hash: <I::Hash as InitHashField<E, S, I>>::VALUE,
    };
}

type UnerasedSerializeFn<E, I> =
    unsafe extern "C" fn(
        RRef<'_, ErasedObject>,
    ) -> RResult<<I as GetSerializeEnumProxy<E>>::ProxyType, RBoxError>;

impl<E, S, I> NonExhaustiveVtable_Ref<E, S, I> {
    pub(crate) fn serialize(self) -> UnerasedSerializeFn<E, I>
    where
        I: InterfaceType<Serialize = Implemented<trait_marker::Serialize>>,
        I: GetSerializeEnumProxy<E>,
    {
        unsafe {
            std::mem::transmute::<
                unsafe extern "C" fn(RRef<'_, ErasedObject>) -> RResult<ErasedObject, RBoxError>,
                UnerasedSerializeFn<E, I>,
            >(self.priv_serialize())
        }
    }
}

use self::trait_bounds::*;
pub mod trait_bounds {
    use super::*;

    macro_rules! declare_conditional_marker {
        (
            type $selector:ident;
            trait $trait_name:ident[$self_:ident,$Filler:ident,$OrigPtr:ident]
            where [ $($where_preds:tt)* ]
        ) => (
            pub trait $trait_name<$self_,$Filler,$OrigPtr>{}

            impl<$self_,$Filler,$OrigPtr> $trait_name<$self_,$Filler,$OrigPtr>
            for Unimplemented<trait_marker::$selector>
            {}

            impl<$self_,$Filler,$OrigPtr> $trait_name<$self_,$Filler,$OrigPtr>
            for Implemented<trait_marker::$selector>
            where
                $($where_preds)*
            {}
        )
    }

    macro_rules! declare_field_initalizer {
        (
            type $selector:ident;
            trait $trait_name:ident[$enum_:ident,$filler:ident,$interf:ident]
            $( where_for_both[ $($where_preds_both:tt)* ] )?
            where [ $($where_preds:tt)* ]
            $priv_field:ident,$field:ident : $field_ty:ty;
            field_index=$field_index:ident;
            value=$field_value:expr,
        ) => (
            pub trait $trait_name<$enum_,$filler,$interf>
            where
                $($($where_preds_both)*)?
            {
                const VALUE:Option<$field_ty>;
            }

            impl<$enum_,$filler,$interf> $trait_name<$enum_,$filler,$interf>
            for Unimplemented<trait_marker::$selector>
            where
                $($($where_preds_both)*)?
            {
                const VALUE:Option<$field_ty>=None;
            }

            impl<$enum_,$filler,$interf> $trait_name<$enum_,$filler,$interf>
            for Implemented<trait_marker::$selector>
            where
                $($($where_preds_both)*)?
                $($where_preds)*
            {
                const VALUE:Option<$field_ty>=Some($field_value);
            }

            impl<E,S,$interf> NonExhaustiveVtable_Ref<E,S,$interf>{
                #[doc = concat!(
                    "Fallibly accesses the `",
                    stringify!($field),
                    "` field, panicking if it doesn't exist."
                )]

                pub fn $field(self) -> $field_ty
                where
                    $interf:InterfaceType<$selector=Implemented<trait_marker::$selector>>,
                {
                    match self.$priv_field().into() {
                        Some(v)=>v,
                        None=>panic_on_missing_fieldname::<
                            NonExhaustiveVtable<E,S,$interf>,
                        >(
                            Self::$field_index,
                            self._prefix_type_layout(),
                        )
                    }
                }
            }
        )
    }

    declare_conditional_marker! {
        type Send;
        trait RequiresSend[E,S,I]
        where [ E:Send ]
    }

    declare_conditional_marker! {
        type Sync;
        trait RequiresSync[E,S,I]
        where [ E:Sync ]
    }

    declare_field_initalizer! {
        type Clone;
        trait InitCloneField[E,S,I]
        where_for_both[ E:GetEnumInfo, ]
        where [ E:Clone ]
        _sabi_clone,clone_:
            unsafe extern "C" fn(
                RRef<'_, ErasedObject>,
                NonExhaustiveVtable_Ref<E,S,I>
            )->NonExhaustive<E,S,I>;
        field_index=field_index_for__sabi_clone;
        value=alt_c_functions::clone_impl::<E,S,I>,
    }
    declare_field_initalizer! {
        type Debug;
        trait InitDebugField[E,S,I]
        where [ E:Debug ]
        _sabi_debug,debug:
            unsafe extern "C" fn(
                RRef<'_, ErasedObject>,
                FormattingMode,
                &mut RString,
            )->RResult<(),()>;
        field_index=field_index_for__sabi_debug;
        value=c_functions::debug_impl::<E>,
    }
    declare_field_initalizer! {
        type Display;
        trait InitDisplayField[E,S,I]
        where [ E:Display ]
        _sabi_display,display:
            unsafe extern "C" fn(
                RRef<'_, ErasedObject>,
                FormattingMode,
                &mut RString,
            )->RResult<(),()>;
        field_index=field_index_for__sabi_display;
        value=c_functions::display_impl::<E>,
    }
    declare_field_initalizer! {
        type Serialize;
        trait InitSerializeField[E,S,I]
        where [ I:SerializeEnum<E> ]
        erased_sabi_serialize,priv_serialize:
            unsafe extern "C" fn(RRef<'_, ErasedObject>)->RResult<ErasedObject,RBoxError>;
        field_index=field_index_for_erased_sabi_serialize;
        value=unsafe{
            Transmuter::<
                unsafe extern "C" fn(
                    RRef<'_, ErasedObject>
                )->RResult<<I as SerializeEnum<E>>::Proxy,RBoxError>,
                unsafe extern "C" fn(RRef<'_, ErasedObject>)->RResult<ErasedObject,RBoxError>
            >{
                from:alt_c_functions::serialize_impl::<E,I>
            }.to
        },
    }
    declare_field_initalizer! {
        type PartialEq;
        trait InitPartialEqField[E,S,I]
        where_for_both[ E:GetEnumInfo, ]
        where [ E:PartialEq ]
        _sabi_partial_eq,partial_eq: unsafe extern "C" fn(RRef<'_, ErasedObject>,RRef<'_, ErasedObject>)->bool;
        field_index=field_index_for__sabi_partial_eq;
        value=alt_c_functions::partial_eq_impl::<E,S,I>,
    }
    declare_field_initalizer! {
        type PartialOrd;
        trait InitPartialOrdField[E,S,I]
        where_for_both[ E:GetEnumInfo, ]
        where [ E:PartialOrd ]
        _sabi_partial_cmp,partial_cmp:
            unsafe extern "C" fn(RRef<'_, ErasedObject>,RRef<'_, ErasedObject>)->ROption<RCmpOrdering>;
        field_index=field_index_for__sabi_partial_cmp;
        value=alt_c_functions::partial_cmp_ord::<E,S,I>,
    }
    declare_field_initalizer! {
        type Ord;
        trait InitOrdField[E,S,I]
        where_for_both[ E:GetEnumInfo, ]
        where [ E:Ord ]
        _sabi_cmp,cmp:
            unsafe extern "C" fn(
                RRef<'_, ErasedObject>,
                RRef<'_, ErasedObject>,
            )->RCmpOrdering;
        field_index=field_index_for__sabi_cmp;
        value=alt_c_functions::cmp_ord::<E,S,I>,
    }
    declare_field_initalizer! {
        type Hash;
        trait InitHashField[E,S,I]
        where [ E:Hash ]
        _sabi_hash,hash: unsafe extern "C" fn(RRef<'_, ErasedObject>,trait_objects::HasherObject<'_>);
        field_index=field_index_for__sabi_hash;
        value=c_functions::hash_Hash::<E>,
    }
}