Skip to content

Commit 16d6760

Browse files
committed
add callback test for detecting recursion
1 parent 225e793 commit 16d6760

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

crates/bevy_asset/src/callback.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<I, O> VisitAssetDependencies for Callback<I, O> {
3333
impl<I: TypePath, O: TypePath> Asset for Callback<I, O> {}
3434

3535
// TODO: Add docs
36-
#[derive(Error)]
36+
#[derive(Error, PartialEq, Eq)]
3737
pub enum CallbackError<I: TypePath, O: TypePath> {
3838
// TODO: Add docs
3939
#[error("Callback {0:?} was not found")]
@@ -380,4 +380,28 @@ mod tests {
380380
let _ = app.world.run_callback(nested_id);
381381
assert_eq!(*app.world.resource::<Counter>(), Counter(5));
382382
}
383+
384+
#[test]
385+
fn error_on_recursive_call() {
386+
#[derive(Component)]
387+
struct Call(Handle<Callback>);
388+
389+
let mut app = App::new();
390+
app.add_plugins(AssetPlugin::default());
391+
app.insert_resource(Counter(0));
392+
393+
let mut callbacks = app.world.resource_mut::<Assets<Callback>>();
394+
395+
let call_self = callbacks.add(Callback::from_system(|world: &mut World| {
396+
let callbacks = world.resource::<Assets<Callback>>();
397+
// this is this callback's id because it is the only callback.
398+
let own_id = callbacks.iter().next().unwrap().0.clone();
399+
let own_handle = Handle::Weak(own_id);
400+
let result = world.run_callback(own_handle.clone());
401+
402+
assert_eq!(result, Err(CallbackError::Recursive(own_handle)));
403+
}));
404+
405+
let _ = app.world.run_callback(call_self);
406+
}
383407
}

0 commit comments

Comments
 (0)