Skip to content

Commit 63ca3fe

Browse files
authored
Allow arbitrary function names (#593)
* Allow arbitrary ASCII strings as NIF names * Just to be sure, reject control chars
1 parent 2ae8765 commit 63ca3fe

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ versions.
1616

1717
### Changed
1818

19+
- Adjust C char types to use the proper FFI type (#592)
20+
- Allow arbitrary (ASCII) NIF function names (#593, idea and initial
21+
implementation by @KoviRobi)
22+
1923
### Removed
2024

2125
## [0.31.0] - 2024-02-13

rustler_codegen/src/nif.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ pub fn transcoder_decorator(nif_attributes: NifAttributes, fun: syn::ItemFn) ->
4848
let argument_names = create_function_params(inputs.clone());
4949
let erl_func_name = nif_attributes
5050
.custom_name
51-
.map(|n| syn::Ident::new(n.value().as_str(), Span::call_site()))
52-
.unwrap_or_else(|| name.clone());
51+
.map_or_else(|| name.to_string(), |n| n.value().to_string());
52+
53+
if !erl_func_name.is_ascii() || erl_func_name.chars().any(|x| x.is_ascii_control()) {
54+
panic!("Only non-Control ASCII strings are supported as function names");
55+
}
5356

5457
quote! {
5558
#[allow(non_camel_case_types)]
5659
pub struct #name;
5760

5861
impl rustler::Nif for #name {
59-
const NAME: *const rustler::codegen_runtime::c_char = concat!(stringify!(#erl_func_name), "\0").as_ptr() as *const rustler::codegen_runtime::c_char;
62+
const NAME: *const rustler::codegen_runtime::c_char = concat!(#erl_func_name, "\0").as_ptr() as *const rustler::codegen_runtime::c_char;
6063
const ARITY: rustler::codegen_runtime::c_uint = #arity;
6164
const FLAGS: rustler::codegen_runtime::c_uint = #flags as rustler::codegen_runtime::c_uint;
6265
const RAW_FUNC: unsafe extern "C" fn(

rustler_tests/lib/rustler_test.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ defmodule RustlerTest do
121121
def raise_term_with_atom_error(), do: err()
122122
def term_with_tuple_error(), do: err()
123123

124-
def nif_attrs_can_rename(), do: err()
124+
def nif_attrs_can_rename!(), do: err()
125125

126126
def add_from_tuple(_tuple), do: err()
127127
def add_one_to_tuple(_tuple), do: err()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[rustler::nif(name = "nif_attrs_can_rename")]
1+
#[rustler::nif(name = "nif_attrs_can_rename!")]
22
pub fn can_rename() -> bool {
33
true
44
}

rustler_tests/test/nif_attrs_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ defmodule NifAttrsTest do
22
use ExUnit.Case
33

44
test "can rename a NIF with an attribute" do
5-
assert RustlerTest.nif_attrs_can_rename()
5+
assert RustlerTest.nif_attrs_can_rename!()
66
end
77
end

0 commit comments

Comments
 (0)