pub trait SerializeContext {
    // Required method
    fn serialize_entity<S>(
        &mut self,
        entity: EntityRef<'_>,
        map: S
    ) -> Result<S::Ok, S::Error>
       where S: SerializeMap;

    // Provided method
    fn component_count(&self, entity: EntityRef<'_>) -> Option<usize> { ... }
}
Available on crate feature row-serialize only.
Expand description

Implements serialization of individual entities

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

Example

use hecs::{*, serialize::row::*};

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

// Could include references to external state for use by `serialize_entity`
struct Context;

impl SerializeContext for Context {
    fn serialize_entity<S>(
        &mut self,
        entity: EntityRef<'_>,
        mut map: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::SerializeMap,
    {
        // Call `try_serialize` for every serializable component we want to save
        try_serialize::<Position, _, _>(&entity, &ComponentId::Position, &mut map)?;
        try_serialize::<Velocity, _, _>(&entity, &ComponentId::Velocity, &mut map)?;
        // Or do something custom for more complex cases.
        map.end()
    }
}

Required Methods§

source

fn serialize_entity<S>( &mut self, entity: EntityRef<'_>, map: S ) -> Result<S::Ok, S::Error>
where S: SerializeMap,

Serialize a single entity into a map

Provided Methods§

source

fn component_count(&self, entity: EntityRef<'_>) -> Option<usize>

Number of entries that serialize_entry will produce for entity, if known

Defaults to None. Must be overridden to return Some to support certain serializers, e.g. bincode.

Object Safety§

This trait is not object safe.

Implementors§