Trait specs::saveload::IntoSerialize[][src]

pub trait IntoSerialize<M>: Component {
    type Data: Serialize;
    type Error;
    fn into<F>(&self, ids: F) -> Result<Self::Data, Self::Error>
    where
        F: FnMut(Entity) -> Option<M>
; }

Converts a component into its serialized form.

This is automatically implemented for any type that is both a Component and Serialize, yielding itself.

Implementing this yourself is usually only needed if you have a component that points to another Entity and you wish to Serialize it.

In most cases, you also likely want to implement the companion trait FromDeserialize.

Example

use serde::Serialize;
use specs::prelude::*;
use specs::error::NoError;
use specs::saveload::{Marker, IntoSerialize};

struct Target(Entity);

impl Component for Target {
    type Storage = VecStorage<Self>;
}

// We need a matching "data" struct to hold our
// marker. In general, you just need a single struct
// per component you want to make `Serialize` with each
// instance of `Entity` replaced with a generic "M".
#[derive(Serialize)]
struct TargetData<M>(M);

impl<M: Marker + Serialize> IntoSerialize<M> for Target {
    type Data = TargetData<M>;
    type Error = NoError;

    fn into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
    where
        F: FnMut(Entity) -> Option<M>
    {
        let marker = ids(self.0).unwrap();
        Ok(TargetData(marker))
    }
}

Associated Types

Serializable data representation for component

Error may occur during serialization or deserialization of component

Required Methods

Convert this component into serializable form (Data) using entity to marker mapping function

Implementors