feat: update cdylib cstr ffi module
This commit is contained in:
@@ -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<StringCache> = 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) }
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user