Skip to content

Commit

Permalink
Use single implementation for resource type open
Browse files Browse the repository at this point in the history
  • Loading branch information
br-sk authored and filmor committed Aug 5, 2024
1 parent bf7a8c3 commit 18aafd8
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions rustler/src/resource/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ unsafe extern "C" fn resource_dyncall<T: Resource>(
res.dyncall(env, call_data);
}

#[cfg(not(feature = "nif_version_2_16"))]
pub unsafe fn open_resource_type(
env: *mut ErlNifEnv,
name: &[u8],
Expand All @@ -212,7 +211,7 @@ pub unsafe fn open_resource_type(

let res = {
let mut tried = MaybeUninit::uninit();
rustler_sys::enif_open_resource_type_x(env, name_p, &init, flags, tried.as_mut_ptr())
OPEN_RESOURCE_TYPE(env, name_p, &init, flags, tried.as_mut_ptr())
};

if res.is_null() {
Expand All @@ -222,29 +221,19 @@ pub unsafe fn open_resource_type(
}
}

#[cfg(feature = "nif_version_2_16")]
pub unsafe fn open_resource_type(
env: *mut ErlNifEnv,
name: &[u8],
init: ErlNifResourceTypeInit,
flags: ErlNifResourceFlags,
) -> Option<*const ErlNifResourceType> {
// Panic if name is not null-terminated.
assert_eq!(name.last().cloned(), Some(0u8));

let name_p = name.as_ptr() as *const c_char;
type OpenResourceTypeFn = unsafe extern "C" fn(
*mut ErlNifEnv,
*const c_char,
*const ErlNifResourceTypeInit,
ErlNifResourceFlags,
*mut ErlNifResourceFlags,
) -> *const ErlNifResourceType;

let res = {
let mut tried = MaybeUninit::uninit();
rustler_sys::enif_init_resource_type(env, name_p, &init, flags, tried.as_mut_ptr())
};
#[cfg(feature = "nif_version_2_16")]
static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = rustler_sys::enif_init_resource_type;

if res.is_null() {
None
} else {
Some(res)
}
}
#[cfg(not(feature = "nif_version_2_16"))]
static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = rustler_sys::enif_open_resource_type_x;

const fn max(i: i32, j: i32) -> i32 {
if i > j {
Expand Down

0 comments on commit 18aafd8

Please sign in to comment.