pub struct OpenEnums { /* private fields */ }open-enums only.Expand description
The registry of the variants of open enums.
Open enums (see #[deser::open_enum])
are deserialized with the variants registered here, the registry is
given to the deserialization in the Context. A
registry holds the variants of any number of open enums. Types are
variants of an open enum if they implement the trait with
#[deser::variant], which register checks:
use deser::{Context, Deserialize, OpenEnums, Serialize};
#[deser::open_enum(tag = "type")]
pub trait Shape: Send + Sync {}
#[derive(Serialize, Deserialize)]
pub struct Circle {
radius: u32,
}
#[deser::variant]
impl Shape for Circle {}
let mut variants = OpenEnums::new();
variants.register::<dyn Shape, Circle>().unwrap();
let context = Context::with(variants);Libraries that provide variants usually provide a function that registers them. Only the registered variants can be deserialized, which is also a way to restrict what input can create. Serializing does not need the registry. The variants of untagged open enums are tried in the order they are registered.
Implementations§
Source§impl OpenEnums
impl OpenEnums
Sourcepub fn register<O: ?Sized + OpenEnum, T: OpenVariant<O>>(
&mut self,
) -> Result<&mut OpenEnums, Error>
pub fn register<O: ?Sized + OpenEnum, T: OpenVariant<O>>( &mut self, ) -> Result<&mut OpenEnums, Error>
Registers the type T as variant of the open enum O.
O is the trait object (dyn Trait). Registering a type again
does nothing. Fails if the name of the variant (or one of its
aliases) is the one of another variant (with
ErrorKind::Configuration), unless the open enum is untagged
(where the names are only used in descriptions and errors).