waffle/
entity.rs

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Type-safe indices and indexed containers.

use std::default::Default;
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};

use arena_traits::{Arena, IndexAlloc, IndexIter};

/// An index into an index-space of entities.
pub trait EntityRef: Clone + Copy + PartialEq + Eq + PartialOrd + Ord + Hash {
    /// Create a new type-safe index value from a known index.
    fn new(value: usize) -> Self;
    /// Get the index value of this type-safe index. Must be a valid
    /// index (will panic otherwise).
    fn index(self) -> usize;
    /// A sentinel invalid value.
    fn invalid() -> Self;
    /// Is this index a valid index (not equal to `Self::invalid()`)?
    fn is_valid(self) -> bool {
        self != Self::invalid()
    }
    /// Is this index an invalid index (equal to `Self::invalid()`)?
    fn is_invalid(self) -> bool {
        self == Self::invalid()
    }
    /// Turn a valid index into `Some(index)`, and an invalid index
    /// into `None`.
    fn maybe_index(self) -> Option<usize> {
        if self.is_valid() {
            Some(self.index())
        } else {
            None
        }
    }
}

#[macro_export]
macro_rules! declare_entity {
    ($name:tt, $prefix:tt) => {
        #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)]
        pub struct $name(u32);

        impl $crate::entity::EntityRef for $name {
            fn new(value: usize) -> Self {
                use std::convert::TryFrom;
                let value = u32::try_from(value).unwrap();
                debug_assert!(value != u32::MAX);
                Self(value)
            }
            fn index(self) -> usize {
                debug_assert!(self.is_valid());
                self.0 as usize
            }
            fn invalid() -> Self {
                Self(u32::MAX)
            }
        }

        impl std::convert::From<u32> for $name {
            fn from(val: u32) -> Self {
                <Self as $crate::entity::EntityRef>::new(val as usize)
            }
        }

        impl std::default::Default for $name {
            fn default() -> Self {
                <Self as $crate::entity::EntityRef>::invalid()
            }
        }

        impl std::fmt::Debug for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "{}{}", $prefix, self.0)
            }
        }
        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "{}{}", $prefix, self.0)
            }
        }
    };
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct EntityVec<Idx: EntityRef, T: Clone + Debug>(Vec<T>, PhantomData<Idx>);

impl<Idx: EntityRef, T: Clone + Debug> std::default::Default for EntityVec<Idx, T> {
    fn default() -> Self {
        Self(vec![], PhantomData)
    }
}

impl<Idx: EntityRef, T: Clone + Debug> From<Vec<T>> for EntityVec<Idx, T> {
    fn from(vec: Vec<T>) -> Self {
        Self(vec, PhantomData)
    }
}

impl<Idx: EntityRef, T: Clone + Debug> EntityVec<Idx, T> {
    /// Add a new entity, returning its assigned index.
    pub fn push(&mut self, t: T) -> Idx {
        let idx = Idx::new(self.0.len());
        self.0.push(t);
        idx
    }

    /// Get the number of entities in this entity space.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Get an iterator over the index-space.
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = Idx> {
        (0..self.0.len()).map(|index| Idx::new(index))
    }

    /// Get an iterator over (borrows of) entity values.
    pub fn values(&self) -> impl DoubleEndedIterator<Item = &T> {
        self.0.iter()
    }

    /// Get an iterator over (mutable borrows of) entity values.
    pub fn values_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
        self.0.iter_mut()
    }

    /// Get an iterator over index, borrow-of-entity tuples.
    pub fn entries(&self) -> impl DoubleEndedIterator<Item = (Idx, &T)> {
        self.0
            .iter()
            .enumerate()
            .map(|(index, t)| (Idx::new(index), t))
    }

    /// Get an iterator over index, mutable-borrow-of-entity tuples.
    pub fn entries_mut(&mut self) -> impl Iterator<Item = (Idx, &mut T)> {
        self.0
            .iter_mut()
            .enumerate()
            .map(|(index, t)| (Idx::new(index), t))
    }

    /// Typesafe element access, returning `None` if `idx` is the
    /// invalid index.
    pub fn get(&self, idx: Idx) -> Option<&T> {
        self.0.get(idx.maybe_index()?)
    }

    /// Typesafe mutable element access, returning `None` if `idx` is
    /// the invalid index.
    pub fn get_mut(&mut self, idx: Idx) -> Option<&mut T> {
        self.0.get_mut(idx.maybe_index()?)
    }

    /// Convert this `EntityVec` into the underlying `Vec` and return
    /// it.
    pub fn into_vec(self) -> Vec<T> {
        self.0
    }
}

impl<Idx: EntityRef, T: Clone + Debug> Index<Idx> for EntityVec<Idx, T> {
    type Output = T;
    fn index(&self, idx: Idx) -> &T {
        &self.0[idx.index()]
    }
}

impl<Idx: EntityRef, T: Clone + Debug> IndexMut<Idx> for EntityVec<Idx, T> {
    fn index_mut(&mut self, idx: Idx) -> &mut T {
        &mut self.0[idx.index()]
    }
}

impl<Idx: EntityRef, T: Clone + Debug> IndexAlloc<Idx> for EntityVec<Idx, T> {
    fn alloc(&mut self, a: Self::Output) -> Idx {
        self.push(a)
    }


}
impl<Idx: EntityRef, T: Clone + Debug> IndexIter<Idx> for EntityVec<Idx, T> {
    fn iter(&self) -> Box<dyn Iterator<Item = Idx> + '_> {
        Box::new(self.iter())
    }
}

#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
/// Vector of state per entity in an index-space that does *not*
/// define the index-space. In other words, this container will not
/// pass out new indices, it will only allow associating state with
/// existing indices; and it requires a default value for data at an
/// index not yet assigned.

pub struct PerEntity<Idx: EntityRef, T: Clone + Debug + Default>(Vec<T>, PhantomData<Idx>, T);

impl<Idx: EntityRef, T: Clone + Debug + Default> Index<Idx> for PerEntity<Idx, T> {
    type Output = T;
    fn index(&self, idx: Idx) -> &T {
        debug_assert!(idx.is_valid());
        self.0.get(idx.index()).unwrap_or(&self.2)
    }
}

impl<Idx: EntityRef, T: Clone + Debug + Default> IndexMut<Idx> for PerEntity<Idx, T> {
    fn index_mut(&mut self, idx: Idx) -> &mut T {
        debug_assert!(idx.is_valid());
        if idx.index() >= self.0.len() {
            self.0.resize(idx.index() + 1, T::default());
        }
        &mut self.0[idx.index()]
    }
}

impl<Idx: EntityRef, T: Clone + Debug + Default + PartialEq> PartialEq for PerEntity<Idx, T> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}
impl<Idx: EntityRef, T: Clone + Debug + Default + PartialEq + Eq> Eq for PerEntity<Idx, T> {}