@@ -33,7 +33,7 @@ trait ResourceStorage: Downcast {}
33
33
impl_downcast ! ( ResourceStorage ) ;
34
34
35
35
struct StoredResource < T : ' static > {
36
- value : T ,
36
+ value : std :: cell :: UnsafeCell < T > ,
37
37
atomic_borrow : AtomicBorrow ,
38
38
}
39
39
@@ -45,27 +45,24 @@ impl<T: 'static> VecResourceStorage<T> {
45
45
fn get ( & self , index : usize ) -> Option < ResourceRef < ' _ , T > > {
46
46
self . stored
47
47
. get ( index)
48
- . map ( |stored| ResourceRef :: new ( & stored. value , & stored . atomic_borrow ) )
48
+ . map ( |stored| ResourceRef :: new ( stored) )
49
49
}
50
50
51
51
fn get_mut ( & self , index : usize ) -> Option < ResourceRefMut < ' _ , T > > {
52
- self . stored . get ( index) . map ( |stored|
53
- // SAFE: ResourceRefMut ensures that this borrow is unique
54
- unsafe {
55
- let value = & stored. value as * const T as * mut T ;
56
- ResourceRefMut :: new ( & mut * value, & stored. atomic_borrow )
57
- } )
52
+ self . stored
53
+ . get ( index)
54
+ . map ( |stored| ResourceRefMut :: new ( stored) )
58
55
}
59
56
60
57
fn push ( & mut self , resource : T ) {
61
58
self . stored . push ( StoredResource {
62
59
atomic_borrow : AtomicBorrow :: new ( ) ,
63
- value : resource,
60
+ value : std :: cell :: UnsafeCell :: new ( resource) ,
64
61
} )
65
62
}
66
63
67
64
fn set ( & mut self , index : usize , resource : T ) {
68
- self . stored [ index] . value = resource;
65
+ self . stored [ index] . value = std :: cell :: UnsafeCell :: new ( resource) ;
69
66
}
70
67
71
68
fn is_empty ( & self ) -> bool {
@@ -414,9 +411,24 @@ pub struct ResourceRef<'a, T: 'static> {
414
411
415
412
impl < ' a , T : ' static > ResourceRef < ' a , T > {
416
413
/// Creates a new resource borrow
417
- pub fn new ( resource : & ' a T , borrow : & ' a AtomicBorrow ) -> Self {
418
- borrow. borrow ( ) ;
419
- Self { resource, borrow }
414
+ fn new (
415
+ StoredResource {
416
+ value,
417
+ atomic_borrow,
418
+ } : & ' a StoredResource < T > ,
419
+ ) -> Self {
420
+ if atomic_borrow. borrow ( ) {
421
+ Self {
422
+ // Safe because we acquired the lock
423
+ resource : unsafe { & * value. get ( ) } ,
424
+ borrow : atomic_borrow,
425
+ }
426
+ } else {
427
+ panic ! (
428
+ "Failed to acquire shared lock on resource: {}" ,
429
+ std:: any:: type_name:: <T >( )
430
+ ) ;
431
+ }
420
432
}
421
433
}
422
434
@@ -454,9 +466,24 @@ pub struct ResourceRefMut<'a, T: 'static> {
454
466
455
467
impl < ' a , T : ' static > ResourceRefMut < ' a , T > {
456
468
/// Creates a new entity component mutable borrow
457
- pub fn new ( resource : & ' a mut T , borrow : & ' a AtomicBorrow ) -> Self {
458
- borrow. borrow_mut ( ) ;
459
- Self { resource, borrow }
469
+ fn new (
470
+ StoredResource {
471
+ value,
472
+ atomic_borrow,
473
+ } : & ' a StoredResource < T > ,
474
+ ) -> Self {
475
+ if atomic_borrow. borrow_mut ( ) {
476
+ Self {
477
+ // Safe because we acquired the lock
478
+ resource : unsafe { & mut * value. get ( ) } ,
479
+ borrow : atomic_borrow,
480
+ }
481
+ } else {
482
+ panic ! (
483
+ "Failed to acquire exclusive lock on resource: {}" ,
484
+ std:: any:: type_name:: <T >( )
485
+ ) ;
486
+ }
460
487
}
461
488
}
462
489
0 commit comments