From: Ian Jackson Date: Sat, 11 Jul 2020 22:49:45 +0000 (+0100) Subject: global.rs: code motion X-Git-Tag: otter-0.2.0~1380 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=5d90c1f5fc58d01aee105aca2704ce95ca4fa0a2;p=otter.git global.rs: code motion --- diff --git a/src/global.rs b/src/global.rs index 2413302c..c12eccd9 100644 --- a/src/global.rs +++ b/src/global.rs @@ -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 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, } +#[derive(Debug)] +pub struct Client { + pub player : PlayerId, +} + +#[derive(Default)] +struct Global { + // lock hierarchy: this is the innermost lock + players : RwLock>, + clients : RwLock>, + // xxx delete instances at some point! +} + +// ---------- API ---------- + #[derive(Clone,Debug)] pub struct InstanceAccessDetails { pub g : Arc>, @@ -37,22 +48,26 @@ pub struct InstanceAccess<'i, Id> { pub type TokenTable = HashMap>; -#[derive(Default)] -struct Global { - // lock hierarchy: this is the innermost lock - players : RwLock>, - clients : RwLock>, - // xxx delete instances at some point! +pub trait AccessId : Copy + Clone + 'static { + fn global_tokens() -> &'static RwLock>; + const ERROR : OnlineError; } +// ========== implementations ========== + +// ---------- newtypes and type aliases ---------- + +impl Borrow 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>; - const ERROR : OnlineError; -} +// ---------- API ---------- impl AccessId for PlayerId { const ERROR : OnlineError = NoPlayer; @@ -69,6 +84,19 @@ pub fn lookup_token(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(iad : InstanceAccessDetails) -> RawToken { let mut rng = thread_rng(); @@ -80,24 +108,13 @@ pub fn record_token(iad : InstanceAccessDetails) 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(),