pub trait Marker: Clone + Component + Debug + Eq + Hash + DeserializeOwned + Serialize {
    type Identifier;
    type Allocator: MarkerAllocator<Self>;

    fn id(&self) -> Self::Identifier;

    fn update(&mut self, new_revision: Self) { ... }
Expand description

This trait should be implemented by a component which is going to be used as marker. This marker should be set to entity that should be serialized. If serialization strategy needs to set marker to some entity then it should use newly allocated marker from Marker::Allocator.


extern crate specs;
extern crate serde;
use std::{collections::HashMap, ops::Range};

use specs::{
    saveload::{MarkedBuilder, Marker, MarkerAllocator},

// Marker for entities that should be synced over network
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
struct NetMarker {
    id: u64,
    seq: u64,

impl Component for NetMarker {
    type Storage = DenseVecStorage<Self>;

impl Marker for NetMarker {
    type Allocator = NetNode;
    type Identifier = u64;

    fn id(&self) -> u64 {

    // Updates sequence id.
    // Entities with too old sequence id get deleted.
    fn update(&mut self, update: Self) {
        self.seq = update.seq;

// Each client and server has one
// Contains id range and `NetMarker -> Entity` mapping
struct NetNode {
    range: Range<u64>,
    mapping: HashMap<u64, Entity>,

impl MarkerAllocator<NetMarker> for NetNode {
    fn allocate(&mut self, entity: Entity, id: Option<u64>) -> NetMarker {
        let id = id.unwrap_or_else(|| {
                .expect("Id range must be virtually endless")
        let marker = NetMarker { id, seq: 0 };
        self.mapping.insert(id, entity);

    fn retrieve_entity_internal(&self, id: u64) -> Option<Entity> {

    fn maintain(&mut self, entities: &EntitiesRes, storage: &ReadStorage<NetMarker>) {
        self.mapping = (entities, storage)
            .map(|(e, m)| (, e))

fn main() {
    let mut world = World::new();

    world.insert(NetNode {
        range: 0..100,
        mapping: HashMap::new(),

    let entity = world.create_entity().marked::<NetMarker>().build();
    let storage = &mut world.write_storage::<NetMarker>();
    let marker = storage.get(entity).unwrap().clone();
            .retrieve_entity(marker, storage, &world.entities()),

Required Associated Types

Id of the marker

Allocator for this Marker

Required Methods

Get this marker internal id. The value of this method should be constant.

Provided Methods

This gets called when an entity is deserialized by DeserializeComponents. It can be used to update internal data that is not used for identification.


This function may assume that == However, it must not exhibit undefined behavior in such a case.


May panic if !=

Default implementation

The default implementation just sets self to new_revision.

#[derive(Clone, Debug, Deserialize, Eq, Hash, Serialize)]
struct MyMarker {
    id: u64,
    last_modified: String,

impl Marker for MyMarker {
    type Identifier = u64;

    fn id(&self) -> u64 {

    fn update(&self, new: Self) {
        self.last_modified = new.last_modified;

Now, the marker always contains the name of the client who updated the entity associated with this marker.