Skip to content

Commit

Permalink
simplify bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Dec 12, 2020
1 parent 88578ef commit f41745f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
10 changes: 5 additions & 5 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any::Any, hash::Hash};
use std::any::Any;

use crate::{
app::{App, AppExit},
Expand Down Expand Up @@ -211,7 +211,7 @@ impl AppBuilder {
format!("state({})", std::any::type_name::<T>())
}

pub fn add_state<T: Clone + Eq + Hash + Resource>(&mut self, initial: T) -> &mut Self {
pub fn add_state<T: Clone + Resource>(&mut self, initial: T) -> &mut Self {
self.add_resource(State::new(initial));
self.app.schedule.add_stage_after(
stage::UPDATE,
Expand All @@ -221,7 +221,7 @@ impl AppBuilder {
self
}

pub fn on_state_enter<T: Clone + Eq + Hash + Resource, Params, S: IntoStage<Params>>(
pub fn on_state_enter<T: Clone + Resource, Params, S: IntoStage<Params>>(
&mut self,
value: T,
stage: S,
Expand All @@ -232,7 +232,7 @@ impl AppBuilder {
)
}

pub fn on_state_update<T: Clone + Eq + Hash + Resource, Params, S: IntoStage<Params>>(
pub fn on_state_update<T: Clone + Resource, Params, S: IntoStage<Params>>(
&mut self,
value: T,
stage: S,
Expand All @@ -243,7 +243,7 @@ impl AppBuilder {
)
}

pub fn on_state_exit<T: Clone + Eq + Hash + Resource, Params, S: IntoStage<Params>>(
pub fn on_state_exit<T: Clone + Resource, Params, S: IntoStage<Params>>(
&mut self,
value: T,
stage: S,
Expand Down
40 changes: 22 additions & 18 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{IntoStage, Resource, Resources, Stage, World};
use bevy_utils::HashMap;
use std::{hash::Hash, mem::Discriminant, ops::Deref};
use std::{mem::Discriminant, ops::Deref};
use thiserror::Error;

#[derive(Default)]
Expand All @@ -22,7 +22,8 @@ impl<T> Default for StateStage<T> {
}
}

impl<T: Eq + Hash> StateStage<T> {
#[allow(clippy::mem_discriminant_non_enum)]
impl<T> StateStage<T> {
pub fn with_on_state_enter<Params, S: IntoStage<Params>>(mut self, state: T, stage: S) -> Self {
self.on_state_enter(state, stage);
self
Expand Down Expand Up @@ -78,7 +79,8 @@ impl<T: Eq + Hash> StateStage<T> {
}
}

impl<T: Resource + Clone + Eq + Hash> Stage for StateStage<T> {
#[allow(clippy::mem_discriminant_non_enum)]
impl<T: Resource + Clone> Stage for StateStage<T> {
fn run(&mut self, world: &mut World, resources: &mut Resources) {
loop {
let (next_stage, current_stage) = {
Expand Down Expand Up @@ -114,15 +116,13 @@ impl<T: Resource + Clone + Eq + Hash> Stage for StateStage<T> {
{
enter_next.run(world, resources);
}
} else {
if let Some(update_current) = self
.stages
.get_mut(&current_stage)
.and_then(|stage| stage.update.as_mut())
{
update_current.run(world, resources);
break;
}
} else if let Some(update_current) = self
.stages
.get_mut(&current_stage)
.and_then(|stage| stage.update.as_mut())
{
update_current.run(world, resources);
break;
}
}
}
Expand All @@ -136,13 +136,14 @@ pub enum StateError {
}

#[derive(Debug)]
pub struct State<T: Clone + Hash + Eq + PartialEq> {
pub struct State<T: Clone> {
previous: Option<T>,
current: T,
next: Option<T>,
}

impl<T: Clone + Hash + Eq + PartialEq> State<T> {
#[allow(clippy::mem_discriminant_non_enum)]
impl<T: Clone> State<T> {
pub fn new(state: T) -> Self {
Self {
current: state.clone(),
Expand All @@ -166,7 +167,7 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {

/// Queue a state change. This will fail if there is already a state in the queue, or if the given `state` matches the current state
pub fn set_next(&mut self, state: T) -> Result<(), StateError> {
if self.current == state {
if std::mem::discriminant(&self.current) == std::mem::discriminant(&state) {
return Err(StateError::AlreadyInState);
}

Expand All @@ -180,7 +181,7 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {

/// Same as [Self::queue], but there is already a next state, it will be overwritten instead of failing
pub fn overwrite_next(&mut self, state: T) -> Result<(), StateError> {
if self.current == state {
if std::mem::discriminant(&self.current) == std::mem::discriminant(&state) {
return Err(StateError::AlreadyInState);
}

Expand All @@ -190,12 +191,15 @@ impl<T: Clone + Hash + Eq + PartialEq> State<T> {

fn apply_next(&mut self) {
if let Some(next) = self.next.take() {
self.previous = Some(std::mem::replace(&mut self.current, next))
let previous = std::mem::replace(&mut self.current, next);
if std::mem::discriminant(&previous) != std::mem::discriminant(&self.current) {
self.previous = Some(previous)
}
}
}
}

impl<T: Clone + Hash + Eq + PartialEq> Deref for State<T> {
impl<T: Clone> Deref for State<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
Expand Down
12 changes: 6 additions & 6 deletions examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ fn main() {
.init_resource::<RpgSpriteHandles>()
.add_plugins(DefaultPlugins)
.add_state(AppState::Setup)
.state_enter(AppState::Setup, load_textures)
.state_update(AppState::Setup, check_textures)
.state_enter(AppState::Finshed, setup)
.on_state_enter(AppState::Setup, load_textures)
.on_state_update(AppState::Setup, check_textures)
.on_state_enter(AppState::Finshed, setup)
.run();
}

#[derive(Clone, Hash, Eq, PartialEq)]
#[derive(Clone)]
enum AppState {
Setup,
Finshed,
Expand All @@ -28,14 +28,14 @@ fn load_textures(mut rpg_sprite_handles: ResMut<RpgSpriteHandles>, asset_server:
}

fn check_textures(
state: Res<State<AppState>>,
mut state: ResMut<State<AppState>>,
rpg_sprite_handles: ResMut<RpgSpriteHandles>,
asset_server: Res<AssetServer>,
) {
if let LoadState::Loaded =
asset_server.get_group_load_state(rpg_sprite_handles.handles.iter().map(|handle| handle.id))
{
state.queue(AppState::Finshed);
state.set_next(AppState::Finshed).unwrap();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
.run();
}

#[derive(Clone, Hash, Eq, PartialEq)]
#[derive(Clone)]
enum AppState {
Menu,
InGame,
Expand Down

0 comments on commit f41745f

Please sign in to comment.