pub trait DeserializeDyn<'borr, D>: InterfaceType<Deserialize = Implemented<Deserialize>> {
    type Proxy;

    // Required method
    fn deserialize_dyn(s: Self::Proxy) -> Result<D, RBoxError>;
}
Expand description

Describes how D is deserialized, using a proxy to do so.

Generally this delegates to a library function, so that the implementation can be delegated to the implementation crate.

Example

use abi_stable::{
    erased_types::{DeserializeDyn, DynTrait},
    external_types::RawValueRef,
    std_types::{RBox, RBoxError, RErr, ROk, RResult, RStr},
    StableAbi,
};

let boxed = serde_json::from_str::<FooBox>(r#"{"field": 10}"#).unwrap();

assert_eq!(*boxed.downcast_as::<Foo>().unwrap(), Foo{field: 10});



type FooBox = DynTrait<'static, RBox<()>, FooInterface>;

/// Implements `InterfaceType`, requiring `Send + Sync + Debug + Eq`
#[repr(C)]
#[derive(StableAbi)]
#[sabi(impl_InterfaceType(Send, Sync, Debug, Eq, Deserialize))]
pub struct FooInterface;

impl<'a> DeserializeDyn<'a, FooBox> for FooInterface {
    type Proxy = RawValueRef<'a>;
     
    fn deserialize_dyn(s: Self::Proxy) -> Result<FooBox, RBoxError> {
        deserialize_foo(s.get_rstr()).into_result()
    }
}

/////////////
// everything below could be defined in an implementation crate

#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
struct Foo {
    field: u32,
}

extern "C" fn deserialize_foo(s: RStr<'_>) -> RResult<FooBox, RBoxError> {
    abi_stable::extern_fn_panic_handling!{
        match serde_json::from_str::<Foo>(s.into()) {
            Ok(x) => ROk(DynTrait::from_value(x)),
            Err(e) => RErr(RBoxError::new(e)),
        }
    }
}

Required Associated Types§

source

type Proxy

The type that is deserialized and then converted into D, with DeserializeDyn::deserialize_dyn.

Required Methods§

source

fn deserialize_dyn(s: Self::Proxy) -> Result<D, RBoxError>

Converts the proxy type into D.

Object Safety§

This trait is not object safe.

Implementors§