From 1a45b309e797a2a7735f4946044754a9aa1cf9b2 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 10 May 2026 16:43:25 +0800 Subject: [PATCH] feat: update cdylib cstr ffi module --- .../src/{string_cache.rs => cstr_ffi.rs} | 21 ++++++++++++------- wfassoc-cdylib/src/lib.rs | 4 ++-- wfassoc-exec/src/runner.rs | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) rename wfassoc-cdylib/src/{string_cache.rs => cstr_ffi.rs} (74%) diff --git a/wfassoc-cdylib/src/string_cache.rs b/wfassoc-cdylib/src/cstr_ffi.rs similarity index 74% rename from wfassoc-cdylib/src/string_cache.rs rename to wfassoc-cdylib/src/cstr_ffi.rs index b74d1fa..ed5b2a7 100644 --- a/wfassoc-cdylib/src/string_cache.rs +++ b/wfassoc-cdylib/src/cstr_ffi.rs @@ -9,7 +9,7 @@ //! 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::{CString, c_char}; +use std::ffi::{CString, CStr, c_char}; struct StringCache { msg: CString, @@ -39,21 +39,28 @@ thread_local! { static STRING_CACHE: RefCell = RefCell::new(StringCache::new()); } -/// Set thread local string cache. -pub fn set_string_cache(msg: &str) { +/// Set thread local string exposed for C code. +pub fn set_ffi_string(msg: &str) { STRING_CACHE.with(|e| { e.borrow_mut().set_msg(msg); }); } -/// Get const pointer to thread local string cache for outside program visiting. -pub fn get_string_cache() -> *const c_char { +/// Get const pointer to thread local string exposed for C code. +pub fn get_ffi_string() -> *const c_char { STRING_CACHE.with(|e| e.borrow().get_msg()) } -/// Clear thread local string cache. -pub fn clear_string_cache() { +/// 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: *const c_char) -> &'a CStr { + unsafe { CStr::from_ptr(ptr) } +} diff --git a/wfassoc-cdylib/src/lib.rs b/wfassoc-cdylib/src/lib.rs index 91f308f..dc20722 100644 --- a/wfassoc-cdylib/src/lib.rs +++ b/wfassoc-cdylib/src/lib.rs @@ -1,10 +1,10 @@ -use std::cell::RefCell; + use std::ffi::{CString, c_char}; use thiserror::Error as TeError; mod object_pool; mod last_error; -mod string_cache; +mod cstr_ffi; mod wrapper; // region: Error diff --git a/wfassoc-exec/src/runner.rs b/wfassoc-exec/src/runner.rs index 18a5058..f0de4c6 100644 --- a/wfassoc-exec/src/runner.rs +++ b/wfassoc-exec/src/runner.rs @@ -34,7 +34,7 @@ pub enum Error { #[error("given extension name {0} is not presented in application")] BadExtName(String), /// Find star (*) extension name with other extension names when converting extension name to index - #[error("given extension name {0} is not presented in application")] + #[error("wildcard extension name \"*\" is not allowed to be used with other extension names")] ExclusiveStarExtName(String), }