abi_stable/nonexhaustive_enum/
alt_c_functions.rs

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
use std::ptr;

use crate::{
    marker_type::ErasedObject,
    nonexhaustive_enum::{
        vtable::NonExhaustiveVtable_Ref, GetEnumInfo, NonExhaustive, SerializeEnum,
    },
    sabi_types::{RMut, RRef},
    std_types::{RBoxError, RCmpOrdering, ROption, RResult, RSome},
    traits::IntoReprC,
};

pub(crate) unsafe extern "C" fn drop_impl<E>(this: RMut<'_, ErasedObject>) {
    extern_fn_panic_handling! {no_early_return; unsafe {
        let this = this.transmute_into_mut::<E>();
        ptr::drop_in_place(this);
    }}
}

pub(crate) unsafe extern "C" fn clone_impl<E, F, I>(
    this: RRef<'_, ErasedObject>,
    vtable: NonExhaustiveVtable_Ref<E, F, I>,
) -> NonExhaustive<E, F, I>
where
    E: GetEnumInfo,
    E: Clone,
{
    extern_fn_panic_handling! {no_early_return; unsafe {
        let this = this.transmute_into_ref::<E>();
        NonExhaustive::with_vtable(this.clone(), vtable)
    }}
}

pub(crate) unsafe extern "C" fn partial_eq_impl<E, F, I>(
    this: RRef<'_, ErasedObject>,
    other: RRef<'_, ErasedObject>,
) -> bool
where
    E: GetEnumInfo + PartialEq,
{
    extern_fn_panic_handling! {no_early_return;
        let this = unsafe { this.transmute_into_ref::<E>() };
        let other = unsafe { other.transmute_into_ref::<NonExhaustive<E,F,I>>() };
        match other.as_enum() {
            Ok(other)=>this==other,
            Err(_)=>false,
        }
    }
}

pub(crate) unsafe extern "C" fn cmp_ord<E, F, I>(
    this: RRef<'_, ErasedObject>,
    other: RRef<'_, ErasedObject>,
) -> RCmpOrdering
where
    E: GetEnumInfo + Ord,
{
    extern_fn_panic_handling! {no_early_return;
        let this = unsafe { this.transmute_into_ref::<E>() };
        let other = unsafe { other.transmute_into_ref::<NonExhaustive<E,F,I>>() };

        match other.as_enum() {
            Ok(other)=>this.cmp(other).into_c(),
            Err(_)=>RCmpOrdering::Less,
        }
    }
}

pub(crate) unsafe extern "C" fn partial_cmp_ord<E, F, I>(
    this: RRef<'_, ErasedObject>,
    other: RRef<'_, ErasedObject>,
) -> ROption<RCmpOrdering>
where
    E: GetEnumInfo + PartialOrd,
{
    extern_fn_panic_handling! {no_early_return;
        let this = unsafe { this.transmute_into_ref::<E>() };
        let other = unsafe { other.transmute_into_ref::<NonExhaustive<E,F,I>>() };

        match other.as_enum() {
            Ok(other)=>this.partial_cmp(other).map(IntoReprC::into_c).into_c(),
            Err(_)=>RSome(RCmpOrdering::Less),
        }
    }
}

pub(crate) unsafe extern "C" fn serialize_impl<E, I>(
    this: RRef<'_, ErasedObject>,
) -> RResult<<I as SerializeEnum<E>>::Proxy, RBoxError>
where
    I: SerializeEnum<E>,
{
    extern_fn_panic_handling! {no_early_return;
        let this = unsafe { this.transmute_into_ref::<E>() };
        I::serialize_enum(this).into()
    }
}