prefer &mut __Userdata over *mut UserData

This commit is contained in:
do butterflies cry? 2026-03-25 01:13:31 +10:00
parent bc5a9cc3aa
commit 648aadf5ab
Signed by: cry
GPG key ID: F68745A836CA0412
2 changed files with 7 additions and 25 deletions

View file

@ -271,7 +271,7 @@ impl ErrorContext {
pub(super) fn get_nix_err_name(&self) -> Option<String> {
unsafe {
// NOTE: an Err here only occurs when "Last error was not a nix error"
wrap::nix_string_callback!(|callback, userdata: *mut __UserData, ctx: &ErrorContext| {
wrap::nix_string_callback!(|callback, userdata: &mut __UserData, ctx: &ErrorContext| {
sys::nix_err_name(ctx.as_ptr(), self.as_ptr(), Some(callback), userdata)
})
.ok()
@ -316,7 +316,7 @@ impl ErrorContext {
pub(super) fn get_nix_err_info_msg(&self) -> Option<String> {
unsafe {
// NOTE: an Err here only occurs when "Last error was not a nix error"
wrap::nix_string_callback!(|callback, user_data, ctx| {
wrap::nix_string_callback!(|callback, user_data, ctx: &ErrorContext| {
sys::nix_err_info_msg(ctx.as_ptr(), self.as_ptr(), Some(callback), user_data)
})
.ok()

View file

@ -103,18 +103,17 @@ macro_rules! nix_callback {
unsafe extern "C" fn __wrapper_callback(
$($( $pre: $pre_ty, )*)?
$userdata: *mut ::std::ffi::c_void,
$userdata: &mut __UserData,
$($( $post: $post_ty, )*)?
) {
let ud = unsafe { &mut *($userdata as *mut __UserData) };
let stored_retval = &raw mut ud.retval;
let stored_retval = &raw mut $userdata.retval;
let retval = unsafe {
__captured_fn(
$($( $pre, )*)?
ud,
$userdata,
$($( $post, )*)?
);
)
};
unsafe {
@ -122,29 +121,12 @@ macro_rules! nix_callback {
};
}
// fn __captured_function(
// callback: unsafe extern "C" fn(
// $userdata: *mut ::std::ffi::c_void,
// $(
// $arg_name: $arg_type,
// )*
// ),
// state: *mut __UserData,
// ctx: &$crate::errors::ErrorContext,
// ) {
// $function(callback, state, ctx);
// }
let mut __ctx: $crate::errors::ErrorContext = $crate::errors::ErrorContext::new();
let mut __state: ::std::mem::MaybeUninit<__UserData> = ::std::mem::MaybeUninit::uninit();
$function(__wrapper_callback, __state.as_mut_ptr(), &__ctx);
$function(__wrapper_callback, &mut __state, &__ctx);
__ctx.pop().and_then(|_| unsafe { __state.assume_init().retval })
// add type annotations for compiler
// let __result: $ret = __ctx.pop().and_then(|_| unsafe { __state.assume_init().retval });
// __result
}};
}
pub(crate) use nix_callback;