Skip to content

Commit 38ed79f

Browse files
committed
Made SystemParamError for optional params
1 parent 193c9de commit 38ed79f

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
455455
},
456456
SystemParamUsage::Resultful(_) => match field_fallibility {
457457
SystemParamUsage::Infallible => (quote! { #ident }, quote! { #ty }),
458-
SystemParamUsage::Optional => (quote! { match #ident { Some(s) => s, None => return Err(().into()), } }, quote! { Option<#ty> }),
458+
SystemParamUsage::Optional => (quote! { match #ident { Some(s) => s, None => return Err(#bevy_ecs::system::SystemParamError::<#ty>::default().into()), } }, quote! { Option<#ty> }),
459459
SystemParamUsage::Resultful(_) => (quote! { match #ident { Ok(o) => o, Err(e) => return Err(e.into()), } }, quote! { Result<#ty, <#ty as #bevy_ecs::system::ResultfulSystemParam>::Error> }),
460460
SystemParamUsage::Ignore => unreachable!(),
461461
},

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,9 @@ unsafe impl<'a, T: Resource> ReadOnlySystemParam for Res<'a, T> {}
730730

731731
// SAFETY: Res ComponentId and ArchetypeComponentId access is applied to SystemMeta. If this Res
732732
// conflicts with any prior access, an error will occur.
733-
unsafe impl<'a, T: Resource> ResultfulSystemParam for Res<'a, T> {
733+
unsafe impl<'a, T: Resource> OptionalSystemParam for Res<'a, T> {
734734
type State = ComponentId;
735735
type Item<'w, 's> = Res<'w, T>;
736-
type Error = SystemParamError<Self>;
737736

738737
fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State {
739738
let component_id = world.initialize_resource::<T>();
@@ -764,7 +763,7 @@ unsafe impl<'a, T: Resource> ResultfulSystemParam for Res<'a, T> {
764763
system_meta: &SystemMeta,
765764
world: &'w World,
766765
change_tick: u32,
767-
) -> Result<Self::Item<'w, 's>, <Self::Item<'w, 's> as ResultfulSystemParam>::Error> {
766+
) -> Option<Self::Item<'w, 's>> {
768767
world
769768
.get_resource_with_ticks(component_id)
770769
.map(|(ptr, ticks)| Res {
@@ -774,16 +773,14 @@ unsafe impl<'a, T: Resource> ResultfulSystemParam for Res<'a, T> {
774773
last_change_tick: system_meta.last_change_tick,
775774
change_tick,
776775
})
777-
.ok_or(SystemParamError::default())
778776
}
779777
}
780778

781779
// SAFETY: Res ComponentId and ArchetypeComponentId access is applied to SystemMeta. If this Res
782780
// conflicts with any prior access, a panic will occur.
783-
unsafe impl<'a, T: Resource> ResultfulSystemParam for ResMut<'a, T> {
781+
unsafe impl<'a, T: Resource> OptionalSystemParam for ResMut<'a, T> {
784782
type State = ComponentId;
785783
type Item<'w, 's> = ResMut<'w, T>;
786-
type Error = SystemParamError<Self>;
787784

788785
fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State {
789786
let component_id = world.initialize_resource::<T>();
@@ -817,7 +814,7 @@ unsafe impl<'a, T: Resource> ResultfulSystemParam for ResMut<'a, T> {
817814
system_meta: &SystemMeta,
818815
world: &'w World,
819816
change_tick: u32,
820-
) -> Result<Self::Item<'w, 's>, <Self::Item<'w, 's> as ResultfulSystemParam>::Error> {
817+
) -> Option<Self::Item<'w, 's>> {
821818
world
822819
.get_resource_unchecked_mut_with_id(component_id)
823820
.map(|value| ResMut {
@@ -829,7 +826,6 @@ unsafe impl<'a, T: Resource> ResultfulSystemParam for ResMut<'a, T> {
829826
change_tick,
830827
},
831828
})
832-
.ok_or(SystemParamError::default())
833829
}
834830
}
835831

@@ -1735,6 +1731,7 @@ mod tests {
17351731
pub struct EncapsulatedParam<'w>(Res<'w, PrivateResource>);
17361732

17371733
#[derive(SystemParam)]
1734+
#[system_param(optional)]
17381735
pub struct OptionalParam<'w> {
17391736
_r0: Res<'w, R<0>>,
17401737
_r1: Res<'w, R<1>>,

0 commit comments

Comments
 (0)