[][src]Trait specs::saveload::ConvertSaveload

pub trait ConvertSaveload<M>: Sized {
    type Data: Serialize + DeserializeOwned;
    type Error;
    fn convert_from<F>(data: Self::Data, ids: F) -> Result<Self, Self::Error>
    where
        F: FnMut(M) -> Option<Entity>
;
fn convert_into<F>(&self, ids: F) -> Result<Self::Data, Self::Error>
    where
        F: FnMut(Entity) -> Option<M>
; }

Converts a data type (usually a Component) into its serializable form and back to actual data from it's deserialized form.

This is automatically implemented for any type that is Clone, Serialize and DeserializeOwned.

Implementing this yourself is usually only needed if you have a component that points to another Entity, or has a field which does, and you wish to Serialize it.

Note: if you're using specs_derive you can use #[derive(Saveload)] to automatically derive this.

You must add the derive to any type that your component holds which does not auto-implement this traits, including the component itself (similar to how normal Serialize and Deserialize work).

Example

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

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`/`Deserialize` with each
// instance of `Entity` replaced with a generic "M".
#[derive(Serialize, Deserialize)]
struct TargetData<M>(M);

impl<M: Marker + Serialize> ConvertSaveload<M> for Target
where
    for<'de> M: Deserialize<'de>,
{
    type Data = TargetData<M>;
    type Error = NoError;

    fn convert_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))
    }

    fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
    where
        F: FnMut(M) -> Option<Entity>,
    {
        let entity = ids(data.0).unwrap();
        Ok(Target(entity))
    }
}

Associated Types

type Data: Serialize + DeserializeOwned

(De)Serializable data representation for data type

type Error

Error may occur during serialization or deserialization of component

Loading content...

Required methods

fn convert_from<F>(data: Self::Data, ids: F) -> Result<Self, Self::Error> where
    F: FnMut(M) -> Option<Entity>, 

Convert this data from a deserializable form (Data) using entity to marker mapping function

fn convert_into<F>(&self, ids: F) -> Result<Self::Data, Self::Error> where
    F: FnMut(Entity) -> Option<M>, 

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

Loading content...

Implementors

impl<C, M> ConvertSaveload<M> for C where
    C: Clone + Serialize + DeserializeOwned
[src]

type Data = Self

type Error = NoError

impl<M> ConvertSaveload<M> for Entity where
    M: Serialize + DeserializeOwned
[src]

type Data = M

type Error = NoError

Loading content...