pub trait SerializeWithRegistry {
// Required method
fn serialize<S>(
&self,
serializer: S,
registry: &TypeRegistry,
) -> Result<S::Ok, S::Error>
where S: Serializer;
}
Expand description
Trait used to provide finer control when serializing a reflected type with one of the reflection serializers.
This trait is the reflection equivalent of serde
’s Serialize
trait.
The main difference is that this trait provides access to the TypeRegistry
,
which means that we can use the registry and all its stored type information
to serialize our type.
This can be useful when writing a custom reflection serializer where we may want to handle parts of the serialization process, but temporarily pass control to the standard reflection serializer for other parts.
For the deserialization equivalent of this trait, see DeserializeWithRegistry
.
§Rationale
Without this trait and its associated type data, such a serializer would have to write out all of the serialization logic itself, possibly including unnecessary code duplication and trivial implementations.
This is because a normal Serialize
implementation has no knowledge of the
TypeRegistry
and therefore cannot create a reflection-based serializer for
nested items.
§Implementors
In order for this to work with the reflection serializers like TypedReflectSerializer
and ReflectSerializer
, implementors should be sure to register the
ReflectSerializeWithRegistry
type data.
This can be done via the registry or by adding #[reflect(SerializeWithRegistry)]
to
the type definition.