feat: fix LazyLock issue and add something in cdylib
This commit is contained in:
@@ -12,6 +12,9 @@ use std::cell::RefCell;
|
||||
use std::ffi::{CStr, CString, c_char};
|
||||
use thiserror::Error as TeError;
|
||||
|
||||
/// The type representing the raw pointer to immutable C-style NUL-terminated string.
|
||||
pub type CStyleString = *const c_char;
|
||||
|
||||
// region: Error
|
||||
|
||||
/// Error occurs in this crate.
|
||||
@@ -31,13 +34,13 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
// endregion
|
||||
|
||||
// region: FFI String
|
||||
// region: String Cache for Exposing
|
||||
|
||||
struct FfiString {
|
||||
struct StringCache {
|
||||
msg: CString,
|
||||
}
|
||||
|
||||
impl FfiString {
|
||||
impl StringCache {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
msg: CString::new("").expect("empty string must be valid for CString"),
|
||||
@@ -49,7 +52,7 @@ impl FfiString {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_msg(&self) -> *const c_char {
|
||||
pub fn get_msg(&self) -> CStyleString {
|
||||
self.msg.as_ptr()
|
||||
}
|
||||
|
||||
@@ -63,7 +66,7 @@ impl FfiString {
|
||||
// region: Exposed Functions
|
||||
|
||||
thread_local! {
|
||||
static STRING_CACHE: RefCell<FfiString> = RefCell::new(FfiString::new());
|
||||
static STRING_CACHE: RefCell<StringCache> = RefCell::new(StringCache::new());
|
||||
}
|
||||
|
||||
/// Set thread local string exposed for C code.
|
||||
@@ -74,7 +77,7 @@ pub fn set_ffi_string(msg: &str) -> Result<()> {
|
||||
}
|
||||
|
||||
/// Get const pointer to thread local string exposed for C code.
|
||||
pub fn get_ffi_string() -> *const c_char {
|
||||
pub fn get_ffi_string() -> CStyleString {
|
||||
STRING_CACHE.with(|e| e.borrow().get_msg())
|
||||
}
|
||||
|
||||
@@ -88,7 +91,7 @@ pub fn clear_ffi_string() {
|
||||
}
|
||||
|
||||
/// Parse string given by C code into Rust string.
|
||||
pub fn parse_ffi_string<'a>(ptr: *const c_char) -> Result<&'a str> {
|
||||
pub fn parse_ffi_string<'a>(ptr: CStyleString) -> Result<&'a str> {
|
||||
if ptr.is_null() {
|
||||
Err(Error::NullPtr)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user