Trait hecs::serialize::row::SerializeContext [−][src]
pub trait SerializeContext {
fn serialize_entity<S>(
&mut self,
entity: EntityRef<'_>,
map: &mut S
) -> Result<(), S::Error>
where
S: SerializeMap;
fn component_count(&self, entity: EntityRef<'_>) -> Option<usize> { ... }
}
This is supported 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<'_>,
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,
fn serialize_entity<S>(
&mut self,
entity: EntityRef<'_>,
map: &mut S
) -> Result<(), S::Error> where
S: SerializeMap,
Serialize a single entity into a map
Provided methods
fn component_count(&self, entity: EntityRef<'_>) -> Option<usize>
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.