Skip to content

Commit

Permalink
implement Deref for State<S> (#8668)
Browse files Browse the repository at this point in the history
# Objective

- Allow for directly call methods on states without first calling
`state.get().my_method()`

## Solution

- Implement `Deref` for `State<S>` with `Target = S`
---
*I did not implement `DerefMut` because states hold no data and should
only be changed via `NextState::set()`*
  • Loading branch information
atornity authored May 25, 2023
1 parent a9ca405 commit bc9144b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Debug;
use std::hash::Hash;
use std::mem;
use std::ops::Deref;

use crate as bevy_ecs;
use crate::change_detection::DetectChangesMut;
Expand Down Expand Up @@ -95,6 +96,14 @@ impl<S: States> PartialEq<S> for State<S> {
}
}

impl<S: States> Deref for State<S> {
type Target = S;

fn deref(&self) -> &Self::Target {
self.get()
}
}

/// The next state of [`State<S>`].
///
/// To queue a transition, just set the contained value to `Some(next_state)`.
Expand Down

0 comments on commit bc9144b

Please sign in to comment.