Diffstat (limited to 'src/protocol.rs')
| -rw-r--r-- | src/protocol.rs | 67 |
1 files changed, 0 insertions, 67 deletions
diff --git a/src/protocol.rs b/src/protocol.rs index f411556..ba5826d 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -35,73 +35,6 @@ use crate::any::{IndirectLtAny, LtAny, LtTypeId, Mut, Ref, TypeNameable}; -pub trait Implementer<'ctx> { - fn interface(&self, id: LtTypeId<'ctx>) -> Option<IndirectLtAny<'_, 'ctx, Ref>>; -} - -pub trait ImplementerMut<'ctx> { - fn interface_mut<'a>(&'a mut self, id: LtTypeId<'ctx>) -> Option<IndirectLtAny<'a, 'ctx, Mut>> where 'ctx: 'a; -} - -/// Extension trait for getting the implementation of a protocol. -pub trait ImplementerMutExt<'ctx>: ImplementerMut<'ctx> { - /// Get an implementation given a protocol type. - /// - /// This wraps [`Implementer::interface`] and [`AnyImpl::downcast`]. - /// If [`Implementer::interface`] returns a [`AnyImpl`] for the wrong protocol then a panic is - /// generated. - fn interface_mut_for<'a, P: TypeNameable<'a, 'ctx>>(&'a mut self) -> Option<&'a mut P> - where - 'ctx: 'a; -} - -impl<'ctx, T: ImplementerMut<'ctx> + ?Sized> ImplementerMutExt<'ctx> for T { - fn interface_mut_for<'a, P: TypeNameable<'a, 'ctx>>(&'a mut self) -> Option<&'a mut P> - where - 'ctx: 'a - { - match self.interface_mut(LtTypeId::of::<P>()) { - Some(interface) => match interface.downcast::<P>() { - Ok(implementation) => Some(implementation), - Err(interface) => panic!( - "unexpected protocol implementation: `{:?}`, expected: `{:?}`", - interface.id(), - LtTypeId::of::<P>() - ), - }, - None => None, - } - } -} - -/// Implement [`Implementer`] and [`Implementation`] for a set of protocols. -#[doc(hidden)] -#[macro_export] -macro_rules! implementer { - { - impl[$a:lifetime, $ctx:lifetime $($generic:tt)*] $name:ty = [$($protocol:ty),* $(,)?]; - } => { - impl<$ctx $($generic)*> $crate::protocol::ImplementerMut<$ctx> for $name { - #[inline] - fn interface_mut<$a>( - &$a mut self, - id: $crate::any::LtTypeId<$ctx> - ) -> ::core::option::Option<$crate::any::IndirectLtAny<$a, $ctx, $crate::any::Mut>> - where - $ctx: $a - { - match id { - $(id if id == $crate::any::LtTypeId::of::<$protocol>() - => ::core::option::Option::Some($crate::any::IndirectLtAny::<$a, $ctx, _>::new::<$protocol>(self as _)),)* - _ => ::core::option::Option::None - } - } - } - }; -} -#[doc(inline)] -pub use implementer; - #[cfg(test)] mod test { use crate::nameable; |