pub enum Emit<'a> {
Atom(Atom<'a>),
Struct(Boxed<dyn StructEmitter + 'a>),
Map(Boxed<dyn MapEmitter + 'a>),
Seq(Boxed<dyn SeqEmitter + 'a>),
Forward(SerializeHandle<'a>),
}Expand description
Describes how a value is emitted, returned by
Serialize::serialize.
A value is either emitted as an atom, by a stateful emitter or by
forwarding to another value. For instance Emit::Atom(Atom::Bool(true))
is emitted to a serializer directly. On the other hand an Emit::Map
contains a stateful emitter that keeps yielding values until it’s done
walking over the map.
The emitters are typically allocated in the arena of the state
with Emit::seq, Emit::map and Emit::structure (see
Boxed). They are the serialization equivalent of the
Sinks of deserialization: they hold the state of a
value that is being serialized.
Variants§
Atom(Atom<'a>)
Struct(Boxed<dyn StructEmitter + 'a>)
Map(Boxed<dyn MapEmitter + 'a>)
Seq(Boxed<dyn SeqEmitter + 'a>)
Forward(SerializeHandle<'a>)
Serializes another value in place of this one.
The driver serializes the value in the handle as if it was produced
instead of the value that returned the Emit. This is useful to
serialize a value by converting it into another value first as the
handle can own that value:
use deser::ser::{Emit, Serialize, SerializeHandle};
use deser::{Error, State};
struct Point(u32, u32);
impl Serialize for Point {
fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
// serialize as a vector
Ok(Emit::Forward(SerializeHandle::arena(
vec![value.0, value.1],
state,
)))
}
}The container_shape of the
value that returned the Emit is not used, the forwarded value provides
its own.
finish is invoked on the forwarded
value first and then on the value that returned the Emit.
Values that forward cannot be flattened into structs.
Implementations§
Source§impl<'a> Emit<'a>
impl<'a> Emit<'a>
Sourcepub fn seq<E: SeqEmitter + 'a>(emitter: E, state: &mut State) -> Emit<'a>
pub fn seq<E: SeqEmitter + 'a>(emitter: E, state: &mut State) -> Emit<'a>
Emits a sequence with an emitter in the arena of the serialization.
Sourcepub fn map<E: MapEmitter + 'a>(emitter: E, state: &mut State) -> Emit<'a>
pub fn map<E: MapEmitter + 'a>(emitter: E, state: &mut State) -> Emit<'a>
Emits a map with an emitter in the arena of the state.
Sourcepub fn structure<E: StructEmitter + 'a>(
emitter: E,
state: &mut State,
) -> Emit<'a>
pub fn structure<E: StructEmitter + 'a>( emitter: E, state: &mut State, ) -> Emit<'a>
Emits a struct with an emitter in the arena of the serialization.