Trait abi_stable::erased_types::SerializeType
source · pub trait SerializeType<'s> {
type Interface: SerializeProxyType<'s>;
// Required method
fn serialize_impl(
&'s self,
) -> Result<<Self::Interface as SerializeProxyType<'s>>::Proxy, RBoxError>;
}
Expand description
Describes how a type is serialized by DynTrait
.
§Example
use abi_stable::{
erased_types::{SerializeType, SerializeProxyType, DynTrait},
external_types::RawValueBox,
std_types::{RBox, RBoxError, RErr, ROk, RResult, RStr},
StableAbi,
};
let boxed = make_foo_box(1234);
let serialized = serde_json::to_string(&boxed).unwrap();
assert_eq!(serialized, r#"{"field":1234}"#);
type FooBox = DynTrait<'static, RBox<()>, FooInterface>;
/// Implements `InterfaceType`, requiring `Send + Sync + Debug + Eq`
#[repr(C)]
#[derive(StableAbi)]
#[sabi(impl_InterfaceType(Send, Sync, Debug, Eq, Serialize))]
pub struct FooInterface;
impl SerializeProxyType<'_> for FooInterface {
type Proxy = RawValueBox;
}
/////////////
// everything below could be defined in an implementation crate
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
struct Foo {
field: u32,
}
impl<'a> SerializeType<'a> for Foo {
type Interface = FooInterface;
fn serialize_impl(&'a self) -> Result<RawValueBox, RBoxError> {
match serde_json::value::to_raw_value::<Foo>(self) {
Ok(x) => Ok(x.into()),
Err(e) => Err(RBoxError::new(e)),
}
}
}
extern "C" fn make_foo_box(field: u32) -> FooBox {
abi_stable::extern_fn_panic_handling!{
FooBox::from_value(Foo{field})
}
}
Required Associated Types§
sourcetype Interface: SerializeProxyType<'s>
type Interface: SerializeProxyType<'s>
An InterfaceType
implementor which determines the
intermediate type through which this is serialized.
Required Methods§
sourcefn serialize_impl(
&'s self,
) -> Result<<Self::Interface as SerializeProxyType<'s>>::Proxy, RBoxError>
fn serialize_impl( &'s self, ) -> Result<<Self::Interface as SerializeProxyType<'s>>::Proxy, RBoxError>
Performs the serialization into the proxy.