From ac661188c8679e767c4f2abebee22b4db3128e99 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Sun, 14 Mar 2021 00:36:16 +0000 Subject: [PATCH] better error message: specify which resource is missing (#1648) --- crates/bevy_ecs/src/system/system_param.rs | 28 ++++++++++++++++++---- crates/bevy_ecs/src/world/mod.rs | 12 +++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 8131a32760..60d8bbe9e0 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -234,7 +234,12 @@ impl<'a, T: Component> SystemParamFetch<'a> for ResState { ) -> Self::Item { let column = world .get_populated_resource_column(state.component_id) - .expect("Requested resource does not exist"); + .unwrap_or_else(|| { + panic!( + "Requested resource does not exist: {}", + std::any::type_name::() + ) + }); Res { value: &*column.get_ptr().as_ptr().cast::(), flags: *column.get_flags_mut_ptr(), @@ -371,7 +376,12 @@ impl<'a, T: Component> SystemParamFetch<'a> for ResMutState { ) -> Self::Item { let value = world .get_resource_unchecked_mut_with_id(state.component_id) - .expect("Requested resource does not exist"); + .unwrap_or_else(|| { + panic!( + "Requested resource does not exist: {}", + std::any::type_name::() + ) + }); ResMut { value: value.value, flags: value.flags, @@ -603,7 +613,12 @@ impl<'a, T: 'static> SystemParamFetch<'a> for NonSendState { NonSend { value: world .get_non_send_with_id::(state.component_id) - .expect("Requested non-send resource does not exist"), + .unwrap_or_else(|| { + panic!( + "Requested non-send resource does not exist: {}", + std::any::type_name::() + ) + }), } } } @@ -692,7 +707,12 @@ impl<'a, T: 'static> SystemParamFetch<'a> for NonSendMutState { ) -> Self::Item { let value = world .get_non_send_unchecked_mut_with_id(state.component_id) - .expect("Requested non-send resource does not exist"); + .unwrap_or_else(|| { + panic!( + "Requested non-send resource does not exist: {}", + std::any::type_name::() + ) + }); NonSendMut { value: value.value, flags: value.flags, diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 43f87c7e91..952bb3ff4e 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -620,15 +620,15 @@ impl World { let component_id = self .components .get_resource_id(TypeId::of::()) - .expect("resource does not exist"); + .unwrap_or_else(|| panic!("resource does not exist: {}", std::any::type_name::())); let (ptr, mut flags) = { let resource_archetype = self.archetypes.resource_mut(); let unique_components = resource_archetype.unique_components_mut(); - let column = unique_components - .get_mut(component_id) - .expect("resource does not exist"); + let column = unique_components.get_mut(component_id).unwrap_or_else(|| { + panic!("resource does not exist: {}", std::any::type_name::()) + }); if column.is_empty() { - panic!("resource does not exist"); + panic!("resource does not exist: {}", std::any::type_name::()); } // SAFE: if a resource column exists, row 0 exists as well. caller takes ownership of // the ptr value / drop is called when T is dropped @@ -644,7 +644,7 @@ impl World { let unique_components = resource_archetype.unique_components_mut(); let column = unique_components .get_mut(component_id) - .expect("resource does not exist"); + .unwrap_or_else(|| panic!("resource does not exist: {}", std::any::type_name::())); // SAFE: new location is immediately written to below let row = unsafe { column.push_uninit() }; // SAFE: row was just allocated above