From: Ian Jackson Date: Mon, 1 Feb 2021 01:13:18 +0000 (+0000) Subject: visible slotmap keys: handle null specially X-Git-Tag: otter-0.4.0~558 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=12ea498b24616db93d4b2570ac3ac92f645c3ab4;p=otter.git visible slotmap keys: handle null specially Signed-off-by: Ian Jackson --- diff --git a/src/gamestate.rs b/src/gamestate.rs index de075fca..f0c03bec 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -8,7 +8,7 @@ type SE = SVGProcessingError; // ---------- newtypes and type aliases ---------- -visible_slotmap_key!{ PlayerId('#') } +visible_slotmap_key!{ PlayerId(b'#') } slotmap::new_key_type!{ pub struct PieceId; @@ -19,7 +19,7 @@ slotmap::new_key_type!{ #[serde(transparent)] pub struct Generation(pub u64); -visible_slotmap_key!{ VisiblePieceId('.') } +visible_slotmap_key!{ VisiblePieceId(b'.') } #[derive(Clone,Copy,Debug)] #[derive(Serialize,Deserialize)] diff --git a/src/global.rs b/src/global.rs index 2e115715..df1c6b8b 100644 --- a/src/global.rs +++ b/src/global.rs @@ -13,7 +13,7 @@ use std::sync::PoisonError; // ---------- newtypes and type aliases ---------- -visible_slotmap_key!{ ClientId('C') } +visible_slotmap_key!{ ClientId(b'C') } const MAX_CLIENT_INACTIVITY: Duration = Duration::from_secs(200); diff --git a/src/keydata.rs b/src/keydata.rs index 291e91ce..89d5e846 100644 --- a/src/keydata.rs +++ b/src/keydata.rs @@ -24,7 +24,9 @@ macro_rules! display_consequential_impls { pub use crate::display_consequential_impls; // this is madness! #[throws(AE)] -pub fn slotkey_parse(s: &str, sep: char) -> SKD { +pub fn slotkey_parse(s: &str, sep: u8) -> SKD { + if s.as_bytes() == [b'0', sep, b'0', b'0'] { return default() } + let sep: char = sep.into(); let e = || anyhow!("could not deserialise visibile piece id"); let mut i = s.splitn(2, sep).map(|s| s.parse().map_err(|_| e())); let l: u32 = i.next().ok_or_else(e)??; @@ -34,7 +36,9 @@ pub fn slotkey_parse(s: &str, sep: char) -> SKD { } #[throws(fmt::Error)] -pub fn slotkey_write(k: SKD, sep: char, f: &mut fmt::Formatter) { +pub fn slotkey_write(k: SKD, sep: u8, f: &mut fmt::Formatter) { + let sep: char = sep.into(); + if k == default() { write!(f, "0{}00", sep)?; return; } let v = k.as_ffi(); write!(f, "{}{}{}", v & 0xffffffff, sep, v >> 32)? }