pub trait SerializeContext {
    fn component_count(&self, archetype: &Archetype) -> usize;
    fn serialize_component_ids<S: SerializeTuple>(
        &mut self,
        archetype: &Archetype,
        out: S
    ) -> Result<S::Ok, S::Error>; fn serialize_components<S: SerializeTuple>(
        &mut self,
        archetype: &Archetype,
        out: S
    ) -> Result<S::Ok, S::Error>; }
Available on crate feature column-serialize only.
Expand description

Implements serialization of archetypes

serialize_component_ids and serialize_components must serialize exactly the number of elements indicated by component_count or return Err.

Data external to the World can be exposed during serialization by storing references inside the struct implementing this trait.

Example

use std::any::TypeId;
use hecs::{*, serialize::column::*};

#[derive(Serialize, Deserialize)]
enum ComponentId { Position, Velocity }

struct Context;

impl SerializeContext for Context {
    fn component_count(&self, archetype: &Archetype) -> usize {
        archetype.component_types()
            .filter(|&t| t == TypeId::of::<Position>() || t == TypeId::of::<Velocity>())
            .count()
    }

    fn serialize_component_ids<S: serde::ser::SerializeTuple>(
        &mut self,
        archetype: &Archetype,
        mut out: S,
    ) -> Result<S::Ok, S::Error> {
        try_serialize_id::<Position, _, _>(archetype, &ComponentId::Position, &mut out)?;
        try_serialize_id::<Velocity, _, _>(archetype, &ComponentId::Velocity, &mut out)?;
        out.end()
    }

    fn serialize_components<S: serde::ser::SerializeTuple>(
        &mut self,
        archetype: &Archetype,
        mut out: S,
    ) -> Result<S::Ok, S::Error> {
        try_serialize::<Position, _>(archetype, &mut out)?;
        try_serialize::<Velocity, _>(archetype, &mut out)?;
        out.end()
    }
}

Required Methods§

Number of entries that serialize_component_ids and serialize_components will produce for archetype

Serialize the IDs of the components from archetype that will be serialized

Serialize component data from archetype into out

For each component ID written by serialize_component_ids, this method must write a tuple containing one value for each entity, e.g. using try_serialize, in the same order. Each tuple’s length must exactly match the number of entities in archetype, and there must be exactly the same number of tuples as there were IDs.

Implementors§