-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve type inference in DynSystemParam::downcast()
by making the type parameter match the return value.
#15103
Merged
alice-i-cecile
merged 5 commits into
bevyengine:main
from
chescock:SystemParamBuilder-DynSystemParam-inference
Sep 16, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
832e9e0
Improve type inference in DynSystemParam::downcast() by making the ty…
chescock 7393c4a
Add dedicated test for inference.
chescock b436a62
Document the complex where clause.
chescock 84375dc
Revert changes to dyn_builder test now that there is a dedicated test…
chescock 263fe68
Make the where clause on the `is` method match the ones on the `downc…
chescock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1894,7 +1894,9 @@ unsafe impl<T: ?Sized> ReadOnlySystemParam for PhantomData<T> {} | |
/// assert!(param.is::<Res<A>>()); | ||
/// assert!(!param.is::<Res<B>>()); | ||
/// assert!(param.downcast_mut::<Res<B>>().is_none()); | ||
/// let foo: Res<A> = param.downcast::<Res<A>>().unwrap(); | ||
/// let res = param.downcast_mut::<Res<A>>().unwrap(); | ||
/// // The type parameter can be left out if it can be determined from use. | ||
/// let res: Res<A> = param.downcast().unwrap(); | ||
/// } | ||
/// | ||
/// let system = ( | ||
|
@@ -1942,13 +1944,21 @@ impl<'w, 's> DynSystemParam<'w, 's> { | |
} | ||
|
||
/// Returns `true` if the inner system param is the same as `T`. | ||
pub fn is<T: SystemParam + 'static>(&self) -> bool { | ||
self.state.is::<ParamState<T>>() | ||
pub fn is<T: SystemParam>(&self) -> bool | ||
// See downcast() function for an explanation of the where clause | ||
where | ||
T::Item<'static, 'static>: SystemParam<Item<'w, 's> = T> + 'static, | ||
{ | ||
self.state.is::<ParamState<T::Item<'static, 'static>>>() | ||
} | ||
|
||
/// Returns the inner system param if it is the correct type. | ||
/// This consumes the dyn param, so the returned param can have its original world and state lifetimes. | ||
pub fn downcast<T: SystemParam + 'static>(self) -> Option<T::Item<'w, 's>> { | ||
pub fn downcast<T: SystemParam>(self) -> Option<T> | ||
// See downcast() function for an explanation of the where clause | ||
where | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This where clause could probably use a doc comment: it's very complex. |
||
T::Item<'static, 'static>: SystemParam<Item<'w, 's> = T> + 'static, | ||
{ | ||
// SAFETY: | ||
// - `DynSystemParam::new()` ensures `state` is a `ParamState<T>`, that the world matches, | ||
// and that it has access required by the inner system param. | ||
|
@@ -1958,7 +1968,11 @@ impl<'w, 's> DynSystemParam<'w, 's> { | |
|
||
/// Returns the inner system parameter if it is the correct type. | ||
/// This borrows the dyn param, so the returned param is only valid for the duration of that borrow. | ||
pub fn downcast_mut<T: SystemParam + 'static>(&mut self) -> Option<T::Item<'_, '_>> { | ||
pub fn downcast_mut<'a, T: SystemParam>(&'a mut self) -> Option<T> | ||
// See downcast() function for an explanation of the where clause | ||
where | ||
T::Item<'static, 'static>: SystemParam<Item<'a, 'a> = T> + 'static, | ||
{ | ||
// SAFETY: | ||
// - `DynSystemParam::new()` ensures `state` is a `ParamState<T>`, that the world matches, | ||
// and that it has access required by the inner system param. | ||
|
@@ -1971,9 +1985,11 @@ impl<'w, 's> DynSystemParam<'w, 's> { | |
/// but since it only performs read access it can keep the original world lifetime. | ||
/// This can be useful with methods like [`Query::iter_inner()`] or [`Res::into_inner()`] | ||
/// to obtain references with the original world lifetime. | ||
pub fn downcast_mut_inner<T: ReadOnlySystemParam + 'static>( | ||
&mut self, | ||
) -> Option<T::Item<'w, '_>> { | ||
pub fn downcast_mut_inner<'a, T: ReadOnlySystemParam>(&'a mut self) -> Option<T> | ||
// See downcast() function for an explanation of the where clause | ||
where | ||
T::Item<'static, 'static>: SystemParam<Item<'w, 'a> = T> + 'static, | ||
{ | ||
// SAFETY: | ||
// - `DynSystemParam::new()` ensures `state` is a `ParamState<T>`, that the world matches, | ||
// and that it has access required by the inner system param. | ||
|
@@ -1988,19 +2004,32 @@ impl<'w, 's> DynSystemParam<'w, 's> { | |
/// in [`init_state`](SystemParam::init_state) for the inner system param. | ||
/// - `world` must be the same `World` that was used to initialize | ||
/// [`state`](SystemParam::init_state) for the inner system param. | ||
unsafe fn downcast<'w, 's, T: SystemParam + 'static>( | ||
unsafe fn downcast<'w, 's, T: SystemParam>( | ||
state: &'s mut dyn Any, | ||
system_meta: &SystemMeta, | ||
world: UnsafeWorldCell<'w>, | ||
change_tick: Tick, | ||
) -> Option<T::Item<'w, 's>> { | ||
state.downcast_mut::<ParamState<T>>().map(|state| { | ||
// SAFETY: | ||
// - The caller ensures the world has access for the underlying system param, | ||
// and since the downcast succeeded, the underlying system param is T. | ||
// - The caller ensures the `world` matches. | ||
unsafe { T::get_param(&mut state.0, system_meta, world, change_tick) } | ||
}) | ||
) -> Option<T> | ||
// We need a 'static version of the SystemParam to use with `Any::downcast_mut()`, | ||
// and we need a <'w, 's> version to actually return. | ||
// The type parameter T must be the one we return in order to get type inference from the return value. | ||
// So we use `T::Item<'static, 'static>` as the 'static version, and require that it be 'static. | ||
// That means the return value will be T::Item<'static, 'static>::Item<'w, 's>, | ||
// so we constrain that to be equal to T. | ||
// Every actual `SystemParam` implementation has `T::Item == T` up to lifetimes, | ||
// so they should all work with this constraint. | ||
where | ||
T::Item<'static, 'static>: SystemParam<Item<'w, 's> = T> + 'static, | ||
{ | ||
state | ||
.downcast_mut::<ParamState<T::Item<'static, 'static>>>() | ||
.map(|state| { | ||
// SAFETY: | ||
// - The caller ensures the world has access for the underlying system param, | ||
// and since the downcast succeeded, the underlying system param is T. | ||
// - The caller ensures the `world` matches. | ||
unsafe { T::Item::get_param(&mut state.0, system_meta, world, change_tick) } | ||
}) | ||
} | ||
|
||
/// The [`SystemParam::State`] for a [`DynSystemParam`]. | ||
|
@@ -2323,4 +2352,12 @@ mod tests { | |
schedule.add_systems((non_send_param_set, non_send_param_set, non_send_param_set)); | ||
schedule.run(&mut world); | ||
} | ||
|
||
fn _dyn_system_param_type_inference(mut p: DynSystemParam) { | ||
// Make sure the downcast() methods are able to infer their type parameters from the use of the return type. | ||
// This is just a compilation test, so there is nothing to run. | ||
let _query: Query<()> = p.downcast_mut().unwrap(); | ||
let _query: Query<()> = p.downcast_mut_inner().unwrap(); | ||
let _query: Query<()> = p.downcast().unwrap(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice documentation, clearly shows the benefit this PR adds.