pub enum Chunk<'a> {
Atom(Atom<'a>),
Struct(Boxed<dyn StructEmitter + 'a>),
Map(Boxed<dyn MapEmitter + 'a>),
Seq(Boxed<dyn SeqEmitter + 'a>),
Forward(SerializeHandle<'a>),
}Expand description
A chunk represents the minimum state necessary to serialize a value.
Chunks are of two types: atomic primitives and stateful emitters.
For instance Chunk::Bool(true) is an atomic primitive. It can be emitted
to a serializer directly. On the other hand a Chunk::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 serialization
with Chunk::seq, Chunk::map and Chunk::structure (see
Boxed).
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 chunk. This is useful to serialize a value by converting it into another value first as the handle can own that value:
use deser::ser::{Chunk, Serialize, SerializeHandle};
use deser::{Error, State};
struct Point(u32, u32);
impl Serialize for Point {
fn serialize(&self, state: &mut State) -> Result<Chunk<'_>, Error> {
// serialize as a vector
Ok(Chunk::Forward(SerializeHandle::arena(
vec![self.0, self.1],
state,
)))
}
}The container_shape of the
value that returned the chunk 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 chunk.
Forwarding chunks cannot be flattened into structs.
Implementations§
Source§impl<'a> Chunk<'a>
impl<'a> Chunk<'a>
Sourcepub fn seq<E: SeqEmitter + 'a>(emitter: E, state: &mut State) -> Chunk<'a>
pub fn seq<E: SeqEmitter + 'a>(emitter: E, state: &mut State) -> Chunk<'a>
Creates a chunk of a sequence emitter in the arena of the serialization.
Sourcepub fn map<E: MapEmitter + 'a>(emitter: E, state: &mut State) -> Chunk<'a>
pub fn map<E: MapEmitter + 'a>(emitter: E, state: &mut State) -> Chunk<'a>
Creates a chunk of a map emitter in the arena of the serialization.
Sourcepub fn structure<E: StructEmitter + 'a>(
emitter: E,
state: &mut State,
) -> Chunk<'a>
pub fn structure<E: StructEmitter + 'a>( emitter: E, state: &mut State, ) -> Chunk<'a>
Creates a chunk of a struct emitter in the arena of the serialization.