From: Ian Jackson Date: Thu, 11 Mar 2021 10:06:26 +0000 (+0000) Subject: hidden: Provide OccultIlks X-Git-Tag: otter-0.4.0~197 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=f279387d612e29c1f909c13fdbb82181bbd7b0b6;p=otter.git hidden: Provide OccultIlks Signed-off-by: Ian Jackson --- diff --git a/src/gamestate.rs b/src/gamestate.rs index 14e5e69d..4ad94d4b 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -165,6 +165,7 @@ pub trait PieceTrait: OutlineTrait + Send + Debug + 'static { fn itemname(&self) -> &str; } +#[typetag::serde] pub trait OccultedPieceTrait: OutlineTrait + 'static { fn svg(&self, f: &mut Html, id: VisiblePieceId) -> Result<(),IE>; fn describe_html(&self) -> Result; diff --git a/src/lib.rs b/src/lib.rs index 1be3cd3f..09b594d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ pub mod hidden; pub mod keydata; pub mod mgmtchannel; pub mod nwtemplates; +pub mod occultmap; pub mod pieces; pub mod shapelib; pub mod spec; diff --git a/src/occultmap.rs b/src/occultmap.rs new file mode 100644 index 00000000..aedacb72 --- /dev/null +++ b/src/occultmap.rs @@ -0,0 +1,69 @@ +// Copyright 2020-2021 Ian Jackson and contributors to Otter +// SPDX-License-Identifier: AGPL-3.0-or-later +// There is NO WARRANTY. + +use crate::prelude::*; + +slotmap::new_key_type!{ pub struct OccultIlkId; } + +/// Does *not* `impl Drop`. Don't just drop it. +#[derive(Debug,Serialize,Deserialize)] +#[serde(transparent)] +pub struct OccultIlkOwningId(Id); + +#[derive(Debug,Clone,Eq,PartialEq,Ord,PartialOrd,Hash)] +#[derive(Serialize,Deserialize)] +#[serde(transparent)] +pub struct OccultIlkName(pub String); + +type Id = OccultIlkId; +type OId = OccultIlkOwningId; +type K = OccultIlkName; +type V = Box; +type Refcount = u32; + +#[derive(Debug,Serialize,Deserialize)] +pub struct OccultIlks { + lookup: HashMap, + table: DenseSlotMap, +} + +#[derive(Debug,Serialize,Deserialize)] +pub struct Data { + k: K, // duplicated, ah well + v: V, + refcount: Refcount, +} + +impl OccultIlks { + #[throws(as Option)] + pub fn get>(&self, id: I) -> &V { + &self.table.get(*id.borrow())?.v + } + + pub fn insert(&mut self, k: K, v: V) -> OId { + let OccultIlks { lookup, table } = self; + let id = *lookup + .entry(k) + .or_insert_with_key(|k|{ + let data = Data { + v, + k: k.clone(), + refcount: 0, + }; + table.insert(data) + }); + table[id].refcount += 1; + OccultIlkOwningId(id) + } + + pub fn dispose(&mut self, id: OId) { + let id: Id = id.0; + let data = self.table.remove(id).unwrap(); + self.lookup.remove(&data.k); + } +} + +impl Borrow for OId { + fn borrow(&self) -> &Id { &self.0 } +}