-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rustler_sys: add 'nif_compare_pids' * rustler: add (Partial)Eq/Ord for LocalPid * rustler_tests: add tests for LocalPid cmp/eq * sys: define 'enif_compare_pids' to behave like macro in C code * tests: add unit test with equality check for local pids
- Loading branch information
Showing
6 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use std::cmp::Ordering; | ||
|
||
use rustler::LocalPid; | ||
|
||
#[rustler::nif] | ||
pub fn compare_local_pids(lhs: LocalPid, rhs: LocalPid) -> i32 { | ||
match lhs.cmp(&rhs) { | ||
Ordering::Less => -1, | ||
Ordering::Equal => 0, | ||
Ordering::Greater => 1, | ||
} | ||
} | ||
|
||
#[rustler::nif] | ||
pub fn are_equal_local_pids(lhs: LocalPid, rhs: LocalPid) -> bool { | ||
lhs == rhs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule RustlerTest.LocalPidTest do | ||
use ExUnit.Case, async: true | ||
|
||
def make_pid() do | ||
{:ok, pid} = Task.start(fn -> :ok end) | ||
pid | ||
end | ||
|
||
def compare(lhs, rhs) do | ||
cond do | ||
lhs < rhs -> -1 | ||
lhs == rhs -> 0 | ||
lhs > rhs -> 1 | ||
end | ||
end | ||
|
||
test "local pid comparison" do | ||
# We make sure that the code we have in rust code matches the comparisons | ||
# that are performed in the BEAM code. | ||
pids = for _ <- 1..3, do: make_pid() | ||
|
||
for lhs <- pids, rhs <- pids do | ||
assert RustlerTest.compare_local_pids(lhs, rhs) == compare(lhs, rhs) | ||
end | ||
end | ||
|
||
test "local pid equality" do | ||
pids = for _ <- 1..3, do: make_pid() | ||
|
||
for lhs <- pids, rhs <- pids do | ||
expected = lhs == rhs | ||
assert RustlerTest.are_equal_local_pids(lhs, rhs) == expected | ||
end | ||
end | ||
end |