chiark / gitweb /
global.rs: code motion
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 11 Jul 2020 22:49:45 +0000 (23:49 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 11 Jul 2020 22:49:45 +0000 (23:49 +0100)
src/global.rs

index 2413302c3d1aea8cce88c39d64f60692502f2189..c12eccd96318b5919c63a4abbf5f9c058afb8fe7 100644 (file)
@@ -2,19 +2,15 @@
 use crate::imports::*;
 use lazy_static::lazy_static;
 
+// ---------- newtypes and type aliases ----------
+
 visible_slotmap_key!{ ClientId('C') }
 visible_slotmap_key!{ PlayerId('#') }
 
 #[derive(Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
 pub struct RawToken (pub String);
-impl Borrow<str> for RawToken {
-  fn borrow(&self) -> &str { &self.0 }
-}
 
-#[derive(Debug)] 
-pub struct Client {
-  pub player : PlayerId,
-}
+// ---------- data structure ----------
 
 #[derive(Debug)]
 pub struct Instance {
@@ -23,6 +19,21 @@ pub struct Instance {
   pub updates : SecondarySlotMap<PlayerId, PlayerUpdates>,
 }
 
+#[derive(Debug)] 
+pub struct Client {
+  pub player : PlayerId,
+}
+
+#[derive(Default)]
+struct Global {
+  // lock hierarchy: this is the innermost lock
+  players : RwLock<TokenTable<PlayerId>>,
+  clients : RwLock<TokenTable<ClientId>>,
+  // xxx delete instances at some point!
+}
+
+// ---------- API ----------
+
 #[derive(Clone,Debug)]
 pub struct InstanceAccessDetails<Id> {
   pub g : Arc<Mutex<Instance>>,
@@ -37,22 +48,26 @@ pub struct InstanceAccess<'i, Id> {
 
 pub type TokenTable<Id> = HashMap<RawToken, InstanceAccessDetails<Id>>;
 
-#[derive(Default)]
-struct Global {
-  // lock hierarchy: this is the innermost lock
-  players : RwLock<TokenTable<PlayerId>>,
-  clients : RwLock<TokenTable<ClientId>>,
-  // xxx delete instances at some point!
+pub trait AccessId : Copy + Clone + 'static {
+  fn global_tokens() -> &'static RwLock<TokenTable<Self>>;
+  const ERROR : OnlineError;
 }
 
+// ========== implementations ==========
+
+// ---------- newtypes and type aliases ----------
+
+impl Borrow<str> for RawToken {
+  fn borrow(&self) -> &str { &self.0 }
+}
+
+// ---------- data structure ----------
+
 lazy_static! {
   static ref GLOBAL : Global = Default::default();
 }
 
-pub trait AccessId : Copy + Clone + 'static {
-  fn global_tokens() -> &'static RwLock<TokenTable<Self>>;
-  const ERROR : OnlineError;
-}
+// ---------- API ----------
 
 impl AccessId for PlayerId {
   const ERROR : OnlineError = NoPlayer;
@@ -69,6 +84,19 @@ pub fn lookup_token<Id : AccessId>(s : &str)
     .ok_or(Id::ERROR)
 }
 
+impl<'r, Id> FromParam<'r> for InstanceAccess<'r, Id>
+  where Id : AccessId
+{
+  type Error = OE;
+  #[throws(OE)]
+  fn from_param(param: &'r RawStr) -> Self {
+    let g = Id::global_tokens().read().unwrap();
+    let token = param.as_str();
+    let i = g.get(token).ok_or(Id::ERROR)?;
+    InstanceAccess { raw_token : token, i : i.clone() }
+  }
+}
+
 pub fn record_token<Id : AccessId>(iad : InstanceAccessDetails<Id>)
                                    -> RawToken {
   let mut rng = thread_rng();
@@ -80,24 +108,13 @@ pub fn record_token<Id : AccessId>(iad : InstanceAccessDetails<Id>)
   token
 }
 
+// ========== ad-hoc and temporary ==========
+
 const XXX_PLAYERS_TOKENS : &[(&str, &str)] = &[
   ("kmqAKPwK4TfReFjMor8MJhdRPBcwIBpe", "alice"),
   ("ccg9kzoTh758QrVE1xMY7BQWB36dNJTx", "bob"),
 ];
 
-impl<'r, Id> FromParam<'r> for InstanceAccess<'r, Id>
-  where Id : AccessId
-{
-  type Error = OE;
-  #[throws(OE)]
-  fn from_param(param: &'r RawStr) -> Self {
-    let g = Id::global_tokens().read().unwrap();
-    let token = param.as_str();
-    let i = g.get(token).ok_or(Id::ERROR)?;
-    InstanceAccess { raw_token : token, i : i.clone() }
-  }
-}
-
 pub fn xxx_global_setup() {
   let gi = Instance {
     gs : xxx_gamestate_init(),