@@ -5,14 +5,18 @@ use crate::storage::{Column, SparseSet};
5
5
use bevy_ptr:: { OwningPtr , Ptr , PtrMut , UnsafeCellDeref } ;
6
6
use std:: cell:: UnsafeCell ;
7
7
8
+ pub ( crate ) struct ResourceInfo {
9
+ pub data : Column ,
10
+ pub component_info : ArchetypeComponentInfo ,
11
+ }
12
+
8
13
/// The backing store for all [`Resource`]s stored in the [`World`].
9
14
///
10
15
/// [`Resource`]: crate::system::Resource
11
16
/// [`World`]: crate::world::World
12
17
#[ derive( Default ) ]
13
18
pub struct Resources {
14
- pub ( crate ) resources : SparseSet < ComponentId , Column > ,
15
- pub ( crate ) components : SparseSet < ComponentId , ArchetypeComponentInfo > ,
19
+ pub ( crate ) resources : SparseSet < ComponentId , ResourceInfo > ,
16
20
}
17
21
18
22
impl Resources {
@@ -22,9 +26,9 @@ impl Resources {
22
26
& self ,
23
27
component_id : ComponentId ,
24
28
) -> Option < ArchetypeComponentId > {
25
- self . components
29
+ self . resources
26
30
. get ( component_id)
27
- . map ( |info| info. archetype_component_id )
31
+ . map ( |info| info. component_info . archetype_component_id )
28
32
}
29
33
30
34
/// The total number of resoruces stored in the [`World`]
@@ -44,35 +48,39 @@ impl Resources {
44
48
self . resources . is_empty ( )
45
49
}
46
50
51
+ /// Gets a read-only [`Ptr`] to a resource, if available.
47
52
#[ inline]
48
53
pub fn get ( & self , component_id : ComponentId ) -> Option < Ptr < ' _ > > {
49
- let column = self . resources . get ( component_id) ?;
54
+ let column = & self . resources . get ( component_id) ?. data ;
50
55
// SAFE: if a resource column exists, row 0 exists as well. caller takes ownership of the
51
56
// ptr value / drop is called when R is dropped
52
57
( !column. is_empty ( ) ) . then ( || unsafe { column. get_data_unchecked ( 0 ) } )
53
58
}
54
59
60
+ /// Gets a read-only [`Ptr`] to a resource, if available.
55
61
#[ inline]
56
62
pub fn get_mut ( & mut self , component_id : ComponentId ) -> Option < PtrMut < ' _ > > {
57
- let column = self . resources . get_mut ( component_id) ?;
63
+ let column = & mut self . resources . get_mut ( component_id) ?. data ;
58
64
// SAFE: if a resource column exists, row 0 exists as well. caller takes ownership of the
59
65
// ptr value / drop is called when R is dropped
60
66
( !column. is_empty ( ) ) . then ( || unsafe { column. get_data_unchecked_mut ( 0 ) } )
61
67
}
62
68
69
+ /// Gets the [`ComponentTicks`] to a resource, if available.
63
70
#[ inline]
64
71
pub fn get_ticks ( & self , component_id : ComponentId ) -> Option < & ComponentTicks > {
65
- let column = self . resources . get ( component_id) ?;
72
+ let column = & self . resources . get ( component_id) ?. data ;
66
73
// SAFE: if a resource column exists, row 0 exists as well. caller takes ownership of the
67
74
// ptr value / drop is called when R is dropped
68
75
( !column. is_empty ( ) ) . then ( || unsafe { column. get_ticks_unchecked ( 0 ) . deref ( ) } )
69
76
}
70
77
78
+ /// Checks if the a resource is currently stored with a given ID.
71
79
#[ inline]
72
80
pub fn contains ( & self , component_id : ComponentId ) -> bool {
73
81
self . resources
74
82
. get ( component_id)
75
- . map ( |column | !column . is_empty ( ) )
83
+ . map ( |info | !info . data . is_empty ( ) )
76
84
. unwrap_or ( false )
77
85
}
78
86
@@ -81,32 +89,40 @@ impl Resources {
81
89
& self ,
82
90
component_id : ComponentId ,
83
91
) -> Option < ( Ptr < ' _ > , & UnsafeCell < ComponentTicks > ) > {
84
- let column = self . resources . get ( component_id) ?;
92
+ let column = & self . resources . get ( component_id) ?. data ;
85
93
// SAFE: if a resource column exists, row 0 exists as well. caller takes ownership of the
86
94
// ptr value / drop is called when R is dropped
87
95
( !column. is_empty ( ) )
88
96
. then ( || unsafe { ( column. get_data_unchecked ( 0 ) , column. get_ticks_unchecked ( 0 ) ) } )
89
97
}
90
98
99
+ /// Inserts a resource into the world.
100
+ ///
91
101
/// # Safety
92
- /// - ptr must point to valid data of this column's component type
102
+ /// ptr must point to valid data of this column's component type which
103
+ /// must correspond to the provided ID.
93
104
#[ inline]
94
105
pub unsafe fn insert (
95
106
& mut self ,
96
107
component_id : ComponentId ,
97
108
data : OwningPtr < ' _ > ,
98
109
ticks : ComponentTicks ,
99
110
) -> Option < ( ) > {
100
- let column = self . resources . get_mut ( component_id) ?;
111
+ let column = & mut self . resources . get_mut ( component_id) ?. data ;
101
112
debug_assert ! ( column. is_empty( ) ) ;
102
113
column. push ( data, ticks) ;
103
114
Some ( ( ) )
104
115
}
105
116
117
+ /// Removes a resource from the world.
118
+ ///
119
+ /// # Safety
120
+ /// ptr must point to valid data of this column's component type which
121
+ /// must correspond to the provided ID.
106
122
#[ inline]
107
123
#[ must_use = "The returned pointer to the removed component should be used or dropped" ]
108
124
pub fn remove ( & mut self , component_id : ComponentId ) -> Option < ( OwningPtr < ' _ > , ComponentTicks ) > {
109
- let column = self . resources . get_mut ( component_id) ?;
125
+ let column = & mut self . resources . get_mut ( component_id) ?. data ;
110
126
if column. is_empty ( ) {
111
127
return None ;
112
128
}
@@ -117,7 +133,7 @@ impl Resources {
117
133
118
134
#[ inline]
119
135
pub ( crate ) fn remove_and_drop ( & mut self , component_id : ComponentId ) -> Option < ( ) > {
120
- let column = self . resources . get_mut ( component_id) ?;
136
+ let column = & mut self . resources . get_mut ( component_id) ?. data ;
121
137
if column. is_empty ( ) {
122
138
return None ;
123
139
}
@@ -130,8 +146,8 @@ impl Resources {
130
146
}
131
147
132
148
pub fn check_change_ticks ( & mut self , change_tick : u32 ) {
133
- for column in self . resources . values_mut ( ) {
134
- column . check_change_ticks ( change_tick) ;
149
+ for info in self . resources . values_mut ( ) {
150
+ info . data . check_change_ticks ( change_tick) ;
135
151
}
136
152
}
137
153
}
0 commit comments