1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::any::Any;
use std::borrow::Borrow;
use std::fmt::{self, Debug};
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::Deref;

//use rustc_serialize::{Encodable, Decodable};

// ++++++++++++++++++++ Component ++++++++++++++++++++

pub trait Component: /*Encodable + Decodable +*/ Debug + Clone + Any + Send + Sync {}

// ++++++++++++++++++++ Id ++++++++++++++++++++

/// TODO: should this be ToString/FromStr?
pub trait Id: Component + Hash + Copy + Ord {}

/// For ids which closely mirror the total entity count. 
/// Ids may either have an indexable-part or consist entirely of it.
///
/// TODO: naming. `Index`? `Indexable...`?
pub trait IdWithIndex: Id {
    fn max_index() -> usize;
    fn index(&self) -> usize;
}

/// Indicates that an id is valid and can be used to safely access an entity.
#[derive(Hash, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
pub struct Valid<'id, ID: Id>{
    id: ID,
    _phantom: PhantomData<&'id ()>,
}

impl<'id, ID: Id> Valid<'id, ID> {
    pub unsafe fn new(id: ID) -> Self {
        Valid{ id: id, _phantom: PhantomData }
    }
}

impl<'id, ID: Id> Debug for Valid<'id, ID> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.id.fmt(fmt)
    }
}

impl<'id, ID: Id> Borrow<ID> for Valid<'id, ID> {
    fn borrow(&self) -> &ID { &self.id }
}

impl<'id, ID: Id> Deref for Valid<'id, ID> {
    type Target = ID;
    fn deref(&self) -> &ID { &self.id }
}

// ++++++++++++++++++++ Event ++++++++++++++++++++

pub trait Event: /*Encodable + Decodable +*/ Debug + Clone + Any + Send + Sync {}

// ++++++++++++++++++++ Action ++++++++++++++++++++

pub trait Action: /*Encodable + Decodable +*/ Debug + Clone + Any + Send + Sync {}

/// All `($id, $action)`-pairs implement `Event`; such an event means as much as:
/// "entity `$id` performs `$action`". 
impl<ID: Id, A: Action> Event for (ID, A) {}