1
1
//! Different types of storages you can use for your components.
2
2
3
3
use std:: collections:: BTreeMap ;
4
+ use std:: mem:: MaybeUninit ;
4
5
5
6
use derivative:: Derivative ;
6
7
use hashbrown:: HashMap ;
@@ -11,14 +12,16 @@ use crate::{
11
12
world:: Index ,
12
13
} ;
13
14
14
- pub trait SafeSliceAccess < T > {
15
- fn as_slice ( & self ) -> & [ T ] ;
16
- fn as_mut_slice ( & mut self ) -> & mut [ T ] ;
17
- }
18
-
19
- pub trait UnsafeSliceAccess < T > {
20
- unsafe fn unsafe_slice ( & self ) -> & [ T ] ;
21
- unsafe fn unsafe_mut_slice ( & mut self ) -> & mut [ T ] ;
15
+ /// Some storages can provide slices to access the underlying data.
16
+ ///
17
+ /// The underlying data may be of type `T`, or it may be of a type
18
+ /// which wraps `T`. The associated type `Element` identifies what
19
+ /// the slices will contain.
20
+ pub trait SliceAccess < T > {
21
+ type Element ;
22
+
23
+ fn as_slice ( & self ) -> & [ Self :: Element ] ;
24
+ fn as_mut_slice ( & mut self ) -> & mut [ Self :: Element ] ;
22
25
}
23
26
24
27
/// BTreeMap-based storage.
@@ -107,13 +110,15 @@ pub struct DenseVecStorage<T> {
107
110
data_id : Vec < Index > ,
108
111
}
109
112
110
- impl < T > SafeSliceAccess < T > for DenseVecStorage < T > {
113
+ impl < T > SliceAccess < T > for DenseVecStorage < T > {
114
+ type Element = T ;
115
+
111
116
/// Returns a slice of all the components in this storage.
112
117
///
113
118
/// Indices inside the slice do not correspond to anything in particular, and
114
119
/// especially do not correspond with entity IDs.
115
120
#[ inline]
116
- fn as_slice ( & self ) -> & [ T ] {
121
+ fn as_slice ( & self ) -> & [ Self :: Element ] {
117
122
self . data . as_slice ( )
118
123
}
119
124
@@ -122,7 +127,7 @@ impl<T> SafeSliceAccess<T> for DenseVecStorage<T> {
122
127
/// Indices inside the slice do not correspond to anything in particular, and
123
128
/// especially do not correspond with entity IDs.
124
129
#[ inline]
125
- fn as_mut_slice ( & mut self ) -> & mut [ T ] {
130
+ fn as_mut_slice ( & mut self ) -> & mut [ Self :: Element ] {
126
131
self . data . as_mut_slice ( )
127
132
}
128
133
}
@@ -216,34 +221,24 @@ unsafe impl<T> DistinctStorage for NullStorage<T> {}
216
221
/// Vector storage. Uses a simple `Vec`. Supposed to have maximum
217
222
/// performance for the components mostly present in entities.
218
223
///
219
- /// `unsafe_slice ()` and `unsafe_mut_slice ()` indices correspond to
224
+ /// `as_slice ()` and `as_mut_slice ()` indices correspond to
220
225
/// entity IDs. These can be compared to other `VecStorage`s, to
221
226
/// other `DefaultVecStorage`s, and to `Entity::id()`s for live
222
227
/// entities.
223
228
#[ derive( Derivative ) ]
224
229
#[ derivative( Default ( bound = "" ) ) ]
225
- pub struct VecStorage < T > ( Vec < T > ) ;
230
+ pub struct VecStorage < T > ( Vec < MaybeUninit < T > > ) ;
231
+
232
+ impl < T > SliceAccess < T > for VecStorage < T > {
233
+ type Element = MaybeUninit < T > ;
226
234
227
- impl < T > UnsafeSliceAccess < T > for VecStorage < T > {
228
- /// Returns a slice of all the components in this storage.
229
- ///
230
- /// # Safety
231
- ///
232
- /// This slice contains uninitialized and dropped values. If this is a
233
- /// problem, consider using `DefaultVecStorage` or `DenseVecStorage` instead.
234
235
#[ inline]
235
- unsafe fn unsafe_slice ( & self ) -> & [ T ] {
236
+ fn as_slice ( & self ) -> & [ Self :: Element ] {
236
237
self . 0 . as_slice ( )
237
238
}
238
239
239
- /// Returns a mutable slice of all the components in this storage.
240
- ///
241
- /// # Safety
242
- ///
243
- /// This slice contains uninitialized and dropped values. If this is a
244
- /// problem, consider using `DefaultVecStorage` or `DenseVecStorage` instead.
245
240
#[ inline]
246
- unsafe fn unsafe_mut_slice ( & mut self ) -> & mut [ T ] {
241
+ fn as_mut_slice ( & mut self ) -> & mut [ Self :: Element ] {
247
242
self . 0 . as_mut_slice ( )
248
243
}
249
244
}
@@ -256,23 +251,22 @@ impl<T> UnprotectedStorage<T> for VecStorage<T> {
256
251
use std:: ptr;
257
252
for ( i, v) in self . 0 . iter_mut ( ) . enumerate ( ) {
258
253
if has. contains ( i as u32 ) {
259
- ptr:: drop_in_place ( v) ;
254
+ // drop in place
255
+ ptr:: drop_in_place ( & mut * v. as_mut_ptr ( ) ) ;
260
256
}
261
257
}
262
258
self . 0 . set_len ( 0 ) ;
263
259
}
264
260
265
261
unsafe fn get ( & self , id : Index ) -> & T {
266
- self . 0 . get_unchecked ( id as usize )
262
+ & * self . 0 . get_unchecked ( id as usize ) . as_ptr ( )
267
263
}
268
264
269
265
unsafe fn get_mut ( & mut self , id : Index ) -> & mut T {
270
- self . 0 . get_unchecked_mut ( id as usize )
266
+ & mut * self . 0 . get_unchecked_mut ( id as usize ) . as_mut_ptr ( )
271
267
}
272
268
273
269
unsafe fn insert ( & mut self , id : Index , v : T ) {
274
- use std:: ptr;
275
-
276
270
let id = id as usize ;
277
271
if self . 0 . len ( ) <= id {
278
272
let delta = id + 1 - self . 0 . len ( ) ;
@@ -281,12 +275,11 @@ impl<T> UnprotectedStorage<T> for VecStorage<T> {
281
275
}
282
276
// Write the value without reading or dropping
283
277
// the (currently uninitialized) memory.
284
- ptr :: write ( self . 0 . get_unchecked_mut ( id) , v) ;
278
+ * self . 0 . get_unchecked_mut ( id as usize ) = MaybeUninit :: new ( v) ;
285
279
}
286
280
287
281
unsafe fn remove ( & mut self , id : Index ) -> T {
288
282
use std:: ptr;
289
-
290
283
ptr:: read ( self . get ( id) )
291
284
}
292
285
}
@@ -347,16 +340,18 @@ impl<T> UnprotectedStorage<T> for DefaultVecStorage<T> where T: Default {
347
340
348
341
unsafe impl < T > DistinctStorage for DefaultVecStorage < T > { }
349
342
350
- impl < T > SafeSliceAccess < T > for DefaultVecStorage < T > {
343
+ impl < T > SliceAccess < T > for DefaultVecStorage < T > {
344
+ type Element = T ;
345
+
351
346
/// Returns a slice of all the components in this storage.
352
347
#[ inline]
353
- fn as_slice ( & self ) -> & [ T ] {
348
+ fn as_slice ( & self ) -> & [ Self :: Element ] {
354
349
self . 0 . as_slice ( )
355
350
}
356
351
357
352
/// Returns a mutable slice of all the components in this storage.
358
353
#[ inline]
359
- fn as_mut_slice ( & mut self ) -> & mut [ T ] {
354
+ fn as_mut_slice ( & mut self ) -> & mut [ Self :: Element ] {
360
355
self . 0 . as_mut_slice ( )
361
356
}
362
357
}
0 commit comments