@@ -3,6 +3,7 @@ use crate::{
3
3
entity:: Entity ,
4
4
storage:: BlobVec ,
5
5
} ;
6
+ use bevy_utils:: NonMaxUsize ;
6
7
use std:: { cell:: UnsafeCell , marker:: PhantomData } ;
7
8
8
9
#[ derive( Debug ) ]
@@ -89,7 +90,7 @@ pub struct ComponentSparseSet {
89
90
dense : BlobVec ,
90
91
ticks : UnsafeCell < Vec < ComponentTicks > > ,
91
92
entities : Vec < Entity > ,
92
- sparse : SparseArray < Entity , usize > ,
93
+ sparse : SparseArray < Entity , NonMaxUsize > ,
93
94
}
94
95
95
96
impl ComponentSparseSet {
@@ -123,11 +124,14 @@ impl ComponentSparseSet {
123
124
let dense = & mut self . dense ;
124
125
let entities = & mut self . entities ;
125
126
let ticks_list = self . ticks . get_mut ( ) ;
126
- let dense_index = * self . sparse . get_or_insert_with ( entity, move || {
127
- ticks_list. push ( ComponentTicks :: new ( change_tick) ) ;
128
- entities. push ( entity) ;
129
- dense. push_uninit ( )
130
- } ) ;
127
+ let dense_index = self
128
+ . sparse
129
+ . get_or_insert_with ( entity, move || {
130
+ ticks_list. push ( ComponentTicks :: new ( change_tick) ) ;
131
+ entities. push ( entity) ;
132
+ NonMaxUsize :: new ( dense. push_uninit ( ) ) . unwrap ( )
133
+ } )
134
+ . get ( ) ;
131
135
// SAFE: dense_index exists thanks to the call above
132
136
self . dense . set_unchecked ( dense_index, value) ;
133
137
( ( * self . ticks . get ( ) ) . get_unchecked_mut ( dense_index) ) . set_changed ( change_tick) ;
@@ -144,7 +148,7 @@ impl ComponentSparseSet {
144
148
pub fn get ( & self , entity : Entity ) -> Option < * mut u8 > {
145
149
self . sparse . get ( entity) . map ( |dense_index| {
146
150
// SAFE: if the sparse index points to something in the dense vec, it exists
147
- unsafe { self . dense . get_unchecked ( * dense_index) }
151
+ unsafe { self . dense . get_unchecked ( dense_index. get ( ) ) }
148
152
} )
149
153
}
150
154
@@ -154,7 +158,7 @@ impl ComponentSparseSet {
154
158
pub unsafe fn get_with_ticks ( & self , entity : Entity ) -> Option < ( * mut u8 , * mut ComponentTicks ) > {
155
159
let ticks = & mut * self . ticks . get ( ) ;
156
160
self . sparse . get ( entity) . map ( move |dense_index| {
157
- let dense_index = * dense_index;
161
+ let dense_index = dense_index. get ( ) ;
158
162
// SAFE: if the sparse index points to something in the dense vec, it exists
159
163
(
160
164
self . dense . get_unchecked ( dense_index) ,
@@ -169,9 +173,8 @@ impl ComponentSparseSet {
169
173
pub unsafe fn get_ticks ( & self , entity : Entity ) -> Option < & mut ComponentTicks > {
170
174
let ticks = & mut * self . ticks . get ( ) ;
171
175
self . sparse . get ( entity) . map ( move |dense_index| {
172
- let dense_index = * dense_index;
173
176
// SAFE: if the sparse index points to something in the dense vec, it exists
174
- ticks. get_unchecked_mut ( dense_index)
177
+ ticks. get_unchecked_mut ( dense_index. get ( ) )
175
178
} )
176
179
}
177
180
@@ -180,16 +183,20 @@ impl ComponentSparseSet {
180
183
/// returned).
181
184
pub fn remove_and_forget ( & mut self , entity : Entity ) -> Option < * mut u8 > {
182
185
self . sparse . remove ( entity) . map ( |dense_index| {
186
+ let dense_index_usize = dense_index. get ( ) ;
183
187
// SAFE: unique access to ticks
184
188
unsafe {
185
- ( * self . ticks . get ( ) ) . swap_remove ( dense_index ) ;
189
+ ( * self . ticks . get ( ) ) . swap_remove ( dense_index_usize ) ;
186
190
}
187
- self . entities . swap_remove ( dense_index ) ;
188
- let is_last = dense_index == self . dense . len ( ) - 1 ;
191
+ self . entities . swap_remove ( dense_index_usize ) ;
192
+ let is_last = dense_index_usize == self . dense . len ( ) - 1 ;
189
193
// SAFE: dense_index was just removed from `sparse`, which ensures that it is valid
190
- let value = unsafe { self . dense . swap_remove_and_forget_unchecked ( dense_index) } ;
194
+ let value = unsafe {
195
+ self . dense
196
+ . swap_remove_and_forget_unchecked ( dense_index_usize)
197
+ } ;
191
198
if !is_last {
192
- let swapped_entity = self . entities [ dense_index ] ;
199
+ let swapped_entity = self . entities [ dense_index_usize ] ;
193
200
* self . sparse . get_mut ( swapped_entity) . unwrap ( ) = dense_index;
194
201
}
195
202
value
@@ -198,13 +205,14 @@ impl ComponentSparseSet {
198
205
199
206
pub fn remove ( & mut self , entity : Entity ) -> bool {
200
207
if let Some ( dense_index) = self . sparse . remove ( entity) {
201
- self . ticks . get_mut ( ) . swap_remove ( dense_index) ;
202
- self . entities . swap_remove ( dense_index) ;
203
- let is_last = dense_index == self . dense . len ( ) - 1 ;
208
+ let dense_index_usize = dense_index. get ( ) ;
209
+ self . ticks . get_mut ( ) . swap_remove ( dense_index_usize) ;
210
+ self . entities . swap_remove ( dense_index_usize) ;
211
+ let is_last = dense_index_usize == self . dense . len ( ) - 1 ;
204
212
// SAFE: if the sparse index points to something in the dense vec, it exists
205
- unsafe { self . dense . swap_remove_and_drop_unchecked ( dense_index ) }
213
+ unsafe { self . dense . swap_remove_and_drop_unchecked ( dense_index_usize ) }
206
214
if !is_last {
207
- let swapped_entity = self . entities [ dense_index ] ;
215
+ let swapped_entity = self . entities [ dense_index_usize ] ;
208
216
* self . sparse . get_mut ( swapped_entity) . unwrap ( ) = dense_index;
209
217
}
210
218
true
@@ -225,7 +233,7 @@ impl ComponentSparseSet {
225
233
pub struct SparseSet < I , V : ' static > {
226
234
dense : Vec < V > ,
227
235
indices : Vec < I > ,
228
- sparse : SparseArray < I , usize > ,
236
+ sparse : SparseArray < I , NonMaxUsize > ,
229
237
}
230
238
231
239
impl < I : SparseSetIndex , V > Default for SparseSet < I , V > {
@@ -261,10 +269,11 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
261
269
if let Some ( dense_index) = self . sparse . get ( index. clone ( ) ) . cloned ( ) {
262
270
// SAFE: dense indices stored in self.sparse always exist
263
271
unsafe {
264
- * self . dense . get_unchecked_mut ( dense_index) = value;
272
+ * self . dense . get_unchecked_mut ( dense_index. get ( ) ) = value;
265
273
}
266
274
} else {
267
- self . sparse . insert ( index. clone ( ) , self . dense . len ( ) ) ;
275
+ self . sparse
276
+ . insert ( index. clone ( ) , NonMaxUsize :: new ( self . dense . len ( ) ) . unwrap ( ) ) ;
268
277
self . indices . push ( index) ;
269
278
self . dense . push ( value) ;
270
279
}
@@ -295,11 +304,12 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
295
304
pub fn get_or_insert_with ( & mut self , index : I , func : impl FnOnce ( ) -> V ) -> & mut V {
296
305
if let Some ( dense_index) = self . sparse . get ( index. clone ( ) ) . cloned ( ) {
297
306
// SAFE: dense indices stored in self.sparse always exist
298
- unsafe { self . dense . get_unchecked_mut ( dense_index) }
307
+ unsafe { self . dense . get_unchecked_mut ( dense_index. get ( ) ) }
299
308
} else {
300
309
let value = func ( ) ;
301
310
let dense_index = self . dense . len ( ) ;
302
- self . sparse . insert ( index. clone ( ) , dense_index) ;
311
+ self . sparse
312
+ . insert ( index. clone ( ) , NonMaxUsize :: new ( dense_index) . unwrap ( ) ) ;
303
313
self . indices . push ( index) ;
304
314
self . dense . push ( value) ;
305
315
// SAFE: dense index was just populated above
@@ -325,25 +335,26 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
325
335
pub fn get ( & self , index : I ) -> Option < & V > {
326
336
self . sparse . get ( index) . map ( |dense_index| {
327
337
// SAFE: if the sparse index points to something in the dense vec, it exists
328
- unsafe { self . dense . get_unchecked ( * dense_index) }
338
+ unsafe { self . dense . get_unchecked ( dense_index. get ( ) ) }
329
339
} )
330
340
}
331
341
332
342
pub fn get_mut ( & mut self , index : I ) -> Option < & mut V > {
333
343
let dense = & mut self . dense ;
334
344
self . sparse . get ( index) . map ( move |dense_index| {
335
345
// SAFE: if the sparse index points to something in the dense vec, it exists
336
- unsafe { dense. get_unchecked_mut ( * dense_index) }
346
+ unsafe { dense. get_unchecked_mut ( dense_index. get ( ) ) }
337
347
} )
338
348
}
339
349
340
350
pub fn remove ( & mut self , index : I ) -> Option < V > {
341
351
self . sparse . remove ( index) . map ( |dense_index| {
342
- let is_last = dense_index == self . dense . len ( ) - 1 ;
343
- let value = self . dense . swap_remove ( dense_index) ;
344
- self . indices . swap_remove ( dense_index) ;
352
+ let dense_index_usize = dense_index. get ( ) ;
353
+ let is_last = dense_index_usize == self . dense . len ( ) - 1 ;
354
+ let value = self . dense . swap_remove ( dense_index_usize) ;
355
+ self . indices . swap_remove ( dense_index_usize) ;
345
356
if !is_last {
346
- let swapped_index = self . indices [ dense_index ] . clone ( ) ;
357
+ let swapped_index = self . indices [ dense_index_usize ] . clone ( ) ;
347
358
* self . sparse . get_mut ( swapped_index) . unwrap ( ) = dense_index;
348
359
}
349
360
value
0 commit comments