Skip to content
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

Make state private and only accessible through getter for State resource #8009

Merged
merged 6 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Getter instead of deref
  • Loading branch information
Aceeri committed Mar 23, 2023
commit 42d8e34fbec75702989f0f56b29157d6930e9a53
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/schedule/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ pub mod common_conditions {
/// assert_eq!(world.resource::<Counter>().0, 0);
/// ```
pub fn in_state<S: States>(state: S) -> impl FnMut(Res<State<S>>) -> bool + Clone {
move |current_state: Res<State<S>>| current_state.0 == state
move |current_state: Res<State<S>>| *current_state.get() == state
Copy link
Member

@cart cart Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thought / proposal:

  1. Only implement .get() for states that implement copy/clone. Return by value. Then 9/10 times comparisons can be current_state.get() == state
  2. Also implement Deref::deref(), which returns the reference. Use double star (or manual deref() calls) in these cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

States has a Clone bound already, so I think it should just return by value 100% of the time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I think implicitly cloning for getting the state would be great, I'd be more comfortable with impling on just Copy + Clone ones though.

If we want to simplify this a bit we could just impl PartialEq/Eq on State<S> itself and it could just be
current_state == state

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the impl on State quite a bit actually: that really feels like the idiomatic approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup way better!

}

/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
Expand Down Expand Up @@ -751,7 +751,7 @@ pub mod common_conditions {
state: S,
) -> impl FnMut(Option<Res<State<S>>>) -> bool + Clone {
move |current_state: Option<Res<State<S>>>| match current_state {
Some(current_state) => current_state.0 == state,
Some(current_state) => *current_state.get() == state,
None => false,
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ pub struct OnUpdate<S: States>(pub S);
#[derive(Resource, Default, Debug)]
pub struct State<S: States>(S);

impl<S: States> std::ops::Deref for State<S> {
type Target = S;
fn deref(&self) -> &Self::Target {
impl<S: States> State<S> {
/// Get the current state.
pub fn get(&self) -> &S {
&self.0
}
}
Expand Down