pub trait DeserializeContext {
    // Required methods
    fn deserialize_component_ids<'de, A>(
        &mut self,
        seq: A
    ) -> Result<ColumnBatchType, A::Error>
       where A: SeqAccess<'de>;
    fn deserialize_components<'de, A>(
        &mut self,
        entity_count: u32,
        seq: A,
        batch: &mut ColumnBatchBuilder
    ) -> Result<(), A::Error>
       where A: SeqAccess<'de>;
}
Available on crate feature column-serialize only.
Expand description

Implements deserialization of archetypes

Example

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

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

// Could include references to external state for use by serialization methods
struct Context {
    /// Components of the archetype currently being deserialized
    components: Vec<ComponentId>,
}

impl DeserializeContext for Context {
    fn deserialize_component_ids<'de, A>(
        &mut self,
        mut seq: A,
    ) -> Result<ColumnBatchType, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        self.components.clear(); // Discard data from the previous archetype
        let mut batch = ColumnBatchType::new();
        while let Some(id) = seq.next_element()? {
            match id {
                ComponentId::Position => {
                    batch.add::<Position>();
                }
                ComponentId::Velocity => {
                    batch.add::<Velocity>();
                }
            }
            self.components.push(id);
        }
        Ok(batch)
    }

    fn deserialize_components<'de, A>(
        &mut self,
        entity_count: u32,
        mut seq: A,
        batch: &mut ColumnBatchBuilder,
    ) -> Result<(), A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        // Decode component data in the order that the component IDs appeared
        for component in &self.components {
            match *component {
                ComponentId::Position => {
                    deserialize_column::<Position, _>(entity_count, &mut seq, batch)?;
                }
                ComponentId::Velocity => {
                    deserialize_column::<Velocity, _>(entity_count, &mut seq, batch)?;
                }
            }
        }
        Ok(())
    }
}

Required Methods§

source

fn deserialize_component_ids<'de, A>( &mut self, seq: A ) -> Result<ColumnBatchType, A::Error>
where A: SeqAccess<'de>,

Deserialize a set of component IDs

Implementers should usually store the deserialized component IDs in self to guide the following deserialize_components call.

source

fn deserialize_components<'de, A>( &mut self, entity_count: u32, seq: A, batch: &mut ColumnBatchBuilder ) -> Result<(), A::Error>
where A: SeqAccess<'de>,

Deserialize all component data for an archetype

seq is a sequence of tuples directly corresponding to the IDs read in deserialize_component_ids, each containing entity_count elements.

Object Safety§

This trait is not object safe.

Implementors§