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()`*
This commit is contained in:
bird 2023-05-25 12:40:43 +02:00 committed by GitHub
parent a9ca40506e
commit bc9144bcd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
@ -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)`.