From 9a832dd56b53acd5ddb5bd1a13eaadeec0b1cbaa Mon Sep 17 00:00:00 2001 From: timokoesters Date: Fri, 30 Aug 2019 20:40:22 +0200 Subject: [PATCH] Move std::mem::discriminant into new method --- client/src/lib.rs | 7 +------ common/src/comp/character_state.rs | 14 ++++++++++++++ server/src/lib.rs | 8 +------- voxygen/src/scene/figure.rs | 17 ++++------------- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index c0b5a4c91d..c74050b4b6 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -290,12 +290,7 @@ impl Client { { if last_character_states .get(entity) - .map(|&l| { - std::mem::discriminant(&l.0.movement) - != std::mem::discriminant(&client_character_state.movement) - || std::mem::discriminant(&l.0.action) - != std::mem::discriminant(&client_character_state.action) - }) + .map(|&l| !client_character_state.is_same_state(&l.0)) .unwrap_or(true) { let _ = last_character_states diff --git a/common/src/comp/character_state.rs b/common/src/comp/character_state.rs index c9d5c55b17..fb34afefd5 100644 --- a/common/src/comp/character_state.rs +++ b/common/src/comp/character_state.rs @@ -63,6 +63,20 @@ pub struct CharacterState { pub action: ActionState, } +impl CharacterState { + pub fn is_same_movement(&self, other: &Self) -> bool { + // Check if enum item is the same without looking at the inner data + std::mem::discriminant(&self.movement) == std::mem::discriminant(&other.movement) + } + pub fn is_same_action(&self, other: &Self) -> bool { + // Check if enum item is the same without looking at the inner data + std::mem::discriminant(&self.action) == std::mem::discriminant(&other.action) + } + pub fn is_same_state(&self, other: &Self) -> bool { + self.is_same_movement(other) && self.is_same_action(other) + } +} + impl Default for CharacterState { fn default() -> Self { Self { diff --git a/server/src/lib.rs b/server/src/lib.rs index df0be23575..c1ebb8d86b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1161,13 +1161,7 @@ impl Server { { if last_character_state .get(entity) - .map(|&l| { - // Check if enum item is the same without looking at the inner data - std::mem::discriminant(&l.0.movement) - != std::mem::discriminant(&client_character_state.movement) - || std::mem::discriminant(&l.0.action) - != std::mem::discriminant(&client_character_state.action) - }) + .map(|&l| !client_character_state.is_same_state(&l.0)) .unwrap_or(true) { let _ = diff --git a/voxygen/src/scene/figure.rs b/voxygen/src/scene/figure.rs index bbb91f6dd2..f6993f8bbe 100644 --- a/voxygen/src/scene/figure.rs +++ b/voxygen/src/scene/figure.rs @@ -691,15 +691,10 @@ impl FigureMgr { _ => continue, }; - if std::mem::discriminant(&last_character.0.movement) - != std::mem::discriminant(&character.movement) - { + if !character.is_same_movement(&last_character.0) { state.last_movement_change = Instant::now(); } - - if std::mem::discriminant(&last_character.0.action) - != std::mem::discriminant(&character.action) - { + if !character.is_same_action(&last_character.0) { state.last_action_change = Instant::now(); } @@ -790,9 +785,7 @@ impl FigureMgr { _ => continue, }; - if std::mem::discriminant(&last_character.0.movement) - != std::mem::discriminant(&character.movement) - { + if !character.is_same_movement(&last_character.0) { state.last_movement_change = Instant::now(); } @@ -839,9 +832,7 @@ impl FigureMgr { _ => continue, }; - if std::mem::discriminant(&last_character.0.movement) - != std::mem::discriminant(&character.movement) - { + if !character.is_same_movement(&last_character.0) { state.last_movement_change = Instant::now(); }