chiark / gitweb /
ById
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 4 Jul 2020 01:51:04 +0000 (02:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 4 Jul 2020 01:51:04 +0000 (02:51 +0100)
src/error.rs
src/global.rs
src/sse.rs

index 5e1f075d9f307c33a4303c25a5c92ed2e44609b1..75978bda137a9507e024456506a1c5cd40fc1356 100644 (file)
@@ -20,3 +20,26 @@ use OnlineError::*;
 impl<X> From<PoisonError<X>> for OnlineError {
   fn from(_: PoisonError<X>) -> OnlineError { GameCorrupted }
 }
+
+
+pub trait ById {
+  type Id;
+  type Entry;
+  #[throws(OE)]
+  fn byid(&self, t: Self::Id) -> &Self::Entry;
+}
+
+impl<I:AccessId+slotmap::Key, T> ById for DenseSlotMap<I,T> {
+  type Id = I;
+  type Entry = T;
+  fn byid(&self, t: Self::Id) -> Result<&Self::Entry, OE> {
+    self.get(t).ok_or(<I as AccessId>::ERROR)
+  }
+}
+impl<I:AccessId+slotmap::Key, T> ById for SecondarySlotMap<I,T> {
+  type Id = I;
+  type Entry = T;
+  fn byid(&self, t: Self::Id) -> Result<&Self::Entry, OE> {
+    self.get(t).ok_or(<I as AccessId>::ERROR)
+  }
+}
index a932ed9a3521288005910dc1283c6004d96f5cd7..b686cfd48e68538789c62f5ae72e9a8509535876 100644 (file)
@@ -74,12 +74,12 @@ pub trait AccessId : Copy + Clone + 'static {
 }
 
 impl AccessId for PlayerId {
-  fn global_tokens() -> &'static RwLock<TokenTable<Self>> { &GLOBAL.players }
   const ERROR : OnlineError = NoPlayer;
+  fn global_tokens() -> &'static RwLock<TokenTable<Self>> { &GLOBAL.players }
 }
 impl AccessId for ClientId {
-  fn global_tokens() -> &'static RwLock<TokenTable<Self>> { &GLOBAL.clients }
   const ERROR : OnlineError = NoClient;
+  fn global_tokens() -> &'static RwLock<TokenTable<Self>> { &GLOBAL.clients }
 }
 
 pub fn lookup_token<Id : AccessId>(s : &str)
index 0ac8fc1f283983515e3e5a127ef980967271a66a..228157bf73676c56c2f7c068ef5f5371be8a84e0 100644 (file)
@@ -177,11 +177,11 @@ pub fn content(iad : InstanceAccessDetails<ClientId>, 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) {