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

index 75c8b9ffe6903e3ebc166a28010ffc8349b28d5b..54a0aa9a62ceb1a98ebe5fa15d4ae0101319b44f 100644 (file)
@@ -32,25 +32,35 @@ pub trait ById {
   fn byid_mut(&mut self, t: Self::Id) -> &mut Self::Entry;
 }
 
-impl<I:AccessId+slotmap::Key, T> ById for DenseSlotMap<I,T> {
+pub trait IdForById {
+  type Error;
+  const ERROR : Self::Error;
+}
+
+impl<I:IdForById+slotmap::Key, T> ById for DenseSlotMap<I,T> {
   type Id = I;
   type Entry = T;
-  type Error = OE;
-  fn byid(&self, t: Self::Id) -> Result<&Self::Entry, OE> {
-    self.get(t).ok_or(<I as AccessId>::ERROR)
+  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, OE> {
-    self.get_mut(t).ok_or(<I as AccessId>::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:AccessId+slotmap::Key, T> ById for SecondarySlotMap<I,T> {
+impl<I:IdForById+slotmap::Key, T> ById for SecondarySlotMap<I,T> {
   type Id = I;
   type Entry = T;
-  type Error = OE;
-  fn byid(&self, t: Self::Id) -> Result<&Self::Entry, OE> {
-    self.get(t).ok_or(<I as AccessId>::ERROR)
+  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, OE> {
-    self.get_mut(t).ok_or(<I as AccessId>::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<T> IdForById for T where T : AccessId {
+  type Error = OE;
+  const ERROR : OE = <Self as AccessId>::ERROR;
+}