simplify plugin code

This commit is contained in:
lumi 2017-05-27 19:40:00 +02:00
parent 1550c52552
commit 1b6b67b332
4 changed files with 6 additions and 6 deletions

View file

@ -103,7 +103,7 @@ impl ClientBuilder {
pub struct Client { pub struct Client {
jid: Jid, jid: Jid,
transport: Arc<Mutex<SslTransport>>, transport: Arc<Mutex<SslTransport>>,
plugins: HashMap<TypeId, Arc<Box<Plugin>>>, plugins: HashMap<TypeId, Arc<Plugin>>,
binding: PluginProxyBinding, binding: PluginProxyBinding,
dispatcher: Arc<Dispatcher>, dispatcher: Arc<Dispatcher>,
} }
@ -118,7 +118,7 @@ impl Client {
pub fn register_plugin<P: Plugin + PluginInit + 'static>(&mut self, mut plugin: P) { pub fn register_plugin<P: Plugin + PluginInit + 'static>(&mut self, mut plugin: P) {
let binding = self.binding.clone(); let binding = self.binding.clone();
plugin.bind(binding); plugin.bind(binding);
let p = Arc::new(Box::new(plugin) as Box<Plugin>); let p = Arc::new(plugin) as Arc<Plugin>;
P::init(&self.dispatcher, p.clone()); P::init(&self.dispatcher, p.clone());
if self.plugins.insert(TypeId::of::<P>(), p).is_some() { if self.plugins.insert(TypeId::of::<P>(), p).is_some() {
panic!("registering a plugin that's already registered"); panic!("registering a plugin that's already registered");

View file

@ -84,7 +84,7 @@ impl ComponentBuilder {
pub struct Component { pub struct Component {
jid: Jid, jid: Jid,
transport: Arc<Mutex<PlainTransport>>, transport: Arc<Mutex<PlainTransport>>,
plugins: HashMap<TypeId, Arc<Box<Plugin>>>, plugins: HashMap<TypeId, Arc<Plugin>>,
binding: PluginProxyBinding, binding: PluginProxyBinding,
dispatcher: Arc<Dispatcher>, dispatcher: Arc<Dispatcher>,
} }
@ -99,7 +99,7 @@ impl Component {
pub fn register_plugin<P: Plugin + PluginInit + 'static>(&mut self, mut plugin: P) { pub fn register_plugin<P: Plugin + PluginInit + 'static>(&mut self, mut plugin: P) {
let binding = self.binding.clone(); let binding = self.binding.clone();
plugin.bind(binding); plugin.bind(binding);
let p = Arc::new(Box::new(plugin) as Box<Plugin>); let p = Arc::new(plugin) as Arc<Plugin>;
P::init(&self.dispatcher, p.clone()); P::init(&self.dispatcher, p.clone());
if self.plugins.insert(TypeId::of::<P>(), p).is_some() { if self.plugins.insert(TypeId::of::<P>(), p).is_some() {
panic!("registering a plugin that's already registered"); panic!("registering a plugin that's already registered");

View file

@ -90,7 +90,7 @@ pub trait Plugin: Any + PluginAny {
} }
pub trait PluginInit { pub trait PluginInit {
fn init(dispatcher: &Dispatcher, me: Arc<Box<Plugin>>); fn init(dispatcher: &Dispatcher, me: Arc<Plugin>);
} }
pub trait PluginAny { pub trait PluginAny {

View file

@ -10,7 +10,7 @@ macro_rules! impl_plugin {
#[allow(unused_variables)] #[allow(unused_variables)]
impl $crate::plugin::PluginInit for $plugin { impl $crate::plugin::PluginInit for $plugin {
fn init( dispatcher: &$crate::event::Dispatcher fn init( dispatcher: &$crate::event::Dispatcher
, me: ::std::sync::Arc<Box<$crate::plugin::Plugin>>) { , me: ::std::sync::Arc<$crate::plugin::Plugin>) {
$( $(
let new_arc = me.clone(); let new_arc = me.clone();
dispatcher.register($pri, move |e: &$evt| { dispatcher.register($pri, move |e: &$evt| {