@@ -7,6 +7,8 @@ use bevy::{
7
7
use dashmap:: { DashMap , Entry } ;
8
8
use smallvec:: SmallVec ;
9
9
10
+ use crate :: error:: InteropError ;
11
+
10
12
use super :: { ReflectAllocationId , ReflectBase } ;
11
13
12
14
#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -80,8 +82,8 @@ pub enum ReflectAccessKind {
80
82
/// for script owned values this is an allocationId, this is used to ensure we have permission to access the value.
81
83
#[ derive( PartialEq , Eq , Copy , Clone , Hash , Debug ) ]
82
84
pub struct ReflectAccessId {
83
- kind : ReflectAccessKind ,
84
- id : u64 ,
85
+ pub ( crate ) kind : ReflectAccessKind ,
86
+ pub ( crate ) id : u64 ,
85
87
}
86
88
87
89
impl AccessMapKey for ReflectAccessId {
@@ -111,19 +113,25 @@ impl AccessMapKey for ReflectAccessId {
111
113
}
112
114
113
115
impl ReflectAccessId {
114
- pub fn for_resource < R : Resource > ( cell : & UnsafeWorldCell ) -> Option < Self > {
115
- Some ( Self {
116
+ pub fn for_resource < R : Resource > ( cell : & UnsafeWorldCell ) -> Result < Self , InteropError > {
117
+ let resource_id = cell. components ( ) . resource_id :: < R > ( ) . ok_or_else ( || {
118
+ InteropError :: unregistered_component_or_resource_type ( std:: any:: type_name :: < R > ( ) )
119
+ } ) ?;
120
+
121
+ Ok ( Self {
116
122
kind : ReflectAccessKind :: ComponentOrResource ,
117
- id : cell . components ( ) . resource_id :: < R > ( ) ? . index ( ) as u64 ,
123
+ id : resource_id. index ( ) as u64 ,
118
124
} )
119
125
}
120
126
121
127
pub fn for_component < C : bevy:: ecs:: component:: Component > (
122
128
cell : & UnsafeWorldCell ,
123
- ) -> Option < Self > {
124
- let component_id = cell. components ( ) . component_id :: < C > ( ) ?;
129
+ ) -> Result < Self , InteropError > {
130
+ let component_id = cell. components ( ) . component_id :: < C > ( ) . ok_or_else ( || {
131
+ InteropError :: unregistered_component_or_resource_type ( std:: any:: type_name :: < C > ( ) )
132
+ } ) ?;
125
133
126
- Some ( Self :: for_component_id ( component_id) )
134
+ Ok ( Self :: for_component_id ( component_id) )
127
135
}
128
136
129
137
pub fn for_allocation ( id : ReflectAllocationId ) -> Self {
@@ -140,11 +148,11 @@ impl ReflectAccessId {
140
148
}
141
149
}
142
150
143
- pub fn for_reference ( base : ReflectBase ) -> Option < Self > {
151
+ pub fn for_reference ( base : ReflectBase ) -> Self {
144
152
match base {
145
- ReflectBase :: Resource ( id) => Some ( Self :: for_component_id ( id) ) ,
146
- ReflectBase :: Component ( _, id) => Some ( Self :: for_component_id ( id) ) ,
147
- ReflectBase :: Owned ( id) => Some ( Self :: for_allocation ( id) ) ,
153
+ ReflectBase :: Resource ( id) => Self :: for_component_id ( id) ,
154
+ ReflectBase :: Component ( _, id) => Self :: for_component_id ( id) ,
155
+ ReflectBase :: Owned ( id) => Self :: for_allocation ( id) ,
148
156
}
149
157
}
150
158
}
@@ -173,6 +181,12 @@ impl From<ReflectAccessId> for ComponentId {
173
181
}
174
182
}
175
183
184
+ impl From < ReflectAccessId > for ReflectAllocationId {
185
+ fn from ( val : ReflectAccessId ) -> Self {
186
+ ReflectAllocationId :: new ( val. id )
187
+ }
188
+ }
189
+
176
190
#[ derive( Debug , Default ) ]
177
191
pub struct AccessMap {
178
192
individual_accesses : DashMap < u64 , AccessCount > ,
@@ -323,17 +337,15 @@ impl DisplayCodeLocation for Option<std::panic::Location<'_>> {
323
337
macro_rules! with_access_read {
324
338
( $access_map: expr, $id: expr, $msg: expr, $body: block) => { {
325
339
if !$access_map. claim_read_access( $id) {
326
- panic!(
327
- "{}. Aliasing access is held somewhere else: {}" ,
340
+ Err ( $crate:: error:: InteropError :: cannot_claim_access(
341
+ $id,
342
+ $access_map. access_location( $id) ,
328
343
$msg,
329
- $crate:: bindings:: access_map:: DisplayCodeLocation :: display_location(
330
- $access_map. access_location( $id)
331
- )
332
- ) ;
344
+ ) )
333
345
} else {
334
346
let result = $body;
335
347
$access_map. release_access( $id) ;
336
- result
348
+ Ok ( result)
337
349
}
338
350
} } ;
339
351
}
@@ -342,17 +354,15 @@ macro_rules! with_access_read {
342
354
macro_rules! with_access_write {
343
355
( $access_map: expr, $id: expr, $msg: expr, $body: block) => {
344
356
if !$access_map. claim_write_access( $id) {
345
- panic!(
346
- "{}. Aliasing access is held somewhere else: {}" ,
357
+ Err ( $crate:: error:: InteropError :: cannot_claim_access(
358
+ $id,
359
+ $access_map. access_location( $id) ,
347
360
$msg,
348
- $crate:: bindings:: access_map:: DisplayCodeLocation :: display_location(
349
- $access_map. access_location( $id)
350
- )
351
- ) ;
361
+ ) )
352
362
} else {
353
363
let result = $body;
354
364
$access_map. release_access( $id) ;
355
- result
365
+ Ok ( result)
356
366
}
357
367
} ;
358
368
}
0 commit comments