Skip to content

Commit

Permalink
Allow arbitrary ASCII strings as NIF names
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Feb 15, 2024
1 parent 8e26422 commit f118abb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions rustler_codegen/src/nif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ pub fn transcoder_decorator(nif_attributes: NifAttributes, fun: syn::ItemFn) ->
let argument_names = create_function_params(inputs.clone());
let erl_func_name = nif_attributes
.custom_name
.map(|n| syn::Ident::new(n.value().as_str(), Span::call_site()))
.unwrap_or_else(|| name.clone());
.map(|n| n.value().to_string())
.unwrap_or_else(|| name.clone().to_string());

if !erl_func_name.is_ascii() {
panic!("Only ASCII strings are supported as function names");
}

quote! {
#[allow(non_camel_case_types)]
Expand Down
2 changes: 1 addition & 1 deletion rustler_tests/lib/rustler_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ defmodule RustlerTest do
def raise_term_with_atom_error(), do: err()
def term_with_tuple_error(), do: err()

def nif_attrs_can_rename(), do: err()
def nif_attrs_can_rename!(), do: err()

def add_from_tuple(_tuple), do: err()
def add_one_to_tuple(_tuple), do: err()
Expand Down
2 changes: 1 addition & 1 deletion rustler_tests/native/rustler_test/src/test_nif_attrs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[rustler::nif(name = "nif_attrs_can_rename")]
#[rustler::nif(name = "nif_attrs_can_rename!")]
pub fn can_rename() -> bool {
true
}
2 changes: 1 addition & 1 deletion rustler_tests/test/nif_attrs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ defmodule NifAttrsTest do
use ExUnit.Case

test "can rename a NIF with an attribute" do
assert RustlerTest.nif_attrs_can_rename()
assert RustlerTest.nif_attrs_can_rename!()
end
end

0 comments on commit f118abb

Please sign in to comment.