Skip to content

Commit

Permalink
Revert to Elixir-like behaviour (unit maps to nil), fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Jan 9, 2024
1 parent 4378329 commit a7f4398
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
7 changes: 1 addition & 6 deletions rustler/src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,7 @@ impl<'a> ser::Serializer for Serializer<'a> {
/// Serializes unit (empty tuple) as `nil`.
#[inline]
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Ok(unsafe {
Term::new(
self.env,
rustler_sys::enif_make_tuple!(self.env.as_c_arg(), 0),
)
})
Ok(atoms::nil().to_term(self.env))
}

#[inline]
Expand Down
5 changes: 4 additions & 1 deletion rustler_tests/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ defmodule RustlerTest.Mixfile do
end

defp deps do
[{:rustler, path: "../rustler_mix", runtime: false}]
[
{:rustler, path: "../rustler_mix", runtime: false},
{:map_diff, version: "~> 1.3"}
]
end
end
2 changes: 1 addition & 1 deletion rustler_tests/native/dynamic_load/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn initialize_dataset(mut asset_path: PathBuf) {
asset_path.push("demo_dataset.txt");

// https://github.com/elixir-lsp/elixir-ls/issues/604
eprintln!("Loading dataset from {:?}.", &asset_path);
// eprintln!("Loading dataset from {:?}.", &asset_path);

let data = read_to_string(asset_path).unwrap().into_boxed_str();
let data = Some(data);
Expand Down
6 changes: 3 additions & 3 deletions rustler_tests/native/rustler_serde_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod types;

use crate::types::Animal;
use rustler::serde::{atoms, from_term, to_term, Deserializer, Error, Serializer};
use rustler::SerdeTerm;
use rustler::{types::tuple, Encoder, Env, NifResult, Term};

init! {
Expand All @@ -34,9 +35,8 @@ init! {

/// Implements the README example.
#[nif]
pub fn readme<'a>(env: Env<'a>, arg: Term<'a>) -> NifResult<Term<'a>> {
let animal: Animal = from_term(arg)?;
println!("\n deserialized animal from README example: {:?}", animal);
pub fn readme<'a>(env: Env<'a>, SerdeTerm(animal): SerdeTerm<Animal>) -> NifResult<Term<'a>> {
// println!("\n deserialized animal from README example: {:?}", animal);
to_term(env, animal).map_err(|err| err.into())
}

Expand Down
17 changes: 13 additions & 4 deletions rustler_tests/test/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ defmodule SerdeRustlerTests.Json.JsonTest do

assert expected == actual, ~s"""
DECODING ERROR :: #{ctx[:filename]}
expected: #{inspect(expected)}
actual: #{inspect(actual)}
diff: #{diff(actual, expected)}
"""
end

Expand All @@ -150,8 +149,18 @@ defmodule SerdeRustlerTests.Json.JsonTest do

assert expected == actual, ~s"""
ENCODING ERROR :: #{ctx[:filename]}
expected: #{inspect(expected)}
actual: #{inspect(actual)}
diff: #{diff(actual, expected)}
"""
end

defp diff([actual | _], [expected | _]) do
diff(actual, expected)
end

defp diff(actual, expected) do
MapDiff.diff(expected, actual)
|> Map.delete(:value)
|> Enum.map(fn {k, v} -> "#{Atom.to_string(k)}: #{inspect(v)}" end)
|> Enum.join("\n")
end
end
4 changes: 2 additions & 2 deletions rustler_tests/test/serde_rustler_tests_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ defmodule SerdeRustlerTests.NifTest do

describe "Unit Types:" do
test "unit", ctx do
run_tests("unit", {}, ctx)
run_tests("unit", nil, ctx)
end

test "unit struct", ctx do
run_tests("unit struct", {}, ctx)
run_tests("unit struct", nil, ctx)
end

test "unit variant", ctx do
Expand Down

0 comments on commit a7f4398

Please sign in to comment.