From a84b382d5efa02822cbdec5c344422ad9a10f7c0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 4 Jul 2020 03:16:42 +0100 Subject: [PATCH] macro --- src/error.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/error.rs b/src/error.rs index b8c77e4b..53271619 100644 --- a/src/error.rs +++ b/src/error.rs @@ -45,29 +45,25 @@ pub trait IdForById { const ERROR : Self::Error; } -impl ById for DenseSlotMap { - type Id = I; - type Entry = T; - type Error = ::Error; - fn byid(&self, t: Self::Id) -> Result<&Self::Entry, Self::Error> { - self.get(t).ok_or(::ERROR) - } - fn byid_mut(&mut self, t: Self::Id) -> Result<&mut Self::Entry, Self::Error> { - self.get_mut(t).ok_or(::ERROR) - } -} -impl ById for SecondarySlotMap { - type Id = I; - type Entry = T; - type Error = ::Error; - fn byid(&self, t: Self::Id) -> Result<&Self::Entry, Self::Error> { - self.get(t).ok_or(::ERROR) - } - fn byid_mut(&mut self, t: Self::Id) -> Result<&mut Self::Entry, Self::Error> { - self.get_mut(t).ok_or(::ERROR) +macro_rules! some_slotmap { + ($slotmap:ident) => { + impl ById for $slotmap { + type Id = I; + type Entry = T; + type Error = ::Error; + fn byid (& self, t: Self::Id) -> Result<& T, Self::Error> { + self.get (t).ok_or(::ERROR) + } + fn byid_mut(&mut self, t: Self::Id) -> Result<&mut T, Self::Error> { + self.get_mut(t).ok_or(::ERROR) + } + } } } +some_slotmap!{DenseSlotMap} +some_slotmap!{SecondarySlotMap} + impl IdForById for T where T : AccessId { type Error = OE; const ERROR : OE = ::ERROR; -- 2.30.2