Skip to content

Commit

Permalink
sys: define 'enif_compare_pids' to behave like macro in C code
Browse files Browse the repository at this point in the history
  • Loading branch information
hengermax committed May 28, 2024
1 parent f1ac69a commit 460180f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
10 changes: 3 additions & 7 deletions rustler/src/types/local_pid.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::wrapper::{pid, ErlNifPid};
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
use std::mem::MaybeUninit;
use std::cmp::Ordering;
use std::mem::MaybeUninit;

#[derive(Copy, Clone)]
pub struct LocalPid {
Expand Down Expand Up @@ -37,27 +37,23 @@ impl Encoder for LocalPid {
}
}

#[cfg(feature="nif_version_2_15")]
impl PartialEq for LocalPid {
fn eq(&self, other: &Self) -> bool {
unsafe{ rustler_sys::enif_compare_pids(self.as_c_arg(), other.as_c_arg()) == 0 }
unsafe { rustler_sys::enif_compare_pids(self.as_c_arg(), other.as_c_arg()) == 0 }
}
}

#[cfg(feature="nif_version_2_15")]
impl Eq for LocalPid {}

#[cfg(feature="nif_version_2_15")]
impl PartialOrd for LocalPid {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

#[cfg(feature="nif_version_2_15")]
impl Ord for LocalPid {
fn cmp(&self, other: &Self) -> Ordering {
let cmp = unsafe{ rustler_sys::enif_compare_pids(self.as_c_arg(), other.as_c_arg()) };
let cmp = unsafe { rustler_sys::enif_compare_pids(self.as_c_arg(), other.as_c_arg()) };
cmp.cmp(&0)
}
}
Expand Down
5 changes: 0 additions & 5 deletions rustler_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,11 +858,6 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
"enif_make_monitor_term",
"env: *mut ErlNifEnv, mon: *const ErlNifMonitor",
);
b.func(
"c_int",
"enif_compare_pids",
"pid1: *const ErlNifPid, pid2: *const ErlNifPid"
);
}

// 2.16 was introduced in OTP 24
Expand Down
6 changes: 6 additions & 0 deletions rustler_sys/src/rustler_sys_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ pub unsafe fn enif_make_pid(_env: *mut ErlNifEnv, pid: ErlNifPid) -> ERL_NIF_TER
pid.pid
}

/// See [enif_compare_pids](http://erlang.org/doc/man/erl_nif.html#enif_compare_pids) in the Erlang docs
pub unsafe fn enif_compare_pids(pid1: *const ErlNifPid, pid2: *const ErlNifPid) -> c_int {
// Mimics the implementation of the enif_compare_pids macro
enif_compare((*pid1).pid, (*pid2).pid)
}

/// See [ErlNifSysInfo](http://www.erlang.org/doc/man/erl_nif.html#ErlNifSysInfo) in the Erlang docs.
#[allow(missing_copy_implementations)]
#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion rustler_tests/native/rustler_test/src/test_local_pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ pub fn compare_local_pids(lhs: LocalPid, rhs: LocalPid) -> i32 {
#[rustler::nif]
pub fn are_equal_local_pids(lhs: LocalPid, rhs: LocalPid) -> bool {
lhs == rhs
}
}

0 comments on commit 460180f

Please sign in to comment.