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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::clone::Clone;
use std::fmt::{self, Debug, Formatter};
use std::marker::PhantomData;
use std::mem;
use rand::Rng;
use rand_pcg::Pcg32;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
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: 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)
}
}
#[thread_local]
static mut RNG: Pcg32 = unsafe { mem::transmute(0u128) };
pub fn rng() -> &'static mut Pcg32 {
unsafe { &mut RNG }
}
fn uuidv4() -> Uuid {
let entropy: (u64, u64) = rng().gen();
unsafe { mem::transmute(entropy) }
}
impl<C: CellState, E: EntityState<C>, M: MutEntityState> Clone for Entity<C, E, M>
where
E: Clone,
M: Clone,
{
fn clone(&self) -> Self {
Entity {
state: self.state.clone(),
mut_state: self.mut_state.clone(),
uuid: uuidv4(),
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: mut_state,
uuid: uuidv4(),
phantom: PhantomData,
}
}
}
unsafe impl<C: CellState, E: EntityState<C>, M: MutEntityState> Send for Entity<C, E, M>
where
C: Send,
E: Send,
{
}