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
use super::Body;
use generational_arena::Arena;
use std::ops::{Index, IndexMut};
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct BodyHandle(generational_arena::Index);
pub struct BodySet {
bodies: Arena<Body>,
}
impl Default for BodySet {
fn default() -> Self {
Self::new()
}
}
impl BodySet {
pub fn new() -> Self {
Self {
bodies: Arena::with_capacity(16),
}
}
pub fn insert(&mut self, body: Body) -> BodyHandle {
let key = self.bodies.insert(body);
BodyHandle(key)
}
pub fn get(&self, handle: BodyHandle) -> Option<&Body> {
self.bodies.get(handle.0)
}
pub fn get_mut(&mut self, handle: BodyHandle) -> Option<&mut Body> {
self.bodies.get_mut(handle.0)
}
pub fn iter(&self) -> impl Iterator<Item = (crate::BodyHandle, &Body)> {
self.bodies
.iter()
.map(|(index, body)| (BodyHandle(index), body))
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = (crate::BodyHandle, &mut Body)> {
self.bodies
.iter_mut()
.map(|(index, body)| (BodyHandle(index), body))
}
pub(crate) fn internal_remove(&mut self, handle: BodyHandle) -> Body {
self.bodies
.remove(handle.0)
.expect("Tried to remove nonexistent body")
}
}
impl Index<BodyHandle> for BodySet {
type Output = Body;
fn index(&self, index: BodyHandle) -> &Body {
&self.bodies[index.0]
}
}
impl IndexMut<BodyHandle> for BodySet {
fn index_mut(&mut self, index: BodyHandle) -> &mut Body {
&mut self.bodies[index.0]
}
}