From: Ian Jackson Date: Fri, 17 Jul 2020 23:09:41 +0000 (+0100) Subject: private_y X-Git-Tag: otter-0.2.0~1315 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=990a450c5790fd14df5bd3c91195990235682571;p=otter.git private_y --- diff --git a/src/global.rs b/src/global.rs index 2e751434..c23537c1 100644 --- a/src/global.rs +++ b/src/global.rs @@ -62,11 +62,17 @@ pub struct InstanceAccess<'i, Id> { pub type TokenTable = HashMap>; pub trait AccessId : Copy + Clone + 'static { - fn global_tokens() -> &'static RwLock>; - fn tokens_registry(ig: &mut Instance) -> &mut TokenRegistry; + fn global_tokens(_:PrivateCaller) -> &'static RwLock>; + fn tokens_registry(ig: &mut Instance, _:PrivateCaller) + -> &mut TokenRegistry; const ERROR : OnlineError; } +pub struct PrivateCaller(()); +// outsiders cannot construct this +// workaround for inability to have private trait methods +const PRIVATE_Y : PrivateCaller = PrivateCaller(()); + // ========== implementations ========== // ---------- newtypes and type aliases ---------- @@ -131,8 +137,8 @@ impl InstanceGuard<'_> { token: RawToken, iad: InstanceAccessDetails ) { - Id::tokens_registry(&mut *self.ig).tr.insert(token.clone()); - Id::global_tokens().write().unwrap().insert(token, iad); + Id::tokens_registry(&mut *self.ig, PRIVATE_Y).tr.insert(token.clone()); + Id::global_tokens(PRIVATE_Y).write().unwrap().insert(token, iad); } #[throws(OE)] @@ -145,22 +151,28 @@ impl InstanceGuard<'_> { impl AccessId for PlayerId { const ERROR : OnlineError = NoPlayer; - fn global_tokens() -> &'static RwLock> { &GLOBAL.players } - fn tokens_registry(ig: &mut Instance) -> &mut TokenRegistry { + fn global_tokens(_: PrivateCaller) -> &'static RwLock> { + &GLOBAL.players + } + fn tokens_registry(ig: &mut Instance, _:PrivateCaller) + -> &mut TokenRegistry { &mut ig.tokens_players } } impl AccessId for ClientId { const ERROR : OnlineError = NoClient; - fn global_tokens() -> &'static RwLock> { &GLOBAL.clients } - fn tokens_registry(ig: &mut Instance) -> &mut TokenRegistry { + fn global_tokens(_: PrivateCaller) -> &'static RwLock> { + &GLOBAL.clients + } + fn tokens_registry(ig: &mut Instance, _:PrivateCaller) + -> &mut TokenRegistry { &mut ig.tokens_clients } } pub fn lookup_token(s : &str) -> Result, OE> { - Id::global_tokens().read().unwrap().get(s).cloned() + Id::global_tokens(PRIVATE_Y).read().unwrap().get(s).cloned() .ok_or(Id::ERROR) } @@ -170,7 +182,7 @@ impl<'r, Id> FromParam<'r> for InstanceAccess<'r, Id> type Error = OE; #[throws(OE)] fn from_param(param: &'r RawStr) -> Self { - let g = Id::global_tokens().read().unwrap(); + let g = Id::global_tokens(PRIVATE_Y).read().unwrap(); let token = param.as_str(); let i = g.get(token).ok_or(Id::ERROR)?; InstanceAccess { raw_token : token, i : i.clone() }