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,
|
//! 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.
|
//! once they get it, they must dupliate it immediately before any futher calling to this dynamic library.
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::ffi::{CString, c_char};
|
use std::ffi::{CString, CStr, c_char};
|
||||||
|
|
||||||
struct StringCache {
|
struct StringCache {
|
||||||
msg: CString,
|
msg: CString,
|
||||||
@@ -39,21 +39,28 @@ thread_local! {
|
|||||||
static STRING_CACHE: RefCell<StringCache> = RefCell::new(StringCache::new());
|
static STRING_CACHE: RefCell<StringCache> = RefCell::new(StringCache::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set thread local string cache.
|
/// Set thread local string exposed for C code.
|
||||||
pub fn set_string_cache(msg: &str) {
|
pub fn set_ffi_string(msg: &str) {
|
||||||
STRING_CACHE.with(|e| {
|
STRING_CACHE.with(|e| {
|
||||||
e.borrow_mut().set_msg(msg);
|
e.borrow_mut().set_msg(msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get const pointer to thread local string cache for outside program visiting.
|
/// Get const pointer to thread local string exposed for C code.
|
||||||
pub fn get_string_cache() -> *const c_char {
|
pub fn get_ffi_string() -> *const c_char {
|
||||||
STRING_CACHE.with(|e| e.borrow().get_msg())
|
STRING_CACHE.with(|e| e.borrow().get_msg())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clear thread local string cache.
|
/// Clear thread local string exposed for C code.
|
||||||
pub fn clear_string_cache() {
|
///
|
||||||
|
/// This function usually should be called at the beginning of every exposed C functions.
|
||||||
|
pub fn clear_ffi_string() {
|
||||||
STRING_CACHE.with(|e| {
|
STRING_CACHE.with(|e| {
|
||||||
e.borrow_mut().clear_msg();
|
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 std::ffi::{CString, c_char};
|
||||||
use thiserror::Error as TeError;
|
use thiserror::Error as TeError;
|
||||||
|
|
||||||
mod object_pool;
|
mod object_pool;
|
||||||
mod last_error;
|
mod last_error;
|
||||||
mod string_cache;
|
mod cstr_ffi;
|
||||||
mod wrapper;
|
mod wrapper;
|
||||||
|
|
||||||
// region: Error
|
// region: Error
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ pub enum Error {
|
|||||||
#[error("given extension name {0} is not presented in application")]
|
#[error("given extension name {0} is not presented in application")]
|
||||||
BadExtName(String),
|
BadExtName(String),
|
||||||
/// Find star (*) extension name with other extension names when converting extension name to index
|
/// 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),
|
ExclusiveStarExtName(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user