abi_stable::nonexhaustive_enum

Trait SerializeEnum

Source
pub trait SerializeEnum<Enum>: InterfaceType {
    type Proxy;

    // Required method
    fn serialize_enum(this: &Enum) -> Result<Self::Proxy, RBoxError>;
}
Expand description

Describes how some enum is serialized.

This is generally implemented by the interface of an enum (Enum_Interface for Enum),which also implements InterfaceType).

§Example

use abi_stable::{
    external_types::RawValueBox,
    nonexhaustive_enum::{NonExhaustive, SerializeEnum},
    std_types::{RBoxError, RString},
    StableAbi,
};

let ne = NonExhaustive::new(Foo::C{name: "world".into()});
assert_eq!(serde_json::to_string(&ne).unwrap(), r#"{"C":{"name":"world"}}"#);


#[repr(u8)]
#[derive(StableAbi, Debug, PartialEq, Eq, serde::Serialize)]
#[sabi(kind(WithNonExhaustive(
    size = 64,
    traits(Debug, PartialEq, Eq, Serialize)
)))]
pub enum Foo {
    A,
    B(i8),
    C {
        name: RString
    },
}

impl SerializeEnum<Foo> for Foo_Interface {
    /// A type that `Foo` is converted into to be serialized.
    type Proxy = RawValueBox;

    fn serialize_enum(this: &Foo) -> Result<RawValueBox, RBoxError> {
        match serde_json::value::to_raw_value(&this) {
            Ok(v) => Ok(v.into()),
            Err(e) => Err(RBoxError::new(e)),
        }
    }
}

Required Associated Types§

Source

type Proxy

The intermediate type the Enum is converted into,to serialize it.

Required Methods§

Source

fn serialize_enum(this: &Enum) -> Result<Self::Proxy, RBoxError>

Serializes an enum into its proxy type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§