Trait hecs::serialize::SerializeContext[][src]

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

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::*};

// Could include references to external state for use by `serialize_entity`
struct Context;
#[derive(Serialize, Deserialize)]
enum ComponentId { Position, Velocity }

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

Required methods

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

Serialize a single entity into a map

Loading content...

Implementors

Loading content...