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
use crate::*;
use extend_lifetime::extend_lifetime;
use std::cell::{Ref, RefCell, RefMut};
use std::rc::Rc;
const CAPACITY: usize = 4096;
pub struct BorrowedPerEntity<T> {
values: Vec<T>,
version: Version,
}
impl<T> BorrowedPerEntity<T> {
pub fn new() -> Self {
Self {
values: Vec::with_capacity(CAPACITY),
version: Version(0),
}
}
}
impl<T: 'static> BorrowedStorage for BorrowedPerEntity<T> {
type Item = &'static T;
type Batch = &'static [T];
#[inline(always)]
fn version(&self) -> Version {
self.version
}
#[inline(always)]
fn read(&self, index: usize) -> Option<&'static T> {
let borrow = Some(if cfg!(debug_assertions) {
&self.values[index]
} else {
unsafe { self.values.get_unchecked(index) }
});
unsafe { extend_lifetime(borrow) }
}
#[inline(always)]
fn read_batch(&self) -> Self::Batch {
unsafe { extend_lifetime(&self.values[..]) }
}
}
impl<T: 'static> BorrowedStorageMut for BorrowedPerEntity<T> {
type ItemMut = T;
type BatchMut = &'static mut [T];
#[inline(always)]
fn write(&mut self, index: usize, item: Self::ItemMut) {
self.values[index] = item;
}
#[inline(always)]
fn write_batch(&mut self) -> Self::BatchMut {
let borrow = &mut self.values[..];
unsafe { extend_lifetime(borrow) }
}
}
pub struct PerEntity<T> {
cell: RefCell<BorrowedPerEntity<T>>,
}
impl<T> PerEntity<T> {
pub fn new() -> Self {
let cell = RefCell::new(BorrowedPerEntity::new());
Self { cell }
}
}
impl<T: 'static> AnyStorage for PerEntity<T> {}
impl<T: 'static> ReadableStorage for PerEntity<T> {
type Read = Rc<Self>;
fn get(_world_storage: &Components, archetype_storage: &Components) -> Option<Self::Read> {
archetype_storage.get_storage::<Self>()
}
}
impl<T: 'static> RefLike for PerEntity<T> {
type Borrowed = Ref<'static, BorrowedPerEntity<T>>;
fn borrow(&self) -> Self::Borrowed {
unsafe { extend_lifetime(self.cell.borrow()) }
}
}
impl<T: 'static> WritableStorage for PerEntity<T> {
type ReadMut = Rc<Self>;
fn get_mut(archetype: &Archetype) -> Option<Self::ReadMut> {
archetype.get_storage_mut()
}
}
impl<T: 'static> RefLikeMut for PerEntity<T> {
type BorrowedMut = RefMut<'static, BorrowedPerEntity<T>>;
fn borrow_mut(&self) -> Self::BorrowedMut {
unsafe { extend_lifetime(self.cell.borrow_mut()) }
}
}
impl<T: EntityWriter + Component<Storage = PerEntity<T>>> ArchetypeInitializerFromComponentStorage
for PerEntity<T>
{
type Component = T;
fn initialize(component: T, archetype: &mut Archetype) {
let storage = Self::new();
archetype.add_component::<T>(storage);
component.write(archetype, 0);
}
}
impl<T: Component<Storage = PerEntity<T>>> ArchetypeFilterFromComponentStorage for PerEntity<T> {
type Component = T;
fn includes(_component: &T, archetype: &Archetype) -> bool {
archetype.get_storage::<Self>().is_some()
}
}
impl<T: Component<Storage = PerEntity<T>>> EntityWriterFromComponentStorage for PerEntity<T> {
type Component = T;
#[inline(always)]
fn write(component: T, archetype: &mut Archetype, index: usize) {
let storage = archetype.get_storage_mut::<Self>().unwrap();
let values = &mut storage.cell.borrow_mut().values;
if index == values.len() {
values.push(component)
} else {
values[index] = component;
}
}
}