pub trait DeserializeEnum<'borr, NE>: InterfaceType {
type Proxy;
// Required method
fn deserialize_enum(s: Self::Proxy) -> Result<NE, RBoxError>;
}
Expand description
Describes how a nonexhaustive enum is deserialized.
Generally this delegates to a library function,
so that the implementation can be delegated
to the implementation crate
.
This is generally implemented by the interface of an enum
(Enum_Interface
for Enum
),which also implements InterfaceType
).
The NE
type parameter is expected to be NonExhaustive
.
§Example
use abi_stable::{
nonexhaustive_enum::{DeserializeEnum, NonExhaustive, NonExhaustiveFor},
external_types::RawValueRef,
std_types::{RBoxError, RResult, ROk, RErr, RStr, RString},
rstr, StableAbi,
};
let input = r#"{"C": {"name": "hello"}}"#;
let ne = serde_json::from_str::<NonExhaustiveFor<Foo>>(input).unwrap();
assert_eq!(ne, Foo::C{name: "hello".into()});
#[repr(u8)]
#[derive(StableAbi, Debug, PartialEq, Eq, serde::Deserialize)]
#[sabi(kind(WithNonExhaustive(
size = 64,
traits(Debug, PartialEq, Eq, Deserialize)
)))]
pub enum Foo {
A,
B(i8),
C {
name: RString
},
}
impl<'borr> DeserializeEnum<'borr, NonExhaustiveFor<Foo>> for Foo_Interface {
/// The intermediate type the `NE` is converted from,to deserialize it.
type Proxy = RawValueRef<'borr>;
/// Deserializes an enum from its proxy type.
fn deserialize_enum(s: Self::Proxy) -> Result<NonExhaustiveFor<Foo>, RBoxError> {
deserialize_foo(s.get_rstr()).into_result()
}
}
/////////////
// everything below could be defined in an implementation crate
//
// This allows the library that defines the enum to add variants,
// and deserialize the variants that it added,
// regardless of whether the dependent crates know about those variants.
extern "C" fn deserialize_foo(s: RStr<'_>) -> RResult<NonExhaustiveFor<Foo>, RBoxError> {
abi_stable::extern_fn_panic_handling!{
match serde_json::from_str::<Foo>(s.into()) {
Ok(x) => ROk(NonExhaustive::new(x)),
Err(e) => RErr(RBoxError::new(e)),
}
}
}
Required Associated Types§
Required Methods§
sourcefn deserialize_enum(s: Self::Proxy) -> Result<NE, RBoxError>
fn deserialize_enum(s: Self::Proxy) -> Result<NE, RBoxError>
Deserializes an enum from its proxy type.
Object Safety§
This trait is not object safe.