plugin_macro.rs

 1#[macro_export]
 2macro_rules! impl_plugin {
 3    ($plugin:ty, $proxy:ident, [$(($evt:ty, $pri:expr) => $func:ident),*]) => {
 4        impl $crate::plugin::Plugin for $plugin {
 5            fn get_proxy(&mut self) -> &mut $crate::plugin::PluginProxy {
 6                &mut self.$proxy
 7            }
 8        }
 9
10        #[allow(unused_variables)]
11        impl $crate::plugin::PluginInit for $plugin {
12            fn init( dispatcher: &$crate::event::Dispatcher
13                   , me: ::std::sync::Arc<$crate::plugin::Plugin>) {
14                $(
15                    let new_arc = me.clone();
16                    dispatcher.register($pri, move |e: &$evt| {
17                        let p = new_arc.as_any().downcast_ref::<$plugin>().unwrap();
18                        p . $func(e)
19                    });
20                )*
21            }
22        }
23    };
24
25    ($plugin:ty, $proxy:ident, [$(($evt:ty, $pri:expr) => $func:ident),*,]) => {
26        impl_plugin!($plugin, $proxy, [$(($evt, $pri) => $func),*]);
27    };
28}