chiark / gitweb /
visible_slotmap_key wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Jun 2020 12:45:34 +0000 (13:45 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Jun 2020 12:45:34 +0000 (13:45 +0100)
src/gamestate.rs
src/keydata.rs

index 6aa8d438428c02d04d7fd8781db1cd30a9a7208d..244edf7bd9ef4c9b5335e30f6c3dd858424d1dcc 100644 (file)
@@ -5,10 +5,8 @@ slotmap::new_key_type!{
   pub struct PieceId;
 }
 
-#[derive(Copy,Clone,Serialize,Deserialize)]
-#[serde(into="String")]
-#[serde(try_from="&str")]
-pub struct VisiblePieceId(pub slotmap::KeyData);
+visible_slotmap_key!{VisiblePieceId}
+display_consequential_impls!{VisiblePieceId}
 
 pub fn make_pieceid_visible(p : PieceId) -> VisiblePieceId {
   // xxx need to do censorship mapping here
@@ -24,20 +22,6 @@ pub struct PieceRenderInstructions {
 
 pub type VisiblePieceIdSvgIds = &'static [&'static str];
 
-impl Display for VisiblePieceId {
-  #[throws(fmt::Error)]
-  fn fmt(&self, f : &mut fmt::Formatter) { slotkey_write(self.0,'.',f)? }
-}
-display_consequential_impls!{VisiblePieceId}
-
-impl TryFrom<&str> for VisiblePieceId {
-  type Error = AE;
-  #[throws(AE)]
-  fn try_from(s : &str) -> VisiblePieceId {
-    VisiblePieceId(slotkey_parse(s,'.')?)
-  }
-}
-
 impl PieceRenderInstructions {
   pub fn id_piece(&self) -> String { format!("piece{}", self.id) }
   pub fn id_select(&self) -> String { format!("select{}", self.id) }
index 58f25525a2c50d3f6bfeeb680f40c0e71375519f..60e2e2a7ec057afc83a562eeaf110a44d7935004 100644 (file)
@@ -16,7 +16,6 @@ macro_rules! display_consequential_impls {
     }
   }
 }
-
 pub use crate::display_consequential_impls; // this is madness!
 
 #[throws(AE)]
@@ -34,3 +33,27 @@ pub fn slotkey_write(k : SKD, sep : char, f : &mut fmt::Formatter) {
   let v = k.as_ffi();
   write!(f, "{}{}{}", v >> 32, sep, v & 0xffffffff)?
 }
+
+#[macro_export]
+macro_rules! visible_slotmap_key {
+  ( $x:ident ) => {
+    #[derive(Copy,Clone,Serialize,Deserialize)]
+    #[serde(into="String")]
+    #[serde(try_from="&str")]
+    pub struct $x(pub slotmap::KeyData);
+
+    impl Display for $x {
+      #[throws(fmt::Error)]
+      fn fmt(&self, f : &mut fmt::Formatter) { slotkey_write(self.0,'.',f)? }
+    }
+
+    impl TryFrom<&str> for VisiblePieceId {
+      type Error = AE;
+      #[throws(AE)]
+      fn try_from(s : &str) -> VisiblePieceId {
+        VisiblePieceId(slotkey_parse(s,'.')?)
+      }
+    }
+  }
+}
+pub use crate::visible_slotmap_key; // this is madness!