diff options
Diffstat (limited to 'src/mock/mod.rs')
-rw-r--r-- | src/mock/mod.rs | 111 |
1 files changed, 35 insertions, 76 deletions
diff --git a/src/mock/mod.rs b/src/mock/mod.rs index 0e4ba03..b21c451 100644 --- a/src/mock/mod.rs +++ b/src/mock/mod.rs @@ -55,7 +55,7 @@ use ::libc::{c_char, c_int}; use ::ltptr::*; -use ::selfref::{Holder, NewWith, OperateIn, opaque}; +use ::selfref::{Holder, opaque}; struct Context { } @@ -401,19 +401,15 @@ unsafe extern "C" fn hexchat_event_attrs_free<'ph>(ph: MutLtPtr<'ph, Ph<'ph>>, impl<'env> PluginEnvironment<'env> { /// Creates a new plugin environment. pub fn new() -> Holder<'static, PluginEnvironmentK> { - struct NewEnv; - impl<'k> NewWith<'k, PluginEnvironmentK> for NewEnv { - fn new_with<'env>(self) -> PluginEnvironment<'env> { - PluginEnvironment { - plugins: Default::default(), - hooks: Default::default(), - extra_hooks: Default::default(), - depth: Default::default(), - contexts: Default::default(), - } + Holder::<'_, PluginEnvironmentK>::new_with(|builder| builder.build( + PluginEnvironment { + plugins: Default::default(), + hooks: Default::default(), + extra_hooks: Default::default(), + depth: Default::default(), + contexts: Default::default(), } - } - Holder::new_with(NewEnv) + )) } /// Loads a plugin. @@ -426,38 +422,23 @@ impl<'env> PluginEnvironment<'env> { // add the plugin self.plugins.borrow_mut().push(plug.clone()); - struct MockPluginInit<'arg, T> { - arg: Option<&'arg CStr>, - _p: PhantomData<T> - } - impl<'k, 'env: 'k, 'arg, T> OperateIn<'k, MockPluginK<'env>> - for MockPluginInit<'arg, T> - where T: PluginFactory { - type Out = bool; - fn operate_in<'ph>( - self, - ph: Pin<&'ph UnsafeCell<MockPlugin<'ph, 'env>>>, - ) -> bool where 'k: 'ph { - let ptr = ph.get(); - let arg = self.arg; - unsafe { - // setup deinit function - (*ptr).deinit = Some( - crate::hexchat_plugin_deinit::<'ph, T::Plugin<'ph>> - ); - crate::hexchat_plugin_init::<'ph, T::Plugin<'ph>>( - MutLtPtr::from_raw(ptr as *mut _), - ptr::addr_of_mut!((*ptr).plugin_name), - ptr::addr_of_mut!((*ptr).plugin_desc), - ptr::addr_of_mut!((*ptr).plugin_vers), - arg.map(|arg| arg.as_ptr()).unwrap_or(ptr::null()), - ) != 0 - } + if plug.as_ref().operate_in(|ph| { + let ptr = ph.get(); + unsafe { + // setup deinit function + (*ptr).deinit = Some( + crate::hexchat_plugin_deinit::<'_, T::Plugin<'_>> + ); + crate::hexchat_plugin_init::<T::Plugin<'_>>( + MutLtPtr::from_raw(ptr as *mut _), + ptr::addr_of_mut!((*ptr).plugin_name), + ptr::addr_of_mut!((*ptr).plugin_desc), + ptr::addr_of_mut!((*ptr).plugin_vers), + ConstLtPtr::from_raw( + arg.map(|arg| arg.as_ptr()).unwrap_or(ptr::null()) + ), + ) != 0 } - } - if plug.as_ref().operate_in(MockPluginInit::<T> { - arg, - _p: PhantomData, }) { Ok(()) } else { @@ -482,17 +463,8 @@ impl<'ph, 'env> MockPlugin<'ph, 'env> { env: Pin<&'env PluginEnvironment<'env>>, filename: CString, ) -> Pin<Rc<Holder<'env, MockPluginK<'env>>>> { - struct MockPluginNewWith<'env> { - filename: CString, - env: Pin<&'env PluginEnvironment<'env>> - } - impl<'k, 'env> NewWith<'k, MockPluginK<'env>> - for MockPluginNewWith<'env> - where 'env: 'k { - fn new_with<'ph>(self) -> UnsafeCell<MockPlugin<'ph, 'env>> - where 'k: 'ph { - let filename = self.filename; - let env = self.env; + let plug = Rc::pin(Holder::<'_, MockPluginK<'env>>::new_with( + |builder| builder.build({ UnsafeCell::new(MockPlugin { methods: Ph { hexchat_hook_command, @@ -547,28 +519,15 @@ impl<'ph, 'env> MockPlugin<'ph, 'env> { deinit: None, context: None, }) + }) + )); + plug.as_ref().operate_in(|ph| { + let ptr = ph.get(); + unsafe { + // initialize plugin_name from filename(!) + (*ptr).plugin_name = (*ptr).filename.as_ptr(); } - } - let plug = Rc::pin(Holder::new_with(MockPluginNewWith { - filename, - env, - })); - struct MockPluginPreInit; - impl<'k, 'env: 'k> OperateIn<'k, MockPluginK<'env>> - for MockPluginPreInit { - type Out = (); - fn operate_in<'ph>( - self, - ph: Pin<&'ph UnsafeCell<MockPlugin<'ph, 'env>>>, - ) where 'k: 'ph { - let ptr = ph.get(); - unsafe { - // initialize plugin_name from filename(!) - (*ptr).plugin_name = (*ptr).filename.as_ptr(); - } - } - } - plug.as_ref().operate_in(MockPluginPreInit); + }); plug } } |