Trait despero::ecs::SerializeContext
source · pub trait SerializeContext {
// Required methods
fn component_count(&self, archetype: &Archetype) -> usize;
fn serialize_component_ids<S>(
&mut self,
archetype: &Archetype,
out: S
) -> Result<<S as SerializeTuple>::Ok, <S as SerializeTuple>::Error>
where S: SerializeTuple;
fn serialize_components<S>(
&mut self,
archetype: &Archetype,
out: S
) -> Result<<S as SerializeTuple>::Ok, <S as SerializeTuple>::Error>
where S: SerializeTuple;
}
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§
sourcefn component_count(&self, archetype: &Archetype) -> usize
fn component_count(&self, archetype: &Archetype) -> usize
Number of entries that serialize_component_ids
and
serialize_components
will produce for archetype
sourcefn serialize_component_ids<S>(
&mut self,
archetype: &Archetype,
out: S
) -> Result<<S as SerializeTuple>::Ok, <S as SerializeTuple>::Error>where
S: SerializeTuple,
fn serialize_component_ids<S>( &mut self, archetype: &Archetype, out: S ) -> Result<<S as SerializeTuple>::Ok, <S as SerializeTuple>::Error>where S: SerializeTuple,
Serialize the IDs of the components from archetype
that will be serialized
sourcefn serialize_components<S>(
&mut self,
archetype: &Archetype,
out: S
) -> Result<<S as SerializeTuple>::Ok, <S as SerializeTuple>::Error>where
S: SerializeTuple,
fn serialize_components<S>( &mut self, archetype: &Archetype, out: S ) -> Result<<S as SerializeTuple>::Ok, <S as SerializeTuple>::Error>where S: SerializeTuple,
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.