Skip to content

Commit 2ae8765

Browse files
authored
Fix type usage to use the real C types wherever applicable (#592)
* Fix types to use the correct C type everywhere * Bump rustler_sys version due to the breaking API changes
1 parent 4c27032 commit 2ae8765

File tree

12 files changed

+40
-39
lines changed

12 files changed

+40
-39
lines changed

rustler/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ nif_version_2_17 = ["nif_version_2_16", "rustler_sys/nif_version_2_17"]
2020
[dependencies]
2121
lazy_static = "1.4"
2222
rustler_codegen = { path = "../rustler_codegen", version = "0.31.0", optional = true}
23-
rustler_sys = { path = "../rustler_sys", version = "~2.3.2" }
23+
rustler_sys = { path = "../rustler_sys", version = "~2.4.0" }
2424

2525
[package.metadata.release]
2626

rustler/src/codegen_runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{Encoder, Env, OwnedBinary, Term};
88
// Names used by the `rustler::init!` macro or other generated code.
99
pub use crate::wrapper::exception::raise_exception;
1010
pub use crate::wrapper::{
11-
c_int, c_void, get_nif_resource_type_init_size, DEF_NIF_ENTRY, DEF_NIF_FUNC,
11+
c_char, c_int, c_uint, c_void, get_nif_resource_type_init_size, DEF_NIF_ENTRY, DEF_NIF_FUNC,
1212
MUTABLE_NIF_RESOURCE_HANDLE, NIF_ENV, NIF_MAJOR_VERSION, NIF_MINOR_VERSION, NIF_TERM,
1313
};
1414

@@ -73,7 +73,7 @@ impl NifReturned {
7373
args,
7474
} => rustler_sys::enif_schedule_nif(
7575
env.as_c_arg(),
76-
fun_name.as_ptr() as *const u8,
76+
fun_name.as_ptr() as *const c_char,
7777
flags as i32,
7878
fun,
7979
args.len() as i32,

rustler/src/export.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ macro_rules! rustler_export_nifs {
4444
let entry = $crate::codegen_runtime::DEF_NIF_ENTRY {
4545
major: $crate::codegen_runtime::NIF_MAJOR_VERSION,
4646
minor: $crate::codegen_runtime::NIF_MINOR_VERSION,
47-
name: concat!($name, "\x00") as *const str as *const u8,
47+
name: concat!($name, "\x00") as *const str as *const $crate::codegen_runtime::c_char,
4848
num_of_funcs: FUN_ENTRIES.len() as $crate::codegen_runtime::c_int,
4949
funcs: FUN_ENTRIES.as_ptr(),
5050
load: Some(nif_load),
5151
reload: None,
5252
upgrade: None,
5353
unload: None,
54-
vm_variant: b"beam.vanilla\x00".as_ptr(),
54+
vm_variant: b"beam.vanilla\x00".as_ptr() as *const $crate::codegen_runtime::c_char,
5555
options: 0,
5656
sizeof_ErlNifResourceTypeInit: $crate::codegen_runtime::get_nif_resource_type_init_size(),
5757
};
@@ -66,7 +66,7 @@ macro_rules! rustler_export_nifs {
6666
};
6767
(internal_item_init, ($nif_name:expr, $nif_arity:expr, $nif_fun:path, $nif_flag:expr)) => {
6868
$crate::codegen_runtime::DEF_NIF_FUNC {
69-
name: concat!($nif_name, "\x00") as *const str as *const u8,
69+
name: concat!($nif_name, "\x00") as *const str as *const $crate::codegen_runtime::c_char,
7070
arity: $nif_arity,
7171
function: {
7272
extern "C" fn nif_func(

rustler/src/nif.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::codegen_runtime::{c_int, DEF_NIF_FUNC, NIF_ENV, NIF_TERM};
1+
use crate::codegen_runtime::{c_char, c_int, c_uint, DEF_NIF_FUNC, NIF_ENV, NIF_TERM};
22

33
pub trait Nif {
4-
const NAME: *const u8;
5-
const ARITY: u32;
6-
const FLAGS: u32;
4+
const NAME: *const c_char;
5+
const ARITY: c_uint;
6+
const FLAGS: c_uint;
77
const FUNC: DEF_NIF_FUNC;
88
const RAW_FUNC: unsafe extern "C" fn(
99
nif_env: NIF_ENV,

rustler/src/wrapper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use rustler_sys::{
2626
ERL_NIF_THR_DIRTY_IO_SCHEDULER, ERL_NIF_THR_NORMAL_SCHEDULER, ERL_NIF_THR_UNDEFINED,
2727
};
2828

29-
pub use std::os::raw::{c_double, c_int, c_uchar, c_uint, c_void};
29+
pub use rustler_sys::{c_char, c_double, c_int, c_uchar, c_uint, c_void};
3030
pub type size_t = usize;
3131

3232
pub type NIF_ENV = *mut rustler_sys::ErlNifEnv;

rustler/src/wrapper/atom.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use crate::wrapper::{c_uint, NIF_ENV, NIF_TERM};
1+
use crate::wrapper::{c_char, c_uint, NIF_ENV, NIF_TERM};
22
use crate::Error;
33
use rustler_sys::ErlNifCharEncoding::ERL_NIF_LATIN1;
44

55
pub unsafe fn make_atom(env: NIF_ENV, name: &[u8]) -> NIF_TERM {
6-
rustler_sys::enif_make_atom_len(env, name.as_ptr(), name.len())
6+
rustler_sys::enif_make_atom_len(env, name.as_ptr() as *const c_char, name.len())
77
}
88

99
pub unsafe fn make_existing_atom(env: NIF_ENV, name: &[u8]) -> Option<NIF_TERM> {
1010
let mut atom_out: NIF_TERM = 0;
1111
let success = rustler_sys::enif_make_existing_atom_len(
1212
env,
13-
name.as_ptr(),
13+
name.as_ptr() as *const c_char,
1414
name.len(),
1515
&mut atom_out as *mut NIF_TERM,
1616
ERL_NIF_LATIN1,

rustler/src/wrapper/resource.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::wrapper::{
22
NifResourceDtor, NifResourceFlags, NIF_ENV, NIF_RESOURCE_HANDLE, NIF_RESOURCE_TYPE, NIF_TERM,
33
};
44

5+
use rustler_sys::c_char;
56
pub use rustler_sys::{
67
enif_alloc_resource as alloc_resource, enif_keep_resource as keep_resource,
78
enif_make_resource as make_resource, enif_release_resource as release_resource,
@@ -20,8 +21,8 @@ pub unsafe fn open_resource_type(
2021
assert_eq!(name.last().cloned(), Some(0u8));
2122

2223
// Currently unused as per erlang nif documentation
23-
let module_p: *const u8 = ptr::null();
24-
let name_p = name.as_ptr();
24+
let module_p: *const c_char = ptr::null();
25+
let name_p = name.as_ptr() as *const c_char;
2526
let res = {
2627
let mut tried = MaybeUninit::uninit();
2728
rustler_sys::enif_open_resource_type(env, module_p, name_p, dtor, flags, tried.as_mut_ptr())

rustler_codegen/src/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl From<InitMacroInput> for proc_macro2::TokenStream {
6666
let entry = rustler::codegen_runtime::DEF_NIF_ENTRY {
6767
major: rustler::codegen_runtime::NIF_MAJOR_VERSION,
6868
minor: rustler::codegen_runtime::NIF_MINOR_VERSION,
69-
name: concat!(#name, "\0").as_ptr() as *const u8,
69+
name: concat!(#name, "\0").as_ptr() as *const rustler::codegen_runtime::c_char,
7070
num_of_funcs: #num_of_funcs as rustler::codegen_runtime::c_int,
7171
funcs: [#funcs].as_ptr(),
7272
load: {
@@ -85,7 +85,7 @@ impl From<InitMacroInput> for proc_macro2::TokenStream {
8585
reload: None,
8686
upgrade: None,
8787
unload: None,
88-
vm_variant: b"beam.vanilla\0".as_ptr(),
88+
vm_variant: b"beam.vanilla\0".as_ptr() as *const rustler::codegen_runtime::c_char,
8989
options: 0,
9090
sizeof_ErlNifResourceTypeInit: rustler::codegen_runtime::get_nif_resource_type_init_size(),
9191
};

rustler_codegen/src/nif.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ pub fn transcoder_decorator(nif_attributes: NifAttributes, fun: syn::ItemFn) ->
5656
pub struct #name;
5757

5858
impl rustler::Nif for #name {
59-
const NAME: *const u8 = concat!(stringify!(#erl_func_name), "\0").as_ptr() as *const u8;
60-
const ARITY: u32 = #arity;
61-
const FLAGS: u32 = #flags as u32;
59+
const NAME: *const rustler::codegen_runtime::c_char = concat!(stringify!(#erl_func_name), "\0").as_ptr() as *const rustler::codegen_runtime::c_char;
60+
const ARITY: rustler::codegen_runtime::c_uint = #arity;
61+
const FLAGS: rustler::codegen_runtime::c_uint = #flags as rustler::codegen_runtime::c_uint;
6262
const RAW_FUNC: unsafe extern "C" fn(
6363
nif_env: rustler::codegen_runtime::NIF_ENV,
6464
argc: rustler::codegen_runtime::c_int,

rustler_sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ name = "rustler_sys"
2020
# When depending on this crate, you should ALWAYS
2121
# use a tilde requirements with AT LEAST `~MAJOR.MINOR`.
2222
# Example: "~2.0"
23-
version = "2.3.2"
23+
version = "2.4.0"
2424

2525
authors = ["Daniel Goertzen <[email protected]>"]
2626
description = "Create Erlang NIF modules in Rust using the C NIF API."

rustler_sys/build.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
515515
"enif_is_empty_list",
516516
"arg1: *mut ErlNifEnv, term: ERL_NIF_TERM",
517517
);
518-
b.func("*const ErlNifResourceType", "enif_open_resource_type", "arg1: *mut ErlNifEnv, module_str: *const c_uchar, name_str: *const c_uchar, dtor: Option<unsafe extern \"C\" fn (arg1: *mut ErlNifEnv, arg2: *mut c_void)>, flags: ErlNifResourceFlags, tried: *mut ErlNifResourceFlags");
518+
b.func("*const ErlNifResourceType", "enif_open_resource_type", "arg1: *mut ErlNifEnv, module_str: *const c_char, name_str: *const c_char, dtor: Option<unsafe extern \"C\" fn (arg1: *mut ErlNifEnv, arg2: *mut c_void)>, flags: ErlNifResourceFlags, tried: *mut ErlNifResourceFlags");
519519
b.func(
520520
"*mut c_void",
521521
"enif_alloc_resource",
@@ -557,13 +557,13 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
557557
b.func(
558558
"ERL_NIF_TERM",
559559
"enif_make_atom_len",
560-
"env: *mut ErlNifEnv, name: *const c_uchar, len: size_t",
560+
"env: *mut ErlNifEnv, name: *const c_char, len: size_t",
561561
);
562-
b.func("c_int", "enif_make_existing_atom_len", "env: *mut ErlNifEnv, name: *const c_uchar, len: size_t, atom: *mut ERL_NIF_TERM, arg1: ErlNifCharEncoding");
562+
b.func("c_int", "enif_make_existing_atom_len", "env: *mut ErlNifEnv, name: *const c_char, len: size_t, atom: *mut ERL_NIF_TERM, arg1: ErlNifCharEncoding");
563563
b.func(
564564
"ERL_NIF_TERM",
565565
"enif_make_string_len",
566-
"env: *mut ErlNifEnv, string: *const c_uchar, len: size_t, arg1: ErlNifCharEncoding",
566+
"env: *mut ErlNifEnv, string: *const c_char, len: size_t, arg1: ErlNifCharEncoding",
567567
);
568568
b.func("*mut ErlNifEnv", "enif_alloc_env", "");
569569
b.func("", "enif_free_env", "env: *mut ErlNifEnv");
@@ -633,8 +633,8 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
633633
"enif_is_number",
634634
"arg1: *mut ErlNifEnv, term: ERL_NIF_TERM",
635635
);
636-
b.func("*mut c_void", "enif_dlopen", "lib: *const c_uchar, err_handler: Option<unsafe extern \"C\" fn (arg1: *mut c_void, arg2: *const c_uchar)>, err_arg: *mut c_void");
637-
b.func("*mut c_void", "enif_dlsym", "handle: *mut c_void, symbol: *const c_uchar, err_handler: Option<unsafe extern \"C\" fn (arg1: *mut c_void, arg2: *const c_uchar)>, err_arg: *mut c_void");
636+
b.func("*mut c_void", "enif_dlopen", "lib: *const c_char, err_handler: Option<unsafe extern \"C\" fn (arg1: *mut c_void, arg2: *const c_char)>, err_arg: *mut c_void");
637+
b.func("*mut c_void", "enif_dlsym", "handle: *mut c_void, symbol: *const c_char, err_handler: Option<unsafe extern \"C\" fn (arg1: *mut c_void, arg2: *const c_char)>, err_arg: *mut c_void");
638638
b.func(
639639
"c_int",
640640
"enif_consume_timeslice",
@@ -690,7 +690,7 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
690690
"env: *mut ErlNifEnv, iter: *mut ErlNifMapIterator",
691691
);
692692
b.func("c_int", "enif_map_iterator_get_pair", "env: *mut ErlNifEnv, iter: *mut ErlNifMapIterator, key: *mut ERL_NIF_TERM, value: *mut ERL_NIF_TERM");
693-
b.func("ERL_NIF_TERM", "enif_schedule_nif", "env: *mut ErlNifEnv, fun_name: *const c_uchar, flags:c_int, fp: unsafe extern \"C\" fn(env: *mut ErlNifEnv, argc:c_int, argv:*const ERL_NIF_TERM) -> ERL_NIF_TERM, argc:c_int, argv:*const ERL_NIF_TERM");
693+
b.func("ERL_NIF_TERM", "enif_schedule_nif", "env: *mut ErlNifEnv, fun_name: *const c_char, flags:c_int, fp: unsafe extern \"C\" fn(env: *mut ErlNifEnv, argc:c_int, argv:*const ERL_NIF_TERM) -> ERL_NIF_TERM, argc:c_int, argv:*const ERL_NIF_TERM");
694694

695695
// exception
696696
b.func(
@@ -708,7 +708,7 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
708708
b.func(
709709
"c_int",
710710
"enif_getenv",
711-
"key: *const c_uchar, value: *mut c_uchar, value_size: *mut size_t",
711+
"key: *const c_char, value: *mut c_char, value_size: *mut size_t",
712712
);
713713

714714
// time
@@ -766,7 +766,7 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
766766

767767
if opts.nif_version >= (2, 12) {
768768
b.func("c_int", "enif_select", "env: *mut ErlNifEnv, e: ErlNifEvent, flags: ErlNifSelectFlags, obj: *const c_void, pid: *const ErlNifPid, eref: ERL_NIF_TERM");
769-
b.func("*const ErlNifResourceType", "enif_open_resource_type_x", "env: *mut ErlNifEnv, name_str: *const c_uchar, init: *const ErlNifResourceTypeInit, flags: ErlNifResourceFlags, tried: *mut ErlNifResourceFlags");
769+
b.func("*const ErlNifResourceType", "enif_open_resource_type_x", "env: *mut ErlNifEnv, name_str: *const c_char, init: *const ErlNifResourceTypeInit, flags: ErlNifResourceFlags, tried: *mut ErlNifResourceFlags");
770770
b.func("c_int", "enif_monitor_process", "env: *mut ErlNifEnv, obj: *const c_void, pid: *const ErlNifPid, monitor: *mut ErlNifMonitor");
771771
b.func(
772772
"c_int",
@@ -862,7 +862,7 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
862862

863863
// 2.16 was introduced in OTP 24
864864
if opts.nif_version >= (2, 16) {
865-
b.func("*const ErlNifResourceType", "enif_init_resource_type", "env: *mut ErlNifEnv, name_str: *const c_uchar, init: *const ErlNifResourceTypeInit, flags: ErlNifResourceFlags, tried: *mut ErlNifResourceFlags");
865+
b.func("*const ErlNifResourceType", "enif_init_resource_type", "env: *mut ErlNifEnv, name_str: *const c_char, init: *const ErlNifResourceTypeInit, flags: ErlNifResourceFlags, tried: *mut ErlNifResourceFlags");
866866
b.func("c_int", "enif_dynamic_resource_call", "env: *mut ErlNifEnv, module: ERL_NIF_TERM, name: ERL_NIF_TERM, rsrc: ERL_NIF_TERM, call_data: *const c_void");
867867
}
868868

@@ -874,8 +874,8 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
874874
"env: *mut ErlNifEnv, opt: ErlNifOption",
875875
);
876876
b.func("c_int", "enif_get_string_length", "env: *mut ErlNifEnv, list: ERL_NIF_TERM, len: *mut c_uint, encoding: ErlNifCharEncoding");
877-
b.func("c_int", "enif_make_new_atom", "env: *mut ErlNifEnv, name: *const c_uchar, atom: *mut ERL_NIF_TERM, encoding: ErlNifCharEncoding");
878-
b.func("c_int", "enif_make_new_atom_len", "env: *mut ErlNifEnv, name: *const c_uchar, len: size_t, atom: *mut ERL_NIF_TERM, encoding: ErlNifCharEncoding");
877+
b.func("c_int", "enif_make_new_atom", "env: *mut ErlNifEnv, name: *const c_char, atom: *mut ERL_NIF_TERM, encoding: ErlNifCharEncoding");
878+
b.func("c_int", "enif_make_new_atom_len", "env: *mut ErlNifEnv, name: *const c_char, len: size_t, atom: *mut ERL_NIF_TERM, encoding: ErlNifCharEncoding");
879879
}
880880
}
881881

rustler_sys/src/rustler_sys_api.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#[cfg(windows)]
55
use unreachable::UncheckedOptionExt; // unchecked unwrap used in generated Windows code
66

7-
pub use std::os::raw::{c_char, c_double, c_int, c_long, c_uchar, c_uint, c_ulong, c_void};
7+
pub use std::ffi::{c_char, c_double, c_int, c_long, c_uchar, c_uint, c_ulong, c_void};
88

99
use std::os;
1010

@@ -41,7 +41,7 @@ unsafe impl Send for ErlNifEnv {}
4141
// #[allow(missing_copy_implementations)]
4242
#[repr(C)]
4343
pub struct ErlNifFunc {
44-
pub name: *const u8,
44+
pub name: *const c_char,
4545
pub arity: c_uint,
4646
pub function: unsafe extern "C" fn(
4747
env: *mut ErlNifEnv,
@@ -59,7 +59,7 @@ pub struct ErlNifFunc {
5959
pub struct ErlNifEntry {
6060
pub major: c_int,
6161
pub minor: c_int,
62-
pub name: *const u8,
62+
pub name: *const c_char,
6363
pub num_of_funcs: c_int,
6464
pub funcs: *const ErlNifFunc,
6565
pub load: Option<
@@ -85,7 +85,7 @@ pub struct ErlNifEntry {
8585
) -> c_int,
8686
>,
8787
pub unload: Option<unsafe extern "C" fn(env: *mut ErlNifEnv, priv_data: *mut c_void) -> ()>,
88-
pub vm_variant: *const u8,
88+
pub vm_variant: *const c_char,
8989
pub options: c_uint, // added in 2.7
9090
pub sizeof_ErlNifResourceTypeInit: usize, // added in 2.12
9191
}
@@ -98,7 +98,7 @@ pub const ERL_NIF_DIRTY_NIF_OPTION: c_uint = 1;
9898
#[repr(C)]
9999
pub struct ErlNifBinary {
100100
pub size: size_t,
101-
pub data: *mut u8,
101+
pub data: *mut c_uchar,
102102
ref_bin: *mut c_void,
103103
_spare: [*mut c_void; 2],
104104
}

0 commit comments

Comments
 (0)