1
0

feat: update bmap-rs

This commit is contained in:
2026-02-17 00:29:44 +08:00
parent 9aa5e05a03
commit 518d2c77ec

View File

@@ -5,6 +5,7 @@
use crate::bmap::{self, BMBOOL, CKID, CKSTRING, PBMVOID}; use crate::bmap::{self, BMBOOL, CKID, CKSTRING, PBMVOID};
use crate::marshaler; use crate::marshaler;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::mem::MaybeUninit;
use thiserror::Error as TeError; use thiserror::Error as TeError;
// region: Error and Result Types // region: Error and Result Types
@@ -69,15 +70,27 @@ macro_rules! bmap_exec {
}; };
} }
macro_rules! param_in {
($t:ty) => {
$t
};
}
macro_rules! param_out {
($t:ty) => {
*mut $t
};
}
macro_rules! arg_in { macro_rules! arg_in {
($v:ident) => { ($v:expr) => {
$v $v
}; };
} }
macro_rules! arg_out { macro_rules! arg_out {
($v:ident, $t:ty) => { ($v:expr, $t:ty) => {
&mut $v as *mut $t $v as param_out!($t)
}; };
} }
@@ -121,16 +134,46 @@ impl Drop for BMap {
// region: Helper // region: Helper
fn get_string_value<T>( fn get_primitive_value<O, T>(
o: &T, o: &O,
f: unsafe extern "C" fn(PBMVOID, CKID, *mut CKSTRING) -> BMBOOL, f: unsafe extern "C" fn(PBMVOID, CKID, param_out!(T)) -> BMBOOL,
) -> Result<T>
where
O: AbstractObject,
T: Sized + Copy,
{
let mut data = MaybeUninit::<T>::uninit();
bmap_exec!(f(
o.get_pointer(),
o.get_ckid(),
arg_out!(data.as_mut_ptr(), T)
));
Ok(unsafe { data.assume_init() })
}
fn set_primitive_value<O, T>(
o: &O,
f: unsafe extern "C" fn(PBMVOID, CKID, param_in!(T)) -> BMBOOL,
data: T
) -> Result<()>
where
O: AbstractObject,
T: Sized + Copy,
{
bmap_exec!(f(o.get_pointer(), o.get_ckid(), arg_in!(data)));
Ok(())
}
fn get_string_value<O>(
o: &O,
f: unsafe extern "C" fn(PBMVOID, CKID, param_out!(CKSTRING)) -> BMBOOL,
) -> Result<Option<String>> ) -> Result<Option<String>>
where where
T: AbstractObject, O: AbstractObject,
{ {
let mut data = CKSTRING::default(); // Get raw string pointer
bmap_exec!(f(o.get_pointer(), o.get_ckid(), arg_out!(data, CKSTRING))); let data = get_primitive_value(o, f)?;
// Check raw string pointer.
if data.is_null() { if data.is_null() {
Ok(None) Ok(None)
} else { } else {
@@ -138,25 +181,25 @@ where
} }
} }
fn set_string_value<T>( fn set_string_value<O>(
o: &T, o: &O,
f: unsafe extern "C" fn(PBMVOID, CKID, CKSTRING) -> BMBOOL, f: unsafe extern "C" fn(PBMVOID, CKID, param_in!(CKSTRING)) -> BMBOOL,
s: Option<&str>, s: Option<&str>,
) -> Result<()> ) -> Result<()>
where where
T: AbstractObject, O: AbstractObject,
{ {
// Buold raw string pointer.
let native = match s { let native = match s {
Some(s) => Some(unsafe { marshaler::to_native_string(s)? }), Some(s) => Some(unsafe { marshaler::to_native_string(s)? }),
None => None, None => None,
}; };
let data = match &native { let data: CKSTRING = match &native {
Some(native) => unsafe { native.as_raw() }, Some(native) => unsafe { native.as_raw() },
None => std::ptr::null_mut() as CKSTRING, None => std::ptr::null_mut() as CKSTRING,
}; };
bmap_exec!(f(o.get_pointer(), o.get_ckid(), arg_in!(data))); // Set it
set_primitive_value(o, f, data)
Ok(())
} }
// endregion // endregion