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
use ::ffi_sdk::{ffi_utils::repr_c, FfiError};
pub use cbor_value::CborValueGetters;
mod cbor_value;

pub(crate) trait RefCounted: Clone {
    fn retain(&self) -> Self {
        Clone::clone(self)
    }

    /// This method is added precisely to make `.clone()` calls ambiguous (in
    /// non-`Clone`-generic context). The idea being that in such contexts,
    /// explicit `.retain()` ought to be more readable than `.clone`-ing.
    ///
    /// We disable `rustfmt` to let the "suggestion" comment be on the same
    /// line, since that's the only visible line in the "ambiguous method"
    /// message.
    #[rustfmt::skip]
    fn clone(&self) -> Self { // Use `.retain()` instead.
        Clone::clone(self)
    }
}
impl<T: ?Sized> RefCounted for ::std::rc::Rc<T> {}
impl<T: ?Sized> RefCounted for ::std::sync::Arc<T> {}

pub(crate) trait FfiResultIntoRustResult {
    type Result;
    fn into_rust_result(self) -> Self::Result;
}

impl<T: ::safer_ffi::layout::ReprC> FfiResultIntoRustResult for repr_c::FfiResult<T> {
    type Result = Result<T, repr_c::Box<FfiError>>;

    fn into_rust_result(self) -> Self::Result {
        if let Some(error) = self.error {
            Err(error)
        } else {
            Ok(unsafe {
                if ::core::mem::size_of::<T>() > 0 {
                    ::safer_ffi::layout::from_raw_unchecked(self.success)
                } else {
                    ::core::mem::transmute_copy(&self.success)
                }
            })
        }
    }
}