Skip to content

Commit a111905

Browse files
DJMcNabexjam
authored andcommitted
Add some more documentation to SystemParam (bevyengine#4787)
# Objective - Fixes bevyengine#4783 ## Solution - Add more documentation about the derive, and the obscure failure case for this. - Link to [`StaticSystemParam`](https://docs.rs/bevy/latest/bevy/ecs/system/struct.StaticSystemParam.html) in these docs. - Also explain the attributes whilst here.
1 parent adee0d2 commit a111905

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,27 @@ use std::{
2525
///
2626
/// # Derive
2727
///
28-
/// This trait can be derived with the [`derive@super::SystemParam`] macro. The only requirement
29-
/// is that every struct field must also implement `SystemParam`.
28+
/// This trait can be derived with the [`derive@super::SystemParam`] macro.
29+
/// This macro only works if each field on the derived struct implements [`SystemParam`].
30+
/// Note: There are additional requirements on the field types.
31+
/// See the *Generic `SystemParam`s* section for details and workarounds of the probable
32+
/// cause if this derive causes an error to be emitted.
33+
///
34+
///
35+
/// The struct for which `SystemParam` is derived must (currently) have exactly
36+
/// two lifetime parameters.
37+
/// The first is the lifetime of the world, and the second the lifetime
38+
/// of the parameter's state.
39+
///
40+
/// ## Attributes
41+
///
42+
/// `#[system_param(ignore)]`:
43+
/// Can be added to any field in the struct. Fields decorated with this attribute
44+
/// will created with the default value upon realisation.
45+
/// This is most useful for `PhantomData` fields, to ensure that the required lifetimes are
46+
/// used, as shown in the example.
47+
///
48+
/// # Example
3049
///
3150
/// ```
3251
/// # use bevy_ecs::prelude::*;
@@ -46,6 +65,30 @@ use std::{
4665
///
4766
/// # bevy_ecs::system::assert_is_system(my_system);
4867
/// ```
68+
///
69+
/// # Generic `SystemParam`s
70+
///
71+
/// When using the derive macro, you may see an error in the form of:
72+
///
73+
/// ```text
74+
/// expected ... [ParamType]
75+
/// found associated type `<<[ParamType] as SystemParam>::Fetch as SystemParamFetch<'_, '_>>::Item`
76+
/// ```
77+
/// where `[ParamType]` is the type of one of your fields.
78+
/// To solve this error, you can wrap the field of type `[ParamType]` with [`StaticSystemParam`]
79+
/// (i.e. `StaticSystemParam<[ParamType]>`).
80+
///
81+
/// ## Details
82+
///
83+
/// The derive macro requires that the [`SystemParam`] implementation of
84+
/// each field `F`'s [`Fetch`](`SystemParam::Fetch`)'s [`Item`](`SystemParamFetch::Item`) is itself `F`
85+
/// (ignoring lifetimes for simplicity).
86+
/// This assumption is due to type inference reasons, so that the derived [`SystemParam`] can be
87+
/// used as an argument to a function system.
88+
/// If the compiler cannot validate this property for `[ParamType]`, it will error in the form shown above.
89+
///
90+
/// This will most commonly occur when working with `SystemParam`s generically, as the requirement
91+
/// has not been proven to the compiler.
4992
pub trait SystemParam: Sized {
5093
type Fetch: for<'w, 's> SystemParamFetch<'w, 's>;
5194
}

0 commit comments

Comments
 (0)