diff --git a/Cargo.lock b/Cargo.lock index 8f8f210..7859393 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,9 +75,9 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "cfg-if" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "clap" @@ -136,6 +136,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17" + [[package]] name = "crossterm" version = "0.29.0" @@ -159,6 +165,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "dashmap" +version = "6.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6361d5c062261c78a176addb82d4c821ae42bed6089de0e12603cd25de2059c" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "document-features" version = "0.2.11" @@ -196,6 +216,12 @@ dependencies = [ "wasip2", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "hashbrown" version = "0.16.0" @@ -215,7 +241,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.16.0", ] [[package]] @@ -297,9 +323,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.21.3" +version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "once_cell_polyfill" @@ -420,6 +446,15 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "sarasacw-omrf" +version = "1.0.0" +source = "git+https://github.com/SarasasChipWorkshop/sarasacw-omrf.git#38b64a10928566fc9193c3c0c5138901b6cd952e" +dependencies = [ + "dashmap", + "thiserror", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -702,6 +737,7 @@ name = "wfassoc-cdylib" version = "0.1.0" dependencies = [ "num_enum", + "sarasacw-omrf", "slotmap", "thiserror", "wfassoc", diff --git a/wfassoc-cdylib/Cargo.toml b/wfassoc-cdylib/Cargo.toml index 6b161cf..9add552 100644 --- a/wfassoc-cdylib/Cargo.toml +++ b/wfassoc-cdylib/Cargo.toml @@ -3,14 +3,32 @@ name = "wfassoc-cdylib" version = "0.1.0" authors = ["yyc12345"] edition = "2024" -description = "The dynamic wfassoc library exposed for C/C++ and other languages users." -license = "SPDX:MIT" +description = "The dynamic library exposed for C/C++ users reading or manipulating Windows file assocation." +license = "MIT" [lib] crate-type = ["cdylib"] [dependencies] thiserror = { workspace = true } +sarasacw-omrf = { git = "https://github.com/SarasasChipWorkshop/sarasacw-omrf.git"} +# sarasacw-omrf = { version="1.0.0", git = "https://github.com/SarasasChipWorkshop/sarasacw-omrf.git", tag = "omrf/1.0.0" } wfassoc = { path="../wfassoc" } slotmap = "1.1.1" num_enum = "0.7.6" + +[profile.release] +# Any panics is seen as `std::abort` in production environment. +panic = "abort" + +[package.metadata.omrf] +min_version = "1.0.0" +headers = [ + { from = "cbinding/*.h", to = "" } +] +[package.metadata.omrf.cmake] +namespace_name = "wfassoc" +target_name = "wfassoc" +[package.metadata.omrf.pkgconfig] +id = "wfassoc" +name = "Windows file association manipulation library" diff --git a/wfassoc-cdylib/cbinding/Findwfassoc.cmake b/wfassoc-cdylib/cbinding/Findwfassoc.cmake deleted file mode 100644 index b2ee06d..0000000 --- a/wfassoc-cdylib/cbinding/Findwfassoc.cmake +++ /dev/null @@ -1,101 +0,0 @@ -# Findwfassoc.cmake -# ---------------- -# Find wfassoc library and headers. -# -# This module requires the user to set wfassoc_ROOT to the installation -# directory of wfassoc. The directory structure under wfassoc_ROOT must be: -# bin/ - contains wfassoc_cdylib.dll -# include/ - contains wfassoc.h and wfassoc++.h -# lib/ - contains wfassoc_cdylib.dll.lib (import library) -# -# This module defines the following variables: -# wfassoc_FOUND - True if wfassoc was found -# wfassoc_INCLUDE_DIRS - Path to wfassoc include directory -# wfassoc_LIBRARIES - Path to wfassoc import library -# wfassoc_DLL - Path to wfassoc DLL -# wfassoc_ROOT - The root directory (user-provided) -# -# This module also creates the following imported targets: -# wfassoc::wfassoc - Main wfassoc library (includes both include and link) -# - -set(wfassoc_FOUND FALSE) - -# Require user to set wfassoc_ROOT -if(NOT wfassoc_ROOT) - message(FATAL_ERROR "wfassoc_ROOT must be set to the installation directory of wfassoc") -endif() - -# Check existence of required subdirectories -if(NOT EXISTS ${wfassoc_ROOT}) - message(FATAL_ERROR "wfassoc_ROOT directory does not exist: ${wfassoc_ROOT}") -endif() - -set(wfassoc_INCLUDE_DIR ${wfassoc_ROOT}/include) -set(wfassoc_LIB_DIR ${wfassoc_ROOT}/lib) -set(wfassoc_BIN_DIR ${wfassoc_ROOT}/bin) - -# Find header files -if(EXISTS ${wfassoc_INCLUDE_DIR}/wfassoc.h AND EXISTS ${wfassoc_INCLUDE_DIR}/wfassoc++.h) - set(wfassoc_INCLUDE_DIRS ${wfassoc_INCLUDE_DIR}) -else() - message(SEND_ERROR "Missing wfassoc header files in ${wfassoc_INCLUDE_DIR}") - return() -endif() - -# Find import library (.lib) -find_file(wfassoc_LIBRARIES - NAMES wfassoc_cdylib.dll.lib - PATHS ${wfassoc_LIB_DIR} - NO_DEFAULT_PATH - DOC "wfassoc import library" -) - -if(NOT wfassoc_LIBRARIES) - message(SEND_ERROR "Missing wfassoc import library (wfassoc_cdylib.dll.lib) in ${wfassoc_LIB_DIR}") - return() -endif() - -# Find DLL file -find_file(wfassoc_DLL - NAMES wfassoc_cdylib.dll - PATHS ${wfassoc_BIN_DIR} - NO_DEFAULT_PATH - DOC "wfassoc dynamic library" -) - -if(NOT wfassoc_DLL) - message(SEND_ERROR "Missing wfassoc DLL (wfassoc_cdylib.dll) in ${wfassoc_BIN_DIR}") - return() -endif() - -# Everything found -set(wfassoc_FOUND TRUE) - -# Mark variables as advanced for ccmake/cmake-gui -mark_as_advanced(wfassoc_INCLUDE_DIRS wfassoc_LIBRARIES wfassoc_DLL) - -# Create imported target for wfassoc -if(wfassoc_FOUND AND NOT TARGET wfassoc::wfassoc) - add_library(wfassoc::wfassoc SHARED IMPORTED) - - # Set include directories - set_target_properties(wfassoc::wfassoc PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${wfassoc_INCLUDE_DIRS} - ) - - # Set import library location - set_target_properties(wfassoc::wfassoc PROPERTIES - IMPORTED_IMPLIB "${wfassoc_LIBRARIES}" - IMPORTED_LOCATION "${wfassoc_DLL}" - ) -endif() - -# Optional: Print status message -if(wfassoc_FOUND) - message(STATUS "Found wfassoc:") - message(STATUS " Root : ${wfassoc_ROOT}") - message(STATUS " Include : ${wfassoc_INCLUDE_DIRS}") - message(STATUS " Library : ${wfassoc_LIBRARIES}") - message(STATUS " DLL : ${wfassoc_DLL}") -endif() diff --git a/wfassoc-cdylib/src/cstr_ffi.rs b/wfassoc-cdylib/src/cstr_ffi.rs deleted file mode 100644 index 9f3bd9a..0000000 --- a/wfassoc-cdylib/src/cstr_ffi.rs +++ /dev/null @@ -1,101 +0,0 @@ -//! When calling this dynamic library with outside programs, -//! outer programs may usually need to fetch string resource produced by Rust code. -//! However it is impossible pass Rust string directly to outer program. -//! -//! This module provide **thread independent** string cache for resolving this issue. -//! When we need pass string to outer programs, we push that string into this string as C-like format, -//! then return its pointer to outer program. -//! So that outside program can utilize it like calling C/C++ library. -//! The only thing that outer programs should note is that this string is volatile, -//! once they get it, they must dupliate it immediately before any futher calling to this dynamic library. -use std::cell::RefCell; -use std::ffi::{CStr, CString}; -use thiserror::Error as TeError; -use crate::ffi_types::CStyleString; - -// region: Error - -/// Error occurs in this crate. -#[derive(Debug, TeError)] -pub enum Error { - #[error("unexpected NUL when parsing into C/C++ string")] - UnexpectedNul(#[from] std::ffi::NulError), - - #[error("given string pointer is nullptr when parsing from C/C++ string")] - NullPtr, - #[error("invalid UTF8 sequence when parsing from C/C++ string")] - InvalidEncoding(#[from] std::str::Utf8Error), -} - -/// Result type used in this crate. -type Result = std::result::Result; - -// endregion - -// region: String Cache for Exposing - -struct StringCache { - msg: CString, -} - -impl StringCache { - fn new() -> Self { - Self { - msg: CString::new("").expect("empty string must be valid for CString"), - } - } - - pub fn set_msg(&mut self, msg: &str) -> Result<()> { - self.msg = CString::new(msg)?; - Ok(()) - } - - pub fn get_msg(&self) -> CStyleString { - self.msg.as_ptr() - } - - pub fn clear_msg(&mut self) { - self.msg = CString::new("").expect("empty string must be valid for CString"); - } -} - -// endregion - -// region: Exposed Functions - -thread_local! { - static STRING_CACHE: RefCell = RefCell::new(StringCache::new()); -} - -/// Set thread local string exposed for C code. -pub fn set_ffi_string(msg: &str) -> Result<()> { - STRING_CACHE.with(|e| { - e.borrow_mut().set_msg(msg) - }) -} - -/// Get const pointer to thread local string exposed for C code. -pub fn get_ffi_string() -> CStyleString { - STRING_CACHE.with(|e| e.borrow().get_msg()) -} - -/// Clear thread local string exposed for C code. -/// -/// This function usually should be called at the beginning of every exposed C functions. -pub fn clear_ffi_string() { - STRING_CACHE.with(|e| { - e.borrow_mut().clear_msg(); - }); -} - -/// Parse string given by C code into Rust string. -pub fn parse_ffi_string<'a>(ptr: CStyleString) -> Result<&'a str> { - if ptr.is_null() { - Err(Error::NullPtr) - } else { - let c_str = unsafe { CStr::from_ptr(ptr) }; - Ok(c_str.to_str()?) - } -} - -// endregion diff --git a/wfassoc-cdylib/src/last_error.rs b/wfassoc-cdylib/src/last_error.rs deleted file mode 100644 index 6f1997b..0000000 --- a/wfassoc-cdylib/src/last_error.rs +++ /dev/null @@ -1,58 +0,0 @@ -//! When calling function with dynamic library, -//! function return value indicates whether function has been successfully executed. -//! When function return `false`, programmer may want to know which error occurs. -//! -//! This module provide **thread independent** error message string storage, -//! which is more like Win32 `GetLastError()` but return error message instead of error code. -//! These module provided functions will be called when executing main module functions. - -use std::cell::RefCell; -use std::ffi::{CString, c_char}; - -struct LastError { - msg: CString, -} - -impl LastError { - fn new() -> Self { - Self { - msg: CString::new("").expect("empty string must be valid for CString"), - } - } - - pub fn set_msg(&mut self, msg: &str) { - self.msg = CString::new(msg).expect("unexpected blank in error message output"); - } - - pub fn get_msg(&self) -> *const c_char { - self.msg.as_ptr() - } - - pub fn clear_msg(&mut self) { - self.msg = CString::new("").expect("empty string must be valid for CString"); - } -} - -thread_local! { - static LAST_ERROR: RefCell = RefCell::new(LastError::new()); -} - -/// Set thread local error message. -pub fn set_last_error(msg: &str) { - LAST_ERROR.with(|e| { - e.borrow_mut().set_msg(msg); - }); -} - -/// Get const pointer to thread local error message string. -/// If there is no error, return pointer will point to empty string. -pub fn get_last_error() -> *const c_char { - LAST_ERROR.with(|e| e.borrow().get_msg()) -} - -/// Clear thread local error message (reset to empty string). -pub fn clear_last_error() { - LAST_ERROR.with(|e| { - e.borrow_mut().clear_msg(); - }); -} diff --git a/wfassoc-cdylib/src/lib.rs b/wfassoc-cdylib/src/lib.rs index 39cf370..1aa7ea1 100644 --- a/wfassoc-cdylib/src/lib.rs +++ b/wfassoc-cdylib/src/lib.rs @@ -1,10 +1,8 @@ -mod cstr_ffi; -mod ffi_types; -mod last_error; -mod object_pool; +mod wrapper; -use object_pool::ObjectPool; -use std::sync::{LazyLock, RwLock}; +use sarasacw_omrf::object_pool::ObjectPool; +use sarasacw_omrf::{cffi_wrapper, cstr_ffi, in_param_ty, last_error, object_pool, out_param_ty}; +use std::sync::LazyLock; use thiserror::Error as TeError; use wfassoc::highlevel::{Program, Schema}; @@ -12,7 +10,7 @@ use wfassoc::highlevel::{Program, Schema}; /// Error occurs in this crate. #[derive(Debug, TeError)] -enum Error { +enum UserError { /// Error when operating Schema. #[error("wfassoc error: {0}")] Schema(#[from] wfassoc::highlevel::SchemaError), @@ -33,180 +31,65 @@ enum Error { /// Error occurs when checking enum value #[error("the enumeration value provided to FFI function is out of its range")] EnumOutOfRange, - /// Error when manipulating with poison RwLock - #[error("concurrency error: RwLock is poisonous")] - PoisonRwLock, +} + +impl From for WFError { + fn from(_: UserError) -> Self { + WFERROR_ERR + } } /// Result type used in this crate. -type Result = std::result::Result; +type Result = std::result::Result; // endregion // region: Macros -macro_rules! in_param_ty { - ($t:ty) => { - $t - }; -} - -macro_rules! out_param_ty { - ($t:ty) => { - *mut $t - }; -} - -macro_rules! set_out_param { - ($lhs:expr, $rhs:expr) => { - unsafe { *$lhs = $rhs }; - }; -} - macro_rules! resolve_enum { ($t:ty, $v:expr) => { - <$t>::try_from($v).map_err(|_| Error::EnumOutOfRange) + <$t>::try_from($v).map_err(|_| UserError::EnumOutOfRange) }; } -macro_rules! pull_reader { - ($pool:expr) => { - $pool.read().map_err(|_| Error::PoisonRwLock) - }; -} - -macro_rules! pull_writer { - ($pool:expr) => { - $pool.write().map_err(|_| Error::PoisonRwLock) - }; -} - -/// Macro to wrap inner function execution with standard error handling pattern. -/// -/// For functions with no output parameter and no input parameters: -/// ```ignore -/// cffi_wrapper!(|| { -/// // inner function body returning Result<()> -/// }); -/// ``` -/// -/// For functions with no output parameter and with input parameters: -/// ```ignore -/// cffi_wrapper!(|param1: Type1, param2: Type2| { -/// // inner function body using param1, param2 returning Result<()> -/// }); -/// ``` -/// -/// For functions with one output parameter and no input parameters: -/// ```ignore -/// cffi_wrapper!(|| -> (out_param, OutType) { -/// // inner function body returning Result -/// }); -/// ``` -/// -/// For functions with one output parameter and with input parameters: -/// ```ignore -/// cffi_wrapper!(|param1: Type1| -> (out_param, OutType) { -/// // inner function body using param1 returning Result -/// }); -/// ``` -macro_rules! cffi_wrapper { - // Case with output parameter and input parameters - (|$($param:ident: $param_ty:ty),*| -> ($out_param:ident: $out_param_ty:ty) $inner_body:block) => {{ - fn inner($($param: $param_ty),*) -> Result<$out_param_ty> $inner_body - - match inner($($param),*) { - Ok(rv) => { - set_out_param!($out_param, rv); - last_error::clear_last_error(); - true - } - Err(e) => { - last_error::set_last_error(e.to_string().as_str()); - false - } - } - }}; - - // Case with output parameter and no input parameters - (|| -> ($out_param:ident: $out_param_ty:ty) $inner_body:block) => {{ - fn inner() -> Result<$out_param_ty> $inner_body - - match inner() { - Ok(rv) => { - set_out_param!($out_param, rv); - last_error::clear_last_error(); - true - } - Err(e) => { - last_error::set_last_error(e.to_string().as_str()); - false - } - } - }}; - - // Case without output parameter but with input parameters - (|$($param:ident: $param_ty:ty),*| $inner_body:block) => {{ - fn inner($($param: $param_ty),*) -> Result<()> $inner_body - - match inner($($param),*) { - Ok(_) => { - last_error::clear_last_error(); - true - } - Err(e) => { - last_error::set_last_error(e.to_string().as_str()); - false - } - } - }}; - - // Case without output parameter and no input parameters - (|| $inner_body:block) => {{ - fn inner() -> Result<()> $inner_body - - match inner() { - Ok(_) => { - last_error::clear_last_error(); - true - } - Err(e) => { - last_error::set_last_error(e.to_string().as_str()); - false - } - } - }}; -} - // endregion // region: Object Pools -static SCHEMA_POOL: LazyLock>> = - LazyLock::new(|| RwLock::new(ObjectPool::new())); +struct State { + schema_pool: ObjectPool, + program_pool: ObjectPool, + ext_status_pool: ObjectPool, + self_ext_status_pool: ObjectPool, + icon_rc_pool: ObjectPool, +} -static PROGRAM_POOL: LazyLock>> = - LazyLock::new(|| RwLock::new(ObjectPool::new())); +impl State { + fn new() -> Self { + Self { + schema_pool: ObjectPool::new(), + program_pool: ObjectPool::new(), + ext_status_pool: ObjectPool::new(), + self_ext_status_pool: ObjectPool::new(), + icon_rc_pool: ObjectPool::new(), + } + } +} -static EXT_STATUS_POOL: LazyLock>> = - LazyLock::new(|| RwLock::new(ObjectPool::new())); - -static SELF_EXT_STATUS_POOL: LazyLock< - RwLock>, -> = LazyLock::new(|| RwLock::new(ObjectPool::new())); - -static ICON_RC_POOL: LazyLock>> = - LazyLock::new(|| RwLock::new(ObjectPool::new())); +static CONTEXT: LazyLock = LazyLock::new(|| State::new()); // endregion // region: Exposed Types -pub use ffi_types::{ - CStyleString as WFCString, HICON as WFHICON, Scope as WFScope, View as WFView, -}; -pub use ffi_types::{INVALID_HICON as WF_INVALID_HICON, INVALID_INDEX as WF_INVALID_INDEX}; -pub use object_pool::Token as WFToken; +pub use last_error::CERROR_OK as WFERROR_OK; +pub use last_error::CError as WFError; +pub const WFERROR_ERR: WFError = 1; + +pub use cstr_ffi::CStrPtr as WFStrPtr; +pub use object_pool::{INVALID_TOKEN as WF_INVALID_TOKEN, Token as WFToken}; +pub use wrapper::{HICON as WFHICON, Scope as WFScope, View as WFView}; +pub use wrapper::{INVALID_HICON as WF_INVALID_HICON, INVALID_INDEX as WF_INVALID_INDEX}; // endregion @@ -215,67 +98,37 @@ pub use object_pool::Token as WFToken; // region: Facilities #[unsafe(no_mangle)] -pub extern "C" fn WFStartup() -> bool { - // Initialize all pool by fetching writer from them - cffi_wrapper!(|| { - let _pool = pull_writer!(SCHEMA_POOL)?; - let _pool = pull_writer!(PROGRAM_POOL)?; - let _pool = pull_writer!(EXT_STATUS_POOL)?; - let _pool = pull_writer!(SELF_EXT_STATUS_POOL)?; - let _pool = pull_writer!(ICON_RC_POOL)?; - Ok(()) +pub extern "C" fn WFGetLastError() -> WFStrPtr { + last_error::get_error_message() +} + +// endregion + +// region: Global Functions + +#[unsafe(no_mangle)] +pub extern "C" fn WFHasPrivilege(out_has: out_param_ty!(bool)) -> WFError { + cffi_wrapper!(|| -> (out_has: bool) { + Ok(wfassoc::win32::utilities::has_privilege()) }) } -#[unsafe(no_mangle)] -pub extern "C" fn WFShutdown() -> bool { - // Free all pool stored objects - cffi_wrapper!(|| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - pool.clear(); - let mut pool = pull_writer!(PROGRAM_POOL)?; - pool.clear(); - let mut pool = pull_writer!(EXT_STATUS_POOL)?; - pool.clear(); - let mut pool = pull_writer!(SELF_EXT_STATUS_POOL)?; - pool.clear(); - let mut pool = pull_writer!(ICON_RC_POOL)?; - pool.clear(); - Ok(()) - }) -} - -#[unsafe(no_mangle)] -pub extern "C" fn WFGetLastError() -> WFCString { - last_error::get_last_error() -} - -#[unsafe(no_mangle)] -pub extern "C" fn WFHasPrivilege() -> bool { - wfassoc::win32::utilities::has_privilege() -} - -#[unsafe(no_mangle)] -pub extern "C" fn WFInvalidToken() -> WFToken { - object_pool::invalid_token() -} - // endregion // region: Schema #[unsafe(no_mangle)] -pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(WFToken)) -> bool { +pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(WFToken)) -> WFError { cffi_wrapper!(|| -> (out_schema: WFToken) { - let mut pool = pull_writer!(SCHEMA_POOL)?; - Ok(pool.allocate(Schema::new())?) + let pool = &CONTEXT.schema_pool; + Ok(pool.allocate(Schema::new())) }) } #[unsafe(no_mangle)] -pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(WFToken)) -> bool { +pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(WFToken)) -> WFError { cffi_wrapper!(|in_schema: WFToken| { - let mut pool = pull_writer!(SCHEMA_POOL)?; + let pool = &CONTEXT.schema_pool; Ok(pool.free(in_schema)?) }) } @@ -283,11 +136,11 @@ pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(WFToken)) -> bool { #[unsafe(no_mangle)] pub extern "C" fn WFSchemaSetIdentifier( in_schema: in_param_ty!(WFToken), - in_value: in_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_schema: WFToken, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; + in_value: in_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_schema: WFToken, in_value: WFStrPtr| { + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.set_identifier(cstr_ffi::parse_ffi_string(in_value)?); Ok(()) }) @@ -296,11 +149,11 @@ pub extern "C" fn WFSchemaSetIdentifier( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaSetPath( in_schema: in_param_ty!(WFToken), - in_value: in_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_schema: WFToken, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; + in_value: in_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_schema: WFToken, in_value: WFStrPtr| { + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.set_path(cstr_ffi::parse_ffi_string(in_value)?); Ok(()) }) @@ -309,11 +162,11 @@ pub extern "C" fn WFSchemaSetPath( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaSetClsid( in_schema: in_param_ty!(WFToken), - in_value: in_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_schema: WFToken, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; + in_value: in_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_schema: WFToken, in_value: WFStrPtr| { + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.set_clsid(cstr_ffi::parse_ffi_string(in_value)?); Ok(()) }) @@ -322,17 +175,17 @@ pub extern "C" fn WFSchemaSetClsid( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaSetName( in_schema: in_param_ty!(WFToken), - in_value: in_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_schema: WFToken, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; - + in_value: in_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_schema: WFToken, in_value: WFStrPtr| { let name = if in_value.is_null() { None } else { Some(cstr_ffi::parse_ffi_string(in_value)?) }; + + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.set_name(name); Ok(()) }) @@ -341,17 +194,17 @@ pub extern "C" fn WFSchemaSetName( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaSetIcon( in_schema: in_param_ty!(WFToken), - in_value: in_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_schema: WFToken, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; - + in_value: in_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_schema: WFToken, in_value: WFStrPtr| { let icon = if in_value.is_null() { None } else { Some(cstr_ffi::parse_ffi_string(in_value)?) }; + + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.set_icon(icon); Ok(()) }) @@ -360,17 +213,17 @@ pub extern "C" fn WFSchemaSetIcon( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaSetBehavior( in_schema: in_param_ty!(WFToken), - in_value: in_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_schema: WFToken, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; - + in_value: in_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_schema: WFToken, in_value: WFStrPtr| { let behavior = if in_value.is_null() { None } else { Some(cstr_ffi::parse_ffi_string(in_value)?) }; + + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.set_behavior(behavior); Ok(()) }) @@ -379,13 +232,13 @@ pub extern "C" fn WFSchemaSetBehavior( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaAddStr( in_schema: in_param_ty!(WFToken), - in_name: in_param_ty!(WFCString), - in_value: in_param_ty!(WFCString), -) -> bool { + in_name: in_param_ty!(WFStrPtr), + in_value: in_param_ty!(WFStrPtr), +) -> WFError { cffi_wrapper!( - |in_schema: WFToken, in_name: WFCString, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; + |in_schema: WFToken, in_name: WFStrPtr, in_value: WFStrPtr| { + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.add_str( cstr_ffi::parse_ffi_string(in_name)?, cstr_ffi::parse_ffi_string(in_value)?, @@ -398,13 +251,13 @@ pub extern "C" fn WFSchemaAddStr( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaAddIcon( in_schema: in_param_ty!(WFToken), - in_name: in_param_ty!(WFCString), - in_value: in_param_ty!(WFCString), -) -> bool { + in_name: in_param_ty!(WFStrPtr), + in_value: in_param_ty!(WFStrPtr), +) -> WFError { cffi_wrapper!( - |in_schema: WFToken, in_name: WFCString, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; + |in_schema: WFToken, in_name: WFStrPtr, in_value: WFStrPtr| { + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.add_icon( cstr_ffi::parse_ffi_string(in_name)?, cstr_ffi::parse_ffi_string(in_value)?, @@ -417,13 +270,13 @@ pub extern "C" fn WFSchemaAddIcon( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaAddBehavior( in_schema: in_param_ty!(WFToken), - in_name: in_param_ty!(WFCString), - in_value: in_param_ty!(WFCString), -) -> bool { + in_name: in_param_ty!(WFStrPtr), + in_value: in_param_ty!(WFStrPtr), +) -> WFError { cffi_wrapper!( - |in_schema: WFToken, in_name: WFCString, in_value: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; + |in_schema: WFToken, in_name: WFStrPtr, in_value: WFStrPtr| { + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.add_behavior( cstr_ffi::parse_ffi_string(in_name)?, cstr_ffi::parse_ffi_string(in_value)?, @@ -436,18 +289,18 @@ pub extern "C" fn WFSchemaAddBehavior( #[unsafe(no_mangle)] pub extern "C" fn WFSchemaAddExt( in_schema: in_param_ty!(WFToken), - in_ext: in_param_ty!(WFCString), - in_ext_name: in_param_ty!(WFCString), - in_ext_icon: in_param_ty!(WFCString), - in_ext_behavior: in_param_ty!(WFCString), -) -> bool { + in_ext: in_param_ty!(WFStrPtr), + in_ext_name: in_param_ty!(WFStrPtr), + in_ext_icon: in_param_ty!(WFStrPtr), + in_ext_behavior: in_param_ty!(WFStrPtr), +) -> WFError { cffi_wrapper!(|in_schema: WFToken, - in_ext: WFCString, - in_ext_name: WFCString, - in_ext_icon: WFCString, - in_ext_behavior: WFCString| { - let mut pool = pull_writer!(SCHEMA_POOL)?; - let schema = pool.get_mut(in_schema)?; + in_ext: WFStrPtr, + in_ext_name: WFStrPtr, + in_ext_icon: WFStrPtr, + in_ext_behavior: WFStrPtr| { + let pool = &CONTEXT.schema_pool; + let mut schema = pool.get_mut(in_schema)?; schema.add_ext( cstr_ffi::parse_ffi_string(in_ext)?, cstr_ffi::parse_ffi_string(in_ext_name)?, @@ -466,21 +319,21 @@ pub extern "C" fn WFSchemaAddExt( pub extern "C" fn WFProgramCreate( in_schema: in_param_ty!(WFToken), out_program: out_param_ty!(WFToken), -) -> bool { +) -> WFError { cffi_wrapper!(|in_schema: WFToken| -> (out_program: WFToken) { - let mut pool = pull_writer!(SCHEMA_POOL)?; + let pool = &CONTEXT.schema_pool; let schema = pool.pop(in_schema)?; - let mut pool = pull_writer!(PROGRAM_POOL)?; + let pool = &CONTEXT.program_pool; let program = Program::new(schema)?; - Ok(pool.allocate(program)?) + Ok(pool.allocate(program)) }) } #[unsafe(no_mangle)] -pub extern "C" fn WFProgramDestroy(in_program: in_param_ty!(WFToken)) -> bool { +pub extern "C" fn WFProgramDestroy(in_program: in_param_ty!(WFToken)) -> WFError { cffi_wrapper!(|in_program: WFToken| { - let mut pool = pull_writer!(PROGRAM_POOL)?; + let pool = &CONTEXT.program_pool; Ok(pool.free(in_program)?) }) } @@ -488,15 +341,14 @@ pub extern "C" fn WFProgramDestroy(in_program: in_param_ty!(WFToken)) -> bool { #[unsafe(no_mangle)] pub extern "C" fn WFProgramResolveName( in_program: in_param_ty!(WFToken), - out_name: out_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_program: WFToken| -> (out_name: WFCString) { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; + out_name: out_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_program: WFToken| -> (out_name: WFStrPtr) { + let pool = &CONTEXT.program_pool; + let program = pool.get(in_program)?; let name = program.resolve_name()?; - cstr_ffi::set_ffi_string(&name)?; - Ok(cstr_ffi::get_ffi_string()) + Ok(cstr_ffi::push_ffi_string(&name)?) }) } @@ -504,14 +356,14 @@ pub extern "C" fn WFProgramResolveName( pub extern "C" fn WFProgramResolveIcon( in_program: in_param_ty!(WFToken), out_icon_rc: out_param_ty!(WFToken), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken| -> (out_icon_rc: WFToken) { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; + let pool = &CONTEXT.program_pool; + let program = pool.get(in_program)?; let icon = program.resolve_icon()?; - let mut pool = pull_writer!(ICON_RC_POOL)?; - Ok(pool.allocate(icon)?) + let pool = &CONTEXT.icon_rc_pool; + Ok(pool.allocate(icon)) }) } @@ -519,10 +371,10 @@ pub extern "C" fn WFProgramResolveIcon( pub extern "C" fn WFProgramExtsLen( in_program: in_param_ty!(WFToken), out_len: out_param_ty!(usize), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken| -> (out_len: usize) { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; + let pool = &CONTEXT.program_pool; + let program = pool.get(in_program)?; Ok(program.exts_len()) }) } @@ -530,12 +382,12 @@ pub extern "C" fn WFProgramExtsLen( #[unsafe(no_mangle)] pub extern "C" fn WFProgramFindExt( in_program: in_param_ty!(WFToken), - in_body: in_param_ty!(WFCString), + in_body: in_param_ty!(WFStrPtr), out_index: out_param_ty!(usize), -) -> bool { - cffi_wrapper!(|in_program: WFToken, in_body: WFCString| -> (out_index: usize) { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; +) -> WFError { + cffi_wrapper!(|in_program: WFToken, in_body: WFStrPtr| -> (out_index: usize) { + let pool = &CONTEXT.program_pool; + let program = pool.get(in_program)?; let body = cstr_ffi::parse_ffi_string(in_body)?; let index = match program.find_ext(body) { @@ -551,14 +403,14 @@ pub extern "C" fn WFProgramResolveExt( in_program: in_param_ty!(WFToken), in_index: in_param_ty!(usize), out_self_ext_status: out_param_ty!(WFToken), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken, in_index: usize| -> (out_self_ext_status: WFToken) { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; + let pool = &CONTEXT.program_pool; + let program = pool.get(in_program)?; let self_ext_status = program.resolve_ext(in_index)?; - let mut pool = pull_writer!(SELF_EXT_STATUS_POOL)?; - let token = pool.allocate(self_ext_status)?; + let pool = &CONTEXT.self_ext_status_pool; + let token = pool.allocate(self_ext_status); Ok(token) }) } @@ -567,11 +419,12 @@ pub extern "C" fn WFProgramResolveExt( pub extern "C" fn WFProgramRegister( in_program: in_param_ty!(WFToken), in_scope: in_param_ty!(u32), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken, in_scope: u32| { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; let scope = resolve_enum!(WFScope, in_scope)?; + + let pool = &CONTEXT.program_pool; + let mut program = pool.get_mut(in_program)?; program.register(scope.into())?; Ok(()) }) @@ -581,11 +434,12 @@ pub extern "C" fn WFProgramRegister( pub extern "C" fn WFProgramUnregister( in_program: in_param_ty!(WFToken), in_scope: in_param_ty!(u32), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken, in_scope: u32| { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; let scope = resolve_enum!(WFScope, in_scope)?; + + let pool = &CONTEXT.program_pool; + let mut program = pool.get_mut(in_program)?; program.unregister(scope.into())?; Ok(()) }) @@ -596,11 +450,12 @@ pub extern "C" fn WFProgramIsRegistered( in_program: in_param_ty!(WFToken), in_scope: in_param_ty!(u32), out_is_registered: out_param_ty!(bool), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken, in_scope: u32| -> (out_is_registered: bool) { - let pool = pull_reader!(PROGRAM_POOL)?; - let program = pool.get(in_program)?; let scope = resolve_enum!(WFScope, in_scope)?; + + let pool = &CONTEXT.program_pool; + let program = pool.get(in_program)?; Ok(program.is_registered(scope.into())?) }) } @@ -610,11 +465,12 @@ pub extern "C" fn WFProgramLinkExt( in_program: in_param_ty!(WFToken), in_scope: in_param_ty!(u32), in_index: in_param_ty!(usize), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken, in_scope: u32, in_index: usize| { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; let scope = resolve_enum!(WFScope, in_scope)?; + + let pool = &CONTEXT.program_pool; + let mut program = pool.get_mut(in_program)?; program.link_ext(scope.into(), in_index)?; Ok(()) }) @@ -625,11 +481,12 @@ pub extern "C" fn WFProgramUnlinkExt( in_program: in_param_ty!(WFToken), in_scope: in_param_ty!(u32), in_index: in_param_ty!(usize), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken, in_scope: u32, in_index: usize| { - let mut pool = pull_writer!(PROGRAM_POOL)?; - let program = pool.get_mut(in_program)?; let scope = resolve_enum!(WFScope, in_scope)?; + + let pool = &CONTEXT.program_pool; + let mut program = pool.get_mut(in_program)?; program.unlink_ext(scope.into(), in_index)?; Ok(()) }) @@ -641,19 +498,19 @@ pub extern "C" fn WFProgramQueryExt( in_view: in_param_ty!(u32), in_index: in_param_ty!(usize), out_ext_status: out_param_ty!(WFToken), -) -> bool { +) -> WFError { cffi_wrapper!(|in_program: WFToken, in_view: u32, in_index: usize| -> (out_ext_status: WFToken) { - let pool = pull_reader!(PROGRAM_POOL)?; - let program = pool.get(in_program)?; let view = resolve_enum!(WFView, in_view)?; + let pool = &CONTEXT.program_pool; + let program = pool.get(in_program)?; let ext_status = program.query_ext(view.into(), in_index)?; let token = match ext_status { Some(ext_status) => { - let mut pool = pull_writer!(EXT_STATUS_POOL)?; - pool.allocate(ext_status)? + let pool = &CONTEXT.ext_status_pool; + pool.allocate(ext_status) }, - None => object_pool::invalid_token(), + None => WF_INVALID_TOKEN, }; Ok(token) }) @@ -664,9 +521,9 @@ pub extern "C" fn WFProgramQueryExt( // region: Extension Status #[unsafe(no_mangle)] -pub extern "C" fn WFExtStatusDestroy(in_ext_status: in_param_ty!(WFToken)) -> bool { +pub extern "C" fn WFExtStatusDestroy(in_ext_status: in_param_ty!(WFToken)) -> WFError { cffi_wrapper!(|in_ext_status: WFToken| { - let mut pool = pull_writer!(EXT_STATUS_POOL)?; + let pool = &CONTEXT.ext_status_pool; Ok(pool.free(in_ext_status)?) }) } @@ -674,14 +531,13 @@ pub extern "C" fn WFExtStatusDestroy(in_ext_status: in_param_ty!(WFToken)) -> bo #[unsafe(no_mangle)] pub extern "C" fn WFExtStatusGetName( in_ext_status: in_param_ty!(WFToken), - out_name: out_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_ext_status: WFToken| -> (out_name: WFCString) { - let pool = pull_reader!(EXT_STATUS_POOL)?; + out_name: out_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_ext_status: WFToken| -> (out_name: WFStrPtr) { + let pool = &CONTEXT.ext_status_pool; let ext_status = pool.get(in_ext_status)?; - cstr_ffi::set_ffi_string(ext_status.get_name())?; - Ok(cstr_ffi::get_ffi_string()) + Ok(cstr_ffi::push_ffi_string(ext_status.get_name())?) }) } @@ -689,9 +545,9 @@ pub extern "C" fn WFExtStatusGetName( pub extern "C" fn WFExtStatusGetIcon( in_ext_status: in_param_ty!(WFToken), out_icon: out_param_ty!(WFHICON), -) -> bool { +) -> WFError { cffi_wrapper!(|in_ext_status: WFToken| -> (out_icon: WFHICON) { - let pool = pull_reader!(EXT_STATUS_POOL)?; + let pool = &CONTEXT.ext_status_pool; let ext_status = pool.get(in_ext_status)?; let icon = ext_status.get_icon(); @@ -704,9 +560,9 @@ pub extern "C" fn WFExtStatusGetIcon( // region: Self Extension Status #[unsafe(no_mangle)] -pub extern "C" fn WFSelfExtStatusDestroy(in_self_ext_status: in_param_ty!(WFToken)) -> bool { +pub extern "C" fn WFSelfExtStatusDestroy(in_self_ext_status: in_param_ty!(WFToken)) -> WFError { cffi_wrapper!(|in_self_ext_status: WFToken| { - let mut pool = pull_writer!(SELF_EXT_STATUS_POOL)?; + let pool = &CONTEXT.self_ext_status_pool; Ok(pool.free(in_self_ext_status)?) }) } @@ -714,14 +570,13 @@ pub extern "C" fn WFSelfExtStatusDestroy(in_self_ext_status: in_param_ty!(WFToke #[unsafe(no_mangle)] pub extern "C" fn WFSelfExtStatusGetName( in_self_ext_status: in_param_ty!(WFToken), - out_name: out_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_self_ext_status: WFToken| -> (out_name: WFCString) { - let pool = pull_reader!(SELF_EXT_STATUS_POOL)?; + out_name: out_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_self_ext_status: WFToken| -> (out_name: WFStrPtr) { + let pool = &CONTEXT.self_ext_status_pool; let self_ext_status = pool.get(in_self_ext_status)?; - cstr_ffi::set_ffi_string(self_ext_status.get_name())?; - Ok(cstr_ffi::get_ffi_string()) + Ok(cstr_ffi::push_ffi_string(self_ext_status.get_name())?) }) } @@ -729,9 +584,9 @@ pub extern "C" fn WFSelfExtStatusGetName( pub extern "C" fn WFSelfExtStatusGetIcon( in_self_ext_status: in_param_ty!(WFToken), out_icon: out_param_ty!(WFHICON), -) -> bool { +) -> WFError { cffi_wrapper!(|in_self_ext_status: WFToken| -> (out_icon: WFHICON) { - let pool = pull_reader!(SELF_EXT_STATUS_POOL)?; + let pool = &CONTEXT.self_ext_status_pool; let self_ext_status = pool.get(in_self_ext_status)?; let icon = self_ext_status.get_icon(); @@ -742,28 +597,26 @@ pub extern "C" fn WFSelfExtStatusGetIcon( #[unsafe(no_mangle)] pub extern "C" fn WFSelfExtStatusGetExt( in_self_ext_status: in_param_ty!(WFToken), - out_inner: out_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_self_ext_status: WFToken| -> (out_inner: WFCString) { - let pool = pull_reader!(SELF_EXT_STATUS_POOL)?; + out_inner: out_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_self_ext_status: WFToken| -> (out_inner: WFStrPtr) { + let pool = &CONTEXT.self_ext_status_pool; let self_ext_status = pool.get(in_self_ext_status)?; - cstr_ffi::set_ffi_string(self_ext_status.get_ext())?; - Ok(cstr_ffi::get_ffi_string()) + Ok(cstr_ffi::push_ffi_string(self_ext_status.get_ext())?) }) } #[unsafe(no_mangle)] pub extern "C" fn WFSelfExtStatusGetDottedExt( in_self_ext_status: in_param_ty!(WFToken), - out_inner: out_param_ty!(WFCString), -) -> bool { - cffi_wrapper!(|in_self_ext_status: WFToken| -> (out_inner: WFCString) { - let pool = pull_reader!(SELF_EXT_STATUS_POOL)?; + out_inner: out_param_ty!(WFStrPtr), +) -> WFError { + cffi_wrapper!(|in_self_ext_status: WFToken| -> (out_inner: WFStrPtr) { + let pool = &CONTEXT.self_ext_status_pool; let self_ext_status = pool.get(in_self_ext_status)?; - cstr_ffi::set_ffi_string(self_ext_status.get_dotted_ext().as_str())?; - Ok(cstr_ffi::get_ffi_string()) + Ok(cstr_ffi::push_ffi_string(self_ext_status.get_dotted_ext().as_str())?) }) } @@ -772,9 +625,9 @@ pub extern "C" fn WFSelfExtStatusGetDottedExt( // region: Icon Resource #[unsafe(no_mangle)] -pub extern "C" fn WFIconRcDestroy(in_icon_rc: in_param_ty!(WFToken)) -> bool { +pub extern "C" fn WFIconRcDestroy(in_icon_rc: in_param_ty!(WFToken)) -> WFError { cffi_wrapper!(|in_icon_rc: WFToken| { - let mut pool = pull_writer!(ICON_RC_POOL)?; + let pool = &CONTEXT.icon_rc_pool; Ok(pool.free(in_icon_rc)?) }) } @@ -783,9 +636,9 @@ pub extern "C" fn WFIconRcDestroy(in_icon_rc: in_param_ty!(WFToken)) -> bool { pub extern "C" fn WFIconRcGetIcon( in_icon_rc: in_param_ty!(WFToken), out_icon: out_param_ty!(WFHICON), -) -> bool { +) -> WFError { cffi_wrapper!(|in_icon_rc: WFToken| -> (out_icon: WFHICON) { - let pool = pull_reader!(ICON_RC_POOL)?; + let pool = &CONTEXT.icon_rc_pool; let icon_rc = pool.get(in_icon_rc)?; Ok(icon_rc.get_icon()) diff --git a/wfassoc-cdylib/src/object_pool.rs b/wfassoc-cdylib/src/object_pool.rs deleted file mode 100644 index 380cc50..0000000 --- a/wfassoc-cdylib/src/object_pool.rs +++ /dev/null @@ -1,95 +0,0 @@ -//! When exporting resources for C interface, resource management and ownership are important things. -//! In this dynamic library, we hold all resources' ownership in Rust world, -//! and only expose a token for C code manipulation. -//! -//! We need to create a container for holding all resources and providing corresponding operations. -//! So we introduce [ObjectPool] in this module for this purpose. -use slotmap::{DefaultKey, Key, KeyData, SlotMap}; -use thiserror::Error as TeError; - -/// Error occurs when operating with [ObjectPool]. -#[derive(Debug, TeError)] -pub enum Error { - #[error("given token is not presented in object pool")] - NoSuchToken, -} - -/// The token for fetching object in [ObjectPool]. -pub type Token = u64; - -/// Get the invalid token. -/// -/// Invalid token is always invalid for fetching object in pool, -/// And can be useful in FFI scenario. -pub fn invalid_token() -> Token { - DefaultKey::null().data().as_ffi() -} - -/// A pool for managing objects with unique tokens. -/// -/// It is highly suggested to use this pool with [std::sync::RwLock] guard. -pub struct ObjectPool { - objs: SlotMap, -} - -impl ObjectPool { - /// Create a new [ObjectPool]. - pub fn new() -> Self { - Self { - objs: SlotMap::new(), - } - } - - /// Convert [slotmap] crate's [DefaultKey] to our [Token]. - fn key_to_token(key: &DefaultKey) -> Token { - key.data().as_ffi() - } - - /// Convert our [Token] to [slotmap] crate's [DefaultKey]. - fn token_to_key(token: Token) -> DefaultKey { - DefaultKey::from(KeyData::from_ffi(token)) - } - - /// Put given object into the pool and return its token. - /// - /// The ownership of given object is transferred to the pool. - pub fn allocate(&mut self, value: T) -> Result { - let key = self.objs.insert(value); - Ok(Self::key_to_token(&key)) - } - - /// Free the object in the pool corresponding to the given token. - pub fn free(&mut self, token: Token) -> Result<(), Error> { - let _ = self.pop(token)?; - Ok(()) - } - - /// Remove the object from the pool corresponding to the given token and return it. - /// - /// The ownership of the object is transferred to the caller. - pub fn pop(&mut self, token: Token) -> Result { - match self.objs.remove(Self::token_to_key(token)) { - Some(obj) => Ok(obj), - None => Err(Error::NoSuchToken), - } - } - - /// Clear all objects in the pool. - pub fn clear(&mut self) -> () { - self.objs.clear(); - } - - /// Get a reference to the object in the pool corresponding to the given token. - pub fn get(&self, token: Token) -> Result<&T, Error> { - self.objs - .get(Self::token_to_key(token)) - .ok_or(Error::NoSuchToken) - } - - /// Get a mutable reference to the object in the pool corresponding to the given token. - pub fn get_mut(&mut self, token: Token) -> Result<&mut T, Error> { - self.objs - .get_mut(Self::token_to_key(token)) - .ok_or(Error::NoSuchToken) - } -} diff --git a/wfassoc-cdylib/src/ffi_types.rs b/wfassoc-cdylib/src/wrapper.rs similarity index 85% rename from wfassoc-cdylib/src/ffi_types.rs rename to wfassoc-cdylib/src/wrapper.rs index aa5e61e..014c6b4 100644 --- a/wfassoc-cdylib/src/ffi_types.rs +++ b/wfassoc-cdylib/src/wrapper.rs @@ -1,19 +1,8 @@ -//! The module including all FFI types used by this crate, except string type. -//! For string type, see also [crate::cstr_ffi]. +//! The module including all wrapper types for FFI. -use std::ffi::{c_void, c_char}; +use std::ffi::c_void; use num_enum::TryFromPrimitive; -// region: Misc Types and Constants - -/// The type representing the raw pointer to immutable C-style NUL-terminated string. -pub type CStyleString = *const c_char; - -/// The invalid value of index. -pub const INVALID_INDEX: usize = usize::MAX; - -// endregion - // region: HICON /// The type representing Win32 HICON handle. @@ -75,3 +64,10 @@ impl From for wfassoc::View { } // endregion + +// region: Misc Types and Constants + +/// The invalid value of index. +pub const INVALID_INDEX: usize = usize::MAX; + +// endregion diff --git a/wfassoc-exec/Cargo.toml b/wfassoc-exec/Cargo.toml index 6bbee72..6d372a3 100644 --- a/wfassoc-exec/Cargo.toml +++ b/wfassoc-exec/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["yyc12345"] edition = "2024" description = "The executable configuring Windows file assocation according to user given profile and request." -license = "SPDX:MIT" +license = "MIT" [dependencies] thiserror = { workspace = true } diff --git a/wfassoc/Cargo.toml b/wfassoc/Cargo.toml index d8017b7..0c7c45c 100644 --- a/wfassoc/Cargo.toml +++ b/wfassoc/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["yyc12345"] edition = "2024" description = "The library reading or manipulating Windows file assocation." -license = "SPDX:MIT" +license = "MIT" [dependencies] thiserror = { workspace = true }