Skip to content

Commit

Permalink
Add FromWorld bound to T in Local<T> (bevyengine#5481)
Browse files Browse the repository at this point in the history
# Objective

Currently, actually using a `Local` on a system requires that it be `T: FromWorld`, but that requirement is only expressed on the `SystemParam` machinery, which leads to the confusing error message for when the user attempts to add an invalid system. By adding these bounds to `Local` directly, it improves clarity on usage and semantics. 

## Solution

- Add `T: FromWorld` bound to `Local`'s definition

## Migration Guide

- It might be possible for references to `Local`s without `T: FromWorld` to exist, but these should be exceedingly rare and probably dead code. In the event that one of these is encountered, the easiest solutions are to delete the code or wrap the inner `T` in an `Option` to allow it to be default constructed to `None`.
  • Loading branch information
PROMETHIA-27 authored and ItsDoot committed Feb 1, 2023
1 parent d88bfee commit 73c1773
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/system/system_param.rs
Expand Up @@ -640,12 +640,12 @@ impl<'w, 's> SystemParamFetch<'w, 's> for WorldState {
/// // .add_system(reset_to_system(my_config))
/// # assert_is_system(reset_to_system(Config(10)));
/// ```
pub struct Local<'a, T: Resource>(&'a mut T);
pub struct Local<'a, T: Resource + FromWorld>(&'a mut T);

// SAFETY: Local only accesses internal state
unsafe impl<T: Resource> ReadOnlySystemParamFetch for LocalState<T> {}

impl<'a, T: Resource> Debug for Local<'a, T>
impl<'a, T: Resource + FromWorld> Debug for Local<'a, T>
where
T: Debug,
{
Expand All @@ -654,7 +654,7 @@ where
}
}

impl<'a, T: Resource> Deref for Local<'a, T> {
impl<'a, T: Resource + FromWorld> Deref for Local<'a, T> {
type Target = T;

#[inline]
Expand All @@ -663,7 +663,7 @@ impl<'a, T: Resource> Deref for Local<'a, T> {
}
}

impl<'a, T: Resource> DerefMut for Local<'a, T> {
impl<'a, T: Resource + FromWorld> DerefMut for Local<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.0
Expand Down

0 comments on commit 73c1773

Please sign in to comment.