From: Ian Jackson Date: Sat, 4 Jul 2020 01:51:04 +0000 (+0100) Subject: ById X-Git-Tag: otter-0.2.0~1484 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=586d7ab35b1642acb55cb7581c717e092de2f1d3;p=otter.git ById --- diff --git a/src/error.rs b/src/error.rs index 5e1f075d..75978bda 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,3 +20,26 @@ use OnlineError::*; impl From> for OnlineError { fn from(_: PoisonError) -> OnlineError { GameCorrupted } } + + +pub trait ById { + type Id; + type Entry; + #[throws(OE)] + fn byid(&self, t: Self::Id) -> &Self::Entry; +} + +impl ById for DenseSlotMap { + type Id = I; + type Entry = T; + fn byid(&self, t: Self::Id) -> Result<&Self::Entry, OE> { + self.get(t).ok_or(::ERROR) + } +} +impl ById for SecondarySlotMap { + type Id = I; + type Entry = T; + fn byid(&self, t: Self::Id) -> Result<&Self::Entry, OE> { + self.get(t).ok_or(::ERROR) + } +} diff --git a/src/global.rs b/src/global.rs index a932ed9a..b686cfd4 100644 --- a/src/global.rs +++ b/src/global.rs @@ -74,12 +74,12 @@ pub trait AccessId : Copy + Clone + 'static { } impl AccessId for PlayerId { - fn global_tokens() -> &'static RwLock> { &GLOBAL.players } const ERROR : OnlineError = NoPlayer; + fn global_tokens() -> &'static RwLock> { &GLOBAL.players } } impl AccessId for ClientId { - fn global_tokens() -> &'static RwLock> { &GLOBAL.clients } const ERROR : OnlineError = NoClient; + fn global_tokens() -> &'static RwLock> { &GLOBAL.clients } } pub fn lookup_token(s : &str) diff --git a/src/sse.rs b/src/sse.rs index 0ac8fc1f..228157bf 100644 --- a/src/sse.rs +++ b/src/sse.rs @@ -177,11 +177,11 @@ pub fn content(iad : InstanceAccessDetails, gen: Generation) let content = { let mut ig = iad.g.lock()?; let _g = &mut ig.gs; - let cl = ig.clients.get(client).ok_or(NoClient)?; + let cl = ig.clients.byid(client)?; let player = cl.player; let ami = iad.g.clone(); - let log = &ig.updates.get(player).ok_or(NoPlayer)?.log; + let log = &ig.updates.byid(player)?.log; let to_send = match log.into_iter().rev() .find(|(_,update)| update.gen < gen) {