From 890d8bbef986d8b85d6eaaddd4c8895c5f61c09c Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Thu, 16 Jun 2022 22:43:18 -0300 Subject: Migrate to edition 2021 --- Cargo.toml | 5 +- src/lib.rs | 19 ++--- src/mock/mod.rs | 218 +++++++++++++++++++++++++++++++------------------------- 3 files changed, 137 insertions(+), 105 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f2f65c2..a88a2b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,11 +9,14 @@ homepage = "https://ganarchy.autistic.space/project/207026ae5e35da096b8110afe1ba keywords = ["hexchat", "plugin", "hexchat-plugin"] categories = ["api-bindings"] readme = "README.md" +edition = "2021" [features] nul_in_strip = [] -nightly_tests = [] +nightly_tests = ["dep:selfref"] [dependencies] libc = "0.2" impl_trait = "0.1" +ltptr = "0.1.1" +selfref = { version = "0.1", optional = true } diff --git a/src/lib.rs b/src/lib.rs index d4e5d98..308559a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -190,11 +190,14 @@ */ #![cfg_attr(feature="nightly_tests", feature(c_variadic))] +#![cfg_attr(feature="nightly_tests", feature(generic_associated_types))] #[macro_use] extern crate impl_trait; #[doc(hidden)] pub extern crate libc; +extern crate ltptr; + // private macros @@ -220,14 +223,14 @@ mod pluginfo; mod strip; mod word; -pub use eat::*; -pub use infoid::InfoId; -pub use strip::*; -pub use word::*; +pub use crate::eat::*; +pub use crate::infoid::InfoId; +pub use crate::strip::*; +pub use crate::word::*; -use internals::HexchatEventAttrs as RawAttrs; -use internals::Ph as RawPh; -use pluginfo::PluginInfo; +use crate::internals::HexchatEventAttrs as RawAttrs; +use crate::internals::Ph as RawPh; +use crate::pluginfo::PluginInfo; use std::borrow::Cow; use std::cell::Cell; @@ -399,7 +402,7 @@ mod valid_context { } } } -pub use valid_context::ValidContext; +pub use crate::valid_context::ValidContext; /// Event attributes. // TODO better docs. diff --git a/src/mock/mod.rs b/src/mock/mod.rs index ed41780..92de66c 100644 --- a/src/mock/mod.rs +++ b/src/mock/mod.rs @@ -46,19 +46,25 @@ use std::marker::PhantomData; use std::marker::PhantomPinned; use std::pin::Pin; use std::ptr; +use std::rc::Rc; use libc::c_char; +use ::ltptr::*; + +use ::selfref::{Holder, Opaque, SelfRef, opaque}; + +// MUST be repr(C) #[repr(C)] -struct MockPlugin { +struct MockPlugin<'dangling, 'env> { // NOTE: MUST be first thing in the struct. methods: crate::internals::Ph, filename: *const c_char, - plugin_name: *const c_char, - plugin_desc: *const c_char, - plugin_vers: *const c_char, + plugin_name: Cell<*const c_char>, + plugin_desc: Cell<*const c_char>, + plugin_vers: Cell<*const c_char>, free_strings: bool, - env: *const PluginEnvironment, + env: &'env PluginEnvironment<'dangling, 'env>, _pin: PhantomPinned, } @@ -90,21 +96,40 @@ enum MockCallback { HookDeleted, } -// This MockHook is deliberately incompatible with hexchat's own hook struct. -struct MockHook<'hook, 'env> { - pl: &MockPlugin<'env>, +struct MockHook<'dangling, 'env> { + //pl: Rc>>, + pl: Rc>, cb: RefCell, name: Option>, pri_or_fd: libc::c_int, userdata: *mut libc::c_void, + _pin: PhantomPinned, } -pub struct PluginEnvironment<'env> { - plugins: Option>>>>, - hooks: Option>>>>, - depth: usize, +pub struct PluginEnvironment<'dangling, 'env> { + //plugins: RefCell>>>>>, + plugins: RefCell>>>>, + // priority-based hooks + //hooks: RefCell>>>>>, + hooks: RefCell>>>>, + // fd/timer hooks + //extra_hooks: RefCell>>>>>, + extra_hooks: RefCell>>>>, + depth: Cell, +} +impl<'dangling, 'env> SelfRef<'env> for PluginEnvironment<'dangling, 'env> {} + +pub struct PluginEnvironmentK<'dangling> { + _p: PhantomData<&'dangling ()>, +} +opaque! { + impl['dangling] Opaque for PluginEnvironmentK<'dangling> { + type Kind<'env> = PluginEnvironment<'dangling, 'env>; + } } +pub type GlobalPluginEnvironment<'dangling> = Holder<'dangling, PluginEnvironmentK<'dangling>>; + unsafe extern "C" fn hexchat_hook_command( ph: *mut crate::internals::HexchatPlugin, name: *const libc::c_char, @@ -325,93 +350,94 @@ unsafe extern "C" fn hexchat_event_attrs_free(ph: *mut crate::internals::Hexchat todo!(); } -impl PluginEnvironment { - pub fn new() -> Pin> { - Box::pin(Self { +impl<'dangling, 'env> PluginEnvironment<'dangling, 'env> { + pub fn new() -> Self { + Self { plugins: Default::default(), hooks: Default::default(), + extra_hooks: Default::default(), depth: Default::default(), - _pin: PhantomPinned, - }) - } -} - -impl MockPlugin { - pub fn new crate::Plugin<'ph> + Default>( - env: Pin<&PluginEnvironment>, - filename: CString, - arg: Option<&CStr>, - ) -> Option>> { - let filename = filename.into_raw(); - let mut plug = Box::pin(MockPlugin { - methods: crate::internals::Ph { - hexchat_hook_command, - hexchat_hook_server, - hexchat_hook_print, - hexchat_hook_timer, - hexchat_hook_fd, - hexchat_unhook, - hexchat_print, - userdata: hexchat_printf as *mut libc::c_void, - hexchat_command, - hexchat_commandf_do_not_use: hexchat_commandf, - hexchat_nickcmp, - hexchat_set_context, - hexchat_find_context, - hexchat_get_context, - hexchat_get_info, - hexchat_get_prefs, - hexchat_list_get, - hexchat_list_free, - hexchat_list_fields, - hexchat_list_next, - hexchat_list_str, - hexchat_list_int, - hexchat_plugingui_add, - hexchat_plugingui_remove, - hexchat_emit_print, - hexchat_read_fd, - hexchat_list_time, - hexchat_gettext, - hexchat_send_modes, - hexchat_strip, - hexchat_free, - hexchat_pluginpref_set_str, - hexchat_pluginpref_get_str, - hexchat_pluginpref_set_int, - hexchat_pluginpref_get_int, - hexchat_pluginpref_delete, - hexchat_pluginpref_list, - hexchat_hook_server_attrs, - hexchat_hook_print_attrs, - hexchat_emit_print_attrs, - hexchat_event_attrs_create, - hexchat_event_attrs_free, - }, - filename: filename, - plugin_name: filename, - plugin_desc: ptr::null(), - plugin_vers: ptr::null(), - free_strings: false, - env: &*env, - _pin: PhantomPinned, - }); - if unsafe { - let ptr: *mut MockPlugin = &mut *plug; - crate::hexchat_plugin_init::<'_, T>( - crate::LtPhPtr { - ph: ptr as *mut crate::internals::Ph, - _lt: PhantomData, - }, - 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 - } { - Some(plug) - } else { - None } } } + +// +//impl MockPlugin { +// pub fn new crate::Plugin<'ph> + Default>( +// env: Pin<&PluginEnvironment>, +// filename: CString, +// arg: Option<&CStr>, +// ) -> Option>> { +// let filename = filename.into_raw(); +// let mut plug = Box::pin(MockPlugin { +// methods: crate::internals::Ph { +// hexchat_hook_command, +// hexchat_hook_server, +// hexchat_hook_print, +// hexchat_hook_timer, +// hexchat_hook_fd, +// hexchat_unhook, +// hexchat_print, +// userdata: hexchat_printf as *mut libc::c_void, +// hexchat_command, +// hexchat_commandf_do_not_use: hexchat_commandf, +// hexchat_nickcmp, +// hexchat_set_context, +// hexchat_find_context, +// hexchat_get_context, +// hexchat_get_info, +// hexchat_get_prefs, +// hexchat_list_get, +// hexchat_list_free, +// hexchat_list_fields, +// hexchat_list_next, +// hexchat_list_str, +// hexchat_list_int, +// hexchat_plugingui_add, +// hexchat_plugingui_remove, +// hexchat_emit_print, +// hexchat_read_fd, +// hexchat_list_time, +// hexchat_gettext, +// hexchat_send_modes, +// hexchat_strip, +// hexchat_free, +// hexchat_pluginpref_set_str, +// hexchat_pluginpref_get_str, +// hexchat_pluginpref_set_int, +// hexchat_pluginpref_get_int, +// hexchat_pluginpref_delete, +// hexchat_pluginpref_list, +// hexchat_hook_server_attrs, +// hexchat_hook_print_attrs, +// hexchat_emit_print_attrs, +// hexchat_event_attrs_create, +// hexchat_event_attrs_free, +// }, +// filename: filename, +// plugin_name: filename, +// plugin_desc: ptr::null(), +// plugin_vers: ptr::null(), +// free_strings: false, +// env: &*env, +// _pin: PhantomPinned, +// }); +// if unsafe { +// let ptr: *mut MockPlugin = &mut *plug; +// crate::hexchat_plugin_init::<'_, T>( +// crate::LtPhPtr { +// ph: ptr as *mut crate::internals::Ph, +// _lt: PhantomData, +// }, +// 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 +// } { +// Some(plug) +// } else { +// None +// } +// } +//} -- cgit 1.4.1