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
//! Internal / private prelude

#![allow(unused_imports)]

#[rustfmt::skip]
pub(crate) use {
    ::ffi_sdk::{
        ffi_utils::{c_slice, char_p, repr_c, NonOpaque},
    },
    ::safer_ffi::{
        prelude::Out,
    },
    ::serde::{
        de::DeserializeOwned, Deserialize, Serialize,
    },
    ::std::{
        any::Any,
        borrow::Borrow,
        collections::HashMap,
        ops::Not,
        os::raw::c_void,
        path::{Path, PathBuf},
        pin::Pin,
        sync::Arc,
    },
    ::to_method::{
        To,
    },
    crate::{
        ditto::Ditto,
        error::{DittoError, ErrorKind, Result},
        prelude::*,
        store::{
            collection::type_traits::MutableValue,
            Store,
        },
        utils::{extension_traits::*, Str},
    },
};

pub(crate) mod arc {
    pub use ::std::sync::Weak;
}

pub(crate) mod marker {
    pub(crate) use crate::utils::InvariantLifetimeMarker as InvariantLifetime;
}

/// Since this is a convenience extension trait, we don't really care about coherence issues.
/// We *know* we won't have overlaps.
///
/// Alas, the blanket impls below will overlap.
/// We work around this limitation by adding a phony generic parameter so as to technically
/// be dealing with impls of distinct traits which therefore do not overlap.
///
/// Should an overlap ever happen, we'd pay the price with type inference errors (we'd be expected
/// to disambiguate the generic parameter).
pub(crate) trait IntoRustResult<CoherenceDisambiguator = ()>: Sized {
    type Ok;
    fn into_rust_result(self) -> Result<Self::Ok, ::core::num::NonZeroI32>;
    fn ok_or(self, error_kind: ErrorKind) -> Result<Self::Ok, DittoError> {
        self.into_rust_result()
            .map_err(|_| DittoError::from_ffi(error_kind))
    }
    fn ok(self) -> Result<Self::Ok, DittoError> {
        self.ok_or(ErrorKind::Internal)
    }
}

impl IntoRustResult for i32 {
    type Ok = ();

    fn into_rust_result(self: i32) -> Result<Self::Ok, ::core::num::NonZeroI32> {
        if let Some(err) = ::core::num::NonZeroI32::new(self) {
            Err(err)
        } else {
            Ok(())
        }
    }
}

impl<T> IntoRustResult for repr_c::Result_Box<T>
where
    repr_c::Box<T>: ::ffi_sdk::ffi_utils::FfiDrop,
{
    type Ok = repr_c::Box<T>;
    fn into_rust_result(self) -> Result<Self::Ok, ::core::num::NonZeroI32> {
        if let Some(err_code) = ::core::num::NonZeroI32::new(self.status_code) {
            Err(err_code)
        } else {
            Ok(self.ok_value.unwrap())
        }
    }
}

impl<T: NonOpaque> IntoRustResult for repr_c::Result_Vec<T> {
    type Ok = repr_c::Vec<T>;
    fn into_rust_result(self) -> Result<Self::Ok, ::core::num::NonZeroI32> {
        if let Some(err_code) = ::core::num::NonZeroI32::new(self.status_code) {
            Err(err_code)
        } else {
            Ok(self.c_vec.unwrap())
        }
    }
}

impl<T: NonOpaque> IntoRustResult for repr_c::Result_BoxedSlice<T> {
    type Ok = c_slice::Box<T>;
    fn into_rust_result(self) -> Result<Self::Ok, ::core::num::NonZeroI32> {
        if let Some(err_code) = ::core::num::NonZeroI32::new(self.status_code) {
            Err(err_code)
        } else {
            match self.c_slice {
                Some(slice) => Ok(slice),
                None => {
                    // FIXME : status code 0 without slice
                    Err(::core::num::NonZeroI32::new(-1).unwrap())
                }
            }
        }
    }
}

impl<T: NonOpaque> IntoRustResult for repr_c::Result_OptionBoxedSlice<T> {
    type Ok = Option<c_slice::Box<T>>;
    fn into_rust_result(self) -> Result<Self::Ok, ::core::num::NonZeroI32> {
        if let Some(err_code) = ::core::num::NonZeroI32::new(self.status_code) {
            Err(err_code)
        } else {
            Ok(self.c_slice)
        }
    }
}

impl<T: NonOpaque> IntoRustResult for repr_c::Result<T> {
    type Ok = T;
    fn into_rust_result(self) -> Result<T, ::core::num::NonZeroI32> {
        if let Some(err_code) = ::core::num::NonZeroI32::new(self.status_code) {
            Err(err_code)
        } else {
            Ok(self.ok_value)
        }
    }
}

impl<T> IntoRustResult<((),)> for T
where
    Self: ::ffi_sdk::IntoRustResult,
{
    type Ok = <T as ::ffi_sdk::IntoRustResult>::Ok;

    fn into_rust_result(self: T) -> Result<Self::Ok, ::core::num::NonZeroI32> {
        ::ffi_sdk::IntoRustResult::into_rust_result(self)
    }
}