chiark / gitweb /
macro
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 4 Jul 2020 02:16:42 +0000 (03:16 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 4 Jul 2020 02:16:42 +0000 (03:16 +0100)
src/error.rs

index b8c77e4b1a0348afe062876ad8b617e2fce37933..53271619f09d9fc94797768d66066fe85802be6e 100644 (file)
@@ -45,29 +45,25 @@ pub trait IdForById {
   const ERROR : Self::Error;
 }
 
-impl<I:IdForById+slotmap::Key, T> ById for DenseSlotMap<I,T> {
-  type Id = I;
-  type Entry = T;
-  type Error = <I as IdForById>::Error;
-  fn byid(&self, t: Self::Id) -> Result<&Self::Entry, Self::Error> {
-    self.get(t).ok_or(<I as IdForById>::ERROR)
-  }
-  fn byid_mut(&mut self, t: Self::Id) -> Result<&mut Self::Entry, Self::Error> {
-    self.get_mut(t).ok_or(<I as IdForById>::ERROR)
-  }
-}
-impl<I:IdForById+slotmap::Key, T> ById for SecondarySlotMap<I,T> {
-  type Id = I;
-  type Entry = T;
-  type Error = <I as IdForById>::Error;
-  fn byid(&self, t: Self::Id) -> Result<&Self::Entry, Self::Error> {
-    self.get(t).ok_or(<I as IdForById>::ERROR)
-  }
-  fn byid_mut(&mut self, t: Self::Id) -> Result<&mut Self::Entry, Self::Error> {
-    self.get_mut(t).ok_or(<I as IdForById>::ERROR)
+macro_rules! some_slotmap {
+  ($slotmap:ident) => {
+    impl<I:IdForById+slotmap::Key, T> ById for $slotmap<I,T> {
+      type Id = I;
+      type Entry = T;
+      type Error = <I as IdForById>::Error;
+      fn byid    (&    self, t: Self::Id) -> Result<&    T, Self::Error> {
+        self.get    (t).ok_or(<I as IdForById>::ERROR)
+      }
+      fn byid_mut(&mut self, t: Self::Id) -> Result<&mut T, Self::Error> {
+        self.get_mut(t).ok_or(<I as IdForById>::ERROR)
+      }
+    }
   }
 }
 
+some_slotmap!{DenseSlotMap}
+some_slotmap!{SecondarySlotMap}
+
 impl<T> IdForById for T where T : AccessId {
   type Error = OE;
   const ERROR : OE = <Self as AccessId>::ERROR;