Something down in the food chain already uses bitflags, so might as well switch to them
authoralfadur
Mon, 16 Jul 2018 22:59:58 +0300
changeset 13527 282e5e54386f
parent 13526 ba5211dddb21
child 13528 8c5dd562c9f7
Something down in the food chain already uses bitflags, so might as well switch to them
gameServer2/Cargo.toml
gameServer2/src/main.rs
gameServer2/src/server/client.rs
--- a/gameServer2/Cargo.toml	Mon Jul 16 22:32:04 2018 +0300
+++ b/gameServer2/Cargo.toml	Mon Jul 16 22:59:58 2018 +0300
@@ -13,3 +13,4 @@
 log = "0.4"
 proptest = "0.8"
 base64 = "0.9"
+bitflags = "1.0"
--- a/gameServer2/src/main.rs	Mon Jul 16 22:32:04 2018 +0300
+++ b/gameServer2/src/main.rs	Mon Jul 16 22:59:58 2018 +0300
@@ -12,6 +12,7 @@
 extern crate log;
 extern crate env_logger;
 #[macro_use] extern crate proptest;
+#[macro_use] extern crate bitflags;
 
 //use std::io::*;
 //use rand::Rng;
--- a/gameServer2/src/server/client.rs	Mon Jul 16 22:32:04 2018 +0300
+++ b/gameServer2/src/server/client.rs	Mon Jul 16 22:59:58 2018 +0300
@@ -1,17 +1,21 @@
 use super::coretypes::ClientId;
 
-const IS_ADMIN: u8 = 0b0000_0001;
-const IS_MASTER: u8 = 0b0000_0010;
-const IS_READY: u8 = 0b0000_0100;
-const IS_IN_GAME: u8 = 0b0000_1000;
-const IS_JOINED_MID_GAME: u8 = 0b0001_0000;
+bitflags!{
+    pub struct ClientFlags: u8 {
+        const IS_ADMIN = 0b0000_0001;
+        const IS_MASTER = 0b0000_0010;
+        const IS_READY = 0b0000_0100;
+        const IS_IN_GAME = 0b0000_1000;
+        const IS_JOINED_MID_GAME = 0b0001_0000;
+    }
+}
 
 pub struct HWClient {
     pub id: ClientId,
     pub room_id: Option<usize>,
     pub nick: String,
     pub protocol_number: u16,
-    flags: u8,
+    pub flags: ClientFlags,
     pub teams_in_game: u8,
     pub team_indices: Vec<u8>,
     pub clan: Option<u8>
@@ -24,26 +28,30 @@
             room_id: None,
             nick: String::new(),
             protocol_number: 0,
-            flags: 0,
+            flags: ClientFlags::empty(),
             teams_in_game: 0,
             team_indices: Vec::new(),
             clan: None,
         }
     }
 
-    fn set(&mut self, mask: u8, value: bool) {
-        if value { self.flags |= mask } else { self.flags &= !mask }
+    fn contains(& self, mask: ClientFlags) -> bool {
+        self.flags.contains(mask)
+    }
+
+    fn set(&mut self, mask: ClientFlags, value: bool) {
+        self.flags.set(mask, value);
     }
 
-    pub fn is_admin(&self)-> bool { self.flags & IS_ADMIN != 0 }
-    pub fn is_master(&self)-> bool { self.flags & IS_MASTER != 0 }
-    pub fn is_ready(&self)-> bool { self.flags & IS_READY != 0 }
-    pub fn is_in_game(&self)-> bool { self.flags & IS_IN_GAME != 0 }
-    pub fn is_joined_mid_game(&self)-> bool { self.flags & IS_JOINED_MID_GAME != 0 }
+    pub fn is_admin(&self)-> bool { self.contains(ClientFlags::IS_ADMIN) }
+    pub fn is_master(&self)-> bool { self.contains(ClientFlags::IS_MASTER) }
+    pub fn is_ready(&self)-> bool { self.contains(ClientFlags::IS_READY) }
+    pub fn is_in_game(&self)-> bool { self.contains(ClientFlags::IS_IN_GAME) }
+    pub fn is_joined_mid_game(&self)-> bool { self.contains(ClientFlags::IS_JOINED_MID_GAME) }
 
-    pub fn set_is_admin(&mut self, value: bool) { self.set(IS_ADMIN, value) }
-    pub fn set_is_master(&mut self, value: bool) { self.set(IS_MASTER, value) }
-    pub fn set_is_ready(&mut self, value: bool) { self.set(IS_READY, value) }
-    pub fn set_is_in_game(&mut self, value: bool) { self.set(IS_IN_GAME, value) }
-    pub fn set_is_joined_mid_game(&mut self, value: bool) { self.set(IS_JOINED_MID_GAME, value) }
+    pub fn set_is_admin(&mut self, value: bool) { self.set(ClientFlags::IS_ADMIN, value) }
+    pub fn set_is_master(&mut self, value: bool) { self.set(ClientFlags::IS_MASTER, value) }
+    pub fn set_is_ready(&mut self, value: bool) { self.set(ClientFlags::IS_READY, value) }
+    pub fn set_is_in_game(&mut self, value: bool) { self.set(ClientFlags::IS_IN_GAME, value) }
+    pub fn set_is_joined_mid_game(&mut self, value: bool) { self.set(ClientFlags::IS_JOINED_MID_GAME, value) }
 }
\ No newline at end of file