qecs-core 0.0.17

Soon to be highly flexible Entity-Component-System framework, core lib.
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) {}