better error message: specify which resource is missing (#1648)

This commit is contained in:
Jakob Hellermann 2021-03-14 00:36:16 +00:00
parent 86e2fc53d0
commit ac661188c8
2 changed files with 30 additions and 10 deletions

View file

@ -234,7 +234,12 @@ impl<'a, T: Component> SystemParamFetch<'a> for ResState<T> {
) -> 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::<T>()
)
});
Res {
value: &*column.get_ptr().as_ptr().cast::<T>(),
flags: *column.get_flags_mut_ptr(),
@ -371,7 +376,12 @@ impl<'a, T: Component> SystemParamFetch<'a> for ResMutState<T> {
) -> 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::<T>()
)
});
ResMut {
value: value.value,
flags: value.flags,
@ -603,7 +613,12 @@ impl<'a, T: 'static> SystemParamFetch<'a> for NonSendState<T> {
NonSend {
value: world
.get_non_send_with_id::<T>(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::<T>()
)
}),
}
}
}
@ -692,7 +707,12 @@ impl<'a, T: 'static> SystemParamFetch<'a> for NonSendMutState<T> {
) -> 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::<T>()
)
});
NonSendMut {
value: value.value,
flags: value.flags,

View file

@ -620,15 +620,15 @@ impl World {
let component_id = self
.components
.get_resource_id(TypeId::of::<T>())
.expect("resource does not exist");
.unwrap_or_else(|| panic!("resource does not exist: {}", std::any::type_name::<T>()));
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::<T>())
});
if column.is_empty() {
panic!("resource does not exist");
panic!("resource does not exist: {}", std::any::type_name::<T>());
}
// 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::<T>()));
// SAFE: new location is immediately written to below
let row = unsafe { column.push_uninit() };
// SAFE: row was just allocated above