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
68
69
70
71
72
73
74
75
use std::cell::Cell as RustCell;
use std::clone::Clone;
use std::fmt::{self, Debug, Formatter};
use std::marker::PhantomData;
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
use uuid::Uuid;
#[allow(unused_imports)]
use test;
use cell::CellState;
#[cfg(feature = "serde")]
pub trait EntityState<C: CellState>:Clone + Serialize + for<'d> Deserialize<'d> {}
#[cfg(not(feature = "serde"))]
pub trait EntityState<C: CellState>:Clone {}
#[cfg(feature = "serde")]
pub trait MutEntityState:Clone + Copy + Default + Serialize + for<'d> Deserialize<'d> {}
#[cfg(not(feature = "serde"))]
pub trait MutEntityState:Clone + Default {}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(bound = "C: for<'d> Deserialize<'d>"))]
pub struct Entity<C: CellState, S: EntityState<C>, M: MutEntityState> {
pub state: S,
pub mut_state: RustCell<M>,
pub uuid: Uuid,
phantom: PhantomData<C>,
}
impl<C: CellState, E: EntityState<C>, M: MutEntityState> Debug for Entity<C, E, M> where E:Debug {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter, "Entity {{state: {:?} }}", self.state)
}
}
impl<C: CellState, E: EntityState<C>, M: MutEntityState> Clone for Entity<C, E, M> where E:Clone, M:Clone {
fn clone(&self) -> Self {
let mut_state_inner = self.mut_state.take();
let mut_state_inner_clone = mut_state_inner.clone();
self.mut_state.set(mut_state_inner_clone);
Entity {
state: self.state.clone(),
mut_state: RustCell::new(mut_state_inner),
uuid: Uuid::new_v4(),
phantom: PhantomData,
}
}
}
impl<C: CellState, E: EntityState<C>, M: MutEntityState> Entity<C, E, M> {
pub fn new(state: E, mut_state: M) -> Entity<C, E, M> {
Entity {
state: state,
mut_state: RustCell::new(mut_state),
uuid: Uuid::new_v4(),
phantom: PhantomData,
}
}
}
unsafe impl<C: CellState, E: EntityState<C>, M: MutEntityState> Send for Entity<C, E, M> where C:Send, E:Send {}