pub use cbor_value::CborValueGetters;
use ffi_sdk::{ffi_utils::repr_c, FfiError};
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.
    #[allow(dead_code)]
    #[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)
                }
            })
        }
    }
}
