Skip to content

Commit bc9144b

Browse files
authored
implement Deref for State<S> (#8668)
# 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()`*
1 parent a9ca405 commit bc9144b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::Debug;
22
use std::hash::Hash;
33
use std::mem;
4+
use std::ops::Deref;
45

56
use crate as bevy_ecs;
67
use crate::change_detection::DetectChangesMut;
@@ -95,6 +96,14 @@ impl<S: States> PartialEq<S> for State<S> {
9596
}
9697
}
9798

99+
impl<S: States> Deref for State<S> {
100+
type Target = S;
101+
102+
fn deref(&self) -> &Self::Target {
103+
self.get()
104+
}
105+
}
106+
98107
/// The next state of [`State<S>`].
99108
///
100109
/// To queue a transition, just set the contained value to `Some(next_state)`.

0 commit comments

Comments
 (0)