diff --git a/Cargo.toml b/Cargo.toml index f857be65..628b540c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,6 @@ resolver = "2" members = [ "rustler", "rustler_codegen", - "rustler_sys", "rustler_tests/native/binary_example", "rustler_tests/native/rustler_test", "rustler_tests/native/rustler_bigint_test", @@ -16,5 +15,4 @@ members = [ default-members = [ "rustler", "rustler_codegen", - "rustler_sys", ] diff --git a/rustler/Cargo.toml b/rustler/Cargo.toml index 55ca9886..77de9e56 100644 --- a/rustler/Cargo.toml +++ b/rustler/Cargo.toml @@ -15,19 +15,22 @@ default = ["nif_version_2_15"] derive = [] alternative_nif_init_name = [] allocator = [] -nif_version_2_14 = ["rustler_sys/nif_version_2_14"] -nif_version_2_15 = ["nif_version_2_14", "rustler_sys/nif_version_2_15"] -nif_version_2_16 = ["nif_version_2_15", "rustler_sys/nif_version_2_16"] -nif_version_2_17 = ["nif_version_2_16", "rustler_sys/nif_version_2_17"] +nif_version_2_14 = [] +nif_version_2_15 = ["nif_version_2_14"] +nif_version_2_16 = ["nif_version_2_15"] +nif_version_2_17 = ["nif_version_2_16"] serde = ["dep:serde"] [dependencies] +libloading = "0.8" inventory = "0.3" rustler_codegen = { path = "../rustler_codegen", version = "0.34.0" } -rustler_sys = { path = "../rustler_sys", version = "~2.4.3" } num-bigint = { version = "0.4", optional = true } serde = { version = "1", optional = true } +[build-dependencies] +regex-lite = "0.1" + [package.metadata.release] [[package.metadata.release.pre-release-replacements]] diff --git a/rustler_sys/build.rs b/rustler/build.rs similarity index 98% rename from rustler_sys/build.rs rename to rustler/build.rs index f9fc7611..0ab9e336 100644 --- a/rustler_sys/build.rs +++ b/rustler/build.rs @@ -77,11 +77,10 @@ impl<'a> ApiBuilder for BasicApiBuilder<'a> { name, name ) .unwrap(); - writeln!(self.0, "#[macro_export]").unwrap(); - writeln!(self.0, "macro_rules! {} {{", name).unwrap(); + writeln!(self.0, "#[macro_export] macro_rules! {} {{", name).unwrap(); writeln!( self.0, - " ( $( $arg:expr ),* ) => {{ $crate::_{}($($arg),*) }};", + " ( $( $arg:expr ),* ) => {{ $crate::sys::_{}($($arg),*) }};", name ) .unwrap(); @@ -92,6 +91,7 @@ impl<'a> ApiBuilder for BasicApiBuilder<'a> { ) .unwrap(); writeln!(self.0, "}}\n").unwrap(); + writeln!(self.0, "pub use {name};\n").unwrap(); } fn dummy(&mut self, _name: &str) {} } @@ -156,11 +156,10 @@ impl<'a> ApiBuilder for WinForwardersApiBuilder<'a> { writeln!(self.0, "}}\n").unwrap(); } fn variadic_func(&mut self, ret: &str, name: &str, args: &str) { - writeln!(self.0, "#[macro_export]").unwrap(); - writeln!(self.0, "macro_rules! {} {{", name).unwrap(); + writeln!(self.0, "#[macro_export] macro_rules! {} {{", name).unwrap(); writeln!( self.0, - " ( $( $arg:expr ),* ) => {{ $crate::get_{}()($($arg),*) }};", + " ( $( $arg:expr ),* ) => {{ $crate::sys::get_{}()($($arg),*) }};", name ) .unwrap(); @@ -171,6 +170,7 @@ impl<'a> ApiBuilder for WinForwardersApiBuilder<'a> { ) .unwrap(); writeln!(self.0, "}}\n").unwrap(); + writeln!(self.0, "pub use {name};\n").unwrap(); write!(self.0, "pub unsafe fn get_{}() -> ", name).unwrap(); write_variadic_fn_type(self.0, args, ret); diff --git a/rustler/src/alloc.rs b/rustler/src/alloc.rs index 61f3428c..48f20c63 100644 --- a/rustler/src/alloc.rs +++ b/rustler/src/alloc.rs @@ -1,5 +1,7 @@ use std::alloc::{GlobalAlloc, Layout}; +use crate::sys::{c_void, enif_alloc, enif_free}; + const SIZEOF_USIZE: usize = std::mem::size_of::(); const MAX_ALIGN: usize = 8; @@ -23,7 +25,7 @@ unsafe impl GlobalAlloc for EnifAllocator { // `layout.align() - 1`. The requirement for an additional `usize` just shifts the // problem without changing the padding requirement. let total_size = SIZEOF_USIZE + layout.size() + layout.align() - 1; - let ptr = rustler_sys::enif_alloc(total_size) as *mut u8; + let ptr = enif_alloc(total_size) as *mut u8; // Shift the returned pointer to make space for the original pointer let ptr1 = ptr.wrapping_add(SIZEOF_USIZE); @@ -37,7 +39,7 @@ unsafe impl GlobalAlloc for EnifAllocator { aligned_ptr } else { - rustler_sys::enif_alloc(layout.size()) as *mut u8 + enif_alloc(layout.size()) as *mut u8 } } @@ -46,10 +48,10 @@ unsafe impl GlobalAlloc for EnifAllocator { // Retrieve the original pointer let header = ptr.wrapping_sub(SIZEOF_USIZE); let ptr = *(header as *mut usize); - ptr as *mut rustler_sys::c_void + ptr as *mut c_void } else { - ptr as *mut rustler_sys::c_void + ptr as *mut c_void }; - rustler_sys::enif_free(ptr); + enif_free(ptr); } } diff --git a/rustler/src/codegen_runtime.rs b/rustler/src/codegen_runtime.rs index 6c86cb23..d745d6b0 100644 --- a/rustler/src/codegen_runtime.rs +++ b/rustler/src/codegen_runtime.rs @@ -18,7 +18,7 @@ pub use crate::wrapper::{ NIF_ENV, NIF_MAJOR_VERSION, NIF_MINOR_VERSION, NIF_TERM, }; -pub use rustler_sys::{internal_set_symbols, internal_write_symbols, DynNifCallbacks}; +pub use crate::sys::{internal_set_symbols, internal_write_symbols, DynNifCallbacks}; pub unsafe trait NifReturnable { unsafe fn into_returned(self, env: Env) -> NifReturned; @@ -76,7 +76,7 @@ impl NifReturned { flags, fun, args, - } => rustler_sys::enif_schedule_nif( + } => crate::sys::enif_schedule_nif( env.as_c_arg(), fun_name.as_ptr() as *const c_char, flags as i32, diff --git a/rustler/src/dynamic.rs b/rustler/src/dynamic.rs index 4eba67c3..5b6dafa2 100644 --- a/rustler/src/dynamic.rs +++ b/rustler/src/dynamic.rs @@ -1,7 +1,7 @@ use std::ffi::c_double; #[cfg(feature = "nif_version_2_15")] -use rustler_sys::ErlNifTermType; +use crate::sys::ErlNifTermType; use crate::wrapper::check; use crate::Term; @@ -108,7 +108,7 @@ impl<'a> Term<'a> { pub fn is_float(self) -> bool { let mut val: c_double = 0.0; unsafe { - rustler_sys::enif_get_double(self.get_env().as_c_arg(), self.as_c_arg(), &mut val) == 1 + crate::sys::enif_get_double(self.get_env().as_c_arg(), self.as_c_arg(), &mut val) == 1 } } diff --git a/rustler/src/env.rs b/rustler/src/env.rs index 208465bb..7b7efb84 100644 --- a/rustler/src/env.rs +++ b/rustler/src/env.rs @@ -1,3 +1,4 @@ +use crate::sys::{enif_alloc_env, enif_clear_env, enif_free_env, enif_send, enif_whereis_pid}; use crate::thread::is_scheduler_thread; use crate::types::LocalPid; use crate::wrapper::{NIF_ENV, NIF_TERM}; @@ -114,9 +115,7 @@ impl<'a> Env<'a> { let message = message.encode(self); // Send the message. - let res = unsafe { - rustler_sys::enif_send(env, pid.as_c_arg(), ptr::null_mut(), message.as_c_arg()) - }; + let res = unsafe { enif_send(env, pid.as_c_arg(), ptr::null_mut(), message.as_c_arg()) }; if res == 1 { Ok(()) @@ -143,7 +142,7 @@ impl<'a> Env<'a> { let mut enif_pid = std::mem::MaybeUninit::uninit(); if unsafe { - rustler_sys::enif_whereis_pid( + enif_whereis_pid( self.as_c_arg(), name_or_pid.as_c_arg(), enif_pid.as_mut_ptr(), @@ -208,7 +207,7 @@ impl OwnedEnv { #[allow(clippy::arc_with_non_send_sync)] // Likely false negative, see https://github.com/rust-lang/rust-clippy/issues/11382 pub fn new() -> OwnedEnv { OwnedEnv { - env: Arc::new(unsafe { rustler_sys::enif_alloc_env() }), + env: Arc::new(unsafe { enif_alloc_env() }), } } @@ -249,9 +248,7 @@ impl OwnedEnv { let message = self.run(|env| closure(env).encode(env).as_c_arg()); - let res = unsafe { - rustler_sys::enif_send(ptr::null_mut(), recipient.as_c_arg(), *self.env, message) - }; + let res = unsafe { enif_send(ptr::null_mut(), recipient.as_c_arg(), *self.env, message) }; self.clear(); @@ -275,7 +272,7 @@ impl OwnedEnv { let c_env = *self.env; self.env = Arc::new(c_env); unsafe { - rustler_sys::enif_clear_env(c_env); + enif_clear_env(c_env); } } @@ -318,7 +315,7 @@ impl OwnedEnv { impl Drop for OwnedEnv { fn drop(&mut self) { unsafe { - rustler_sys::enif_free_env(*self.env); + enif_free_env(*self.env); } } } diff --git a/rustler/src/lib.rs b/rustler/src/lib.rs index 27144ce8..d1335526 100644 --- a/rustler/src/lib.rs +++ b/rustler/src/lib.rs @@ -82,6 +82,4 @@ pub mod serde; #[cfg(feature = "serde")] pub use crate::serde::SerdeTerm; -pub mod sys { - pub use rustler_sys::*; -} +pub mod sys; diff --git a/rustler/src/resource/arc.rs b/rustler/src/resource/arc.rs index a6827cf6..f83b5f8f 100644 --- a/rustler/src/resource/arc.rs +++ b/rustler/src/resource/arc.rs @@ -2,7 +2,10 @@ use std::mem::MaybeUninit; use std::ops::Deref; use std::ptr; -use rustler_sys::{c_void, ErlNifEnv}; +use crate::sys::{ + c_void, enif_alloc_resource, enif_demonitor_process, enif_keep_resource, enif_make_resource, + enif_make_resource_binary, enif_monitor_process, enif_release_resource, ErlNifEnv, +}; use crate::thread::is_scheduler_thread; use crate::{Binary, Decoder, Encoder, Env, Error, LocalPid, Monitor, NifResult, OwnedEnv, Term}; @@ -40,7 +43,7 @@ where pub fn new(data: T) -> Self { let alloc_size = get_alloc_size_struct::(); let resource_type = T::get_resource_type().unwrap(); - let mem_raw = unsafe { rustler_sys::enif_alloc_resource(resource_type, alloc_size) }; + let mem_raw = unsafe { enif_alloc_resource(resource_type, alloc_size) }; let aligned_mem = unsafe { align_alloced_mem_for_struct::(mem_raw) as *mut T }; unsafe { ptr::write(aligned_mem, data) }; @@ -80,7 +83,7 @@ where F: FnOnce(&'a T) -> &'b [u8], { let bin = f(&*self.inner); - let binary = rustler_sys::enif_make_resource_binary( + let binary = enif_make_resource_binary( env.as_c_arg(), self.raw, bin.as_ptr() as *const c_void, @@ -93,17 +96,12 @@ where fn from_term(term: Term) -> Result { let (raw, inner) = unsafe { term.try_get_resource_ptrs::() }.ok_or(Error::BadArg)?; - unsafe { rustler_sys::enif_keep_resource(raw) }; + unsafe { enif_keep_resource(raw) }; Ok(ResourceArc { raw, inner }) } fn as_term<'a>(&self, env: Env<'a>) -> Term<'a> { - unsafe { - Term::new( - env, - rustler_sys::enif_make_resource(env.as_c_arg(), self.raw), - ) - } + unsafe { Term::new(env, enif_make_resource(env.as_c_arg(), self.raw)) } } fn as_c_arg(&mut self) -> *const c_void { @@ -126,9 +124,8 @@ where let env = maybe_env(caller_env); let mut mon = MaybeUninit::uninit(); - let res = unsafe { - rustler_sys::enif_monitor_process(env, self.raw, pid.as_c_arg(), mon.as_mut_ptr()) == 0 - }; + let res = + unsafe { enif_monitor_process(env, self.raw, pid.as_c_arg(), mon.as_mut_ptr()) == 0 }; if res { Some(unsafe { Monitor::new(mon.assume_init()) }) } else { @@ -142,7 +139,7 @@ where } let env = maybe_env(caller_env); - unsafe { rustler_sys::enif_demonitor_process(env, self.raw, mon.as_c_arg()) == 0 } + unsafe { enif_demonitor_process(env, self.raw, mon.as_c_arg()) == 0 } } } @@ -179,9 +176,11 @@ impl<'a> Env<'a> { module: crate::Atom, name: crate::Atom, resource: Term<'a>, - call_data: *mut rustler_sys::c_void, + call_data: *mut c_void, ) -> Result<(), super::DynamicResourceCallError> { - let res = rustler_sys::enif_dynamic_resource_call( + use crate::sys::enif_dynamic_resource_call; + + let res = enif_dynamic_resource_call( self.as_c_arg(), module.as_c_arg(), name.as_c_arg(), @@ -215,7 +214,7 @@ where /// Cloning a `ResourceArc` simply increments the reference count for the /// resource. The `T` value is not cloned. fn clone(&self) -> Self { - unsafe { rustler_sys::enif_keep_resource(self.raw) }; + unsafe { enif_keep_resource(self.raw) }; ResourceArc { raw: self.raw, inner: self.inner, @@ -234,7 +233,7 @@ where /// at an unpredictable time: whenever the VM decides to do garbage /// collection. fn drop(&mut self) { - unsafe { rustler_sys::enif_release_resource(self.as_c_arg()) }; + unsafe { enif_release_resource(self.as_c_arg()) }; } } diff --git a/rustler/src/resource/monitor.rs b/rustler/src/resource/monitor.rs index c0cdfb25..d19ad139 100644 --- a/rustler/src/resource/monitor.rs +++ b/rustler/src/resource/monitor.rs @@ -1,4 +1,4 @@ -use rustler_sys::ErlNifMonitor; +use crate::sys::{enif_compare_monitors, ErlNifMonitor}; /// Handle for a monitor created using `ResourceArc::monitor`. /// @@ -25,6 +25,6 @@ impl Monitor { impl PartialEq for Monitor { fn eq(&self, other: &Self) -> bool { - unsafe { rustler_sys::enif_compare_monitors(&self.inner, &other.inner) == 0 } + unsafe { enif_compare_monitors(&self.inner, &other.inner) == 0 } } } diff --git a/rustler/src/resource/registration.rs b/rustler/src/resource/registration.rs index ab45bd6f..85e7a649 100644 --- a/rustler/src/resource/registration.rs +++ b/rustler/src/resource/registration.rs @@ -2,12 +2,11 @@ use super::traits; use super::util::align_alloced_mem_for_struct; use super::ResourceInitError; use crate::env::EnvKind; -use crate::{Env, LocalPid, Monitor, Resource}; -use rustler_sys::ErlNifResourceDtor; -use rustler_sys::{ - c_char, c_void, ErlNifEnv, ErlNifMonitor, ErlNifPid, ErlNifResourceDown, ErlNifResourceFlags, - ErlNifResourceType, ErlNifResourceTypeInit, +use crate::sys::{ + c_char, c_void, ErlNifEnv, ErlNifMonitor, ErlNifPid, ErlNifResourceDown, ErlNifResourceDtor, + ErlNifResourceFlags, ErlNifResourceType, ErlNifResourceTypeInit, }; +use crate::{Env, LocalPid, Monitor, Resource}; use std::any::TypeId; use std::ffi::CString; use std::mem::MaybeUninit; @@ -116,7 +115,7 @@ impl Registration { if T::IMPLEMENTS_DYNCALL { Self { init: ErlNifResourceTypeInit { - dyncall: resource_dyncall:: as *const rustler_sys::ErlNifResourceDynCall, + dyncall: resource_dyncall:: as *const crate::sys::ErlNifResourceDynCall, members: max(self.init.members, 4), ..self.init }, @@ -230,10 +229,10 @@ type OpenResourceTypeFn = unsafe extern "C" fn( ) -> *const ErlNifResourceType; #[cfg(feature = "nif_version_2_16")] -static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = rustler_sys::enif_init_resource_type; +static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = crate::sys::enif_init_resource_type; #[cfg(not(feature = "nif_version_2_16"))] -static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = rustler_sys::enif_open_resource_type_x; +static OPEN_RESOURCE_TYPE: OpenResourceTypeFn = crate::sys::enif_open_resource_type_x; const fn max(i: i32, j: i32) -> i32 { if i > j { diff --git a/rustler/src/resource/term.rs b/rustler/src/resource/term.rs index 040174d8..a9052fb7 100644 --- a/rustler/src/resource/term.rs +++ b/rustler/src/resource/term.rs @@ -1,7 +1,7 @@ use super::util::align_alloced_mem_for_struct; use super::{Resource, ResourceExt}; +use crate::sys::{c_void, enif_get_resource}; use crate::{Decoder, Error, NifResult, Term}; -use rustler_sys::c_void; use std::mem::MaybeUninit; impl<'a> Term<'a> { @@ -12,7 +12,7 @@ impl<'a> Term<'a> { ) -> Option<(*const c_void, *mut T)> { let typ = T::get_resource_type()?; let mut ret_obj = MaybeUninit::uninit(); - let res = rustler_sys::enif_get_resource( + let res = enif_get_resource( self.get_env().as_c_arg(), self.as_c_arg(), typ, diff --git a/rustler/src/resource/traits.rs b/rustler/src/resource/traits.rs index b1614105..ddb8552b 100644 --- a/rustler/src/resource/traits.rs +++ b/rustler/src/resource/traits.rs @@ -2,9 +2,10 @@ use std::any::TypeId; use std::collections::HashMap; use std::sync::OnceLock; +use crate::sys::ErlNifResourceType; use crate::{Env, LocalPid, Monitor}; -type NifResourcePtr = *const rustler_sys::ErlNifResourceType; +type NifResourcePtr = *const ErlNifResourceType; /// Map from `TypeId` to the `NifResourcePtr`. To be able to store this in a `OnceLock`, the /// pointer is type-erased and stored as a `usize`. @@ -57,7 +58,7 @@ pub trait Resource: Sized + Send + Sync + 'static { #[cfg(feature = "nif_version_2_16")] #[allow(unused)] - unsafe fn dyncall<'a>(&'a self, env: Env<'a>, call_data: *mut rustler_sys::c_void) {} + unsafe fn dyncall<'a>(&'a self, env: Env<'a>, call_data: *mut crate::sys::c_void) {} } #[doc(hidden)] diff --git a/rustler/src/resource/util.rs b/rustler/src/resource/util.rs index 230d127d..16902745 100644 --- a/rustler/src/resource/util.rs +++ b/rustler/src/resource/util.rs @@ -1,4 +1,4 @@ -use rustler_sys::c_void; +use crate::sys::c_void; use std::mem; pub fn get_alloc_size_struct() -> usize { diff --git a/rustler/src/schedule.rs b/rustler/src/schedule.rs index ccc5de1c..21f393bb 100644 --- a/rustler/src/schedule.rs +++ b/rustler/src/schedule.rs @@ -1,3 +1,4 @@ +use crate::sys::enif_consume_timeslice; use crate::wrapper::ErlNifTaskFlags; use crate::Env; @@ -8,6 +9,6 @@ pub enum SchedulerFlags { } pub fn consume_timeslice(env: Env, percent: i32) -> bool { - let success = unsafe { rustler_sys::enif_consume_timeslice(env.as_c_arg(), percent) }; + let success = unsafe { enif_consume_timeslice(env.as_c_arg(), percent) }; success == 1 } diff --git a/rustler_sys/src/lib.rs b/rustler/src/sys.rs similarity index 83% rename from rustler_sys/src/lib.rs rename to rustler/src/sys.rs index 4190e047..cd050160 100644 --- a/rustler_sys/src/lib.rs +++ b/rustler/src/sys.rs @@ -10,5 +10,5 @@ mod functions; mod nif_filler; mod types; -pub use crate::functions::*; -pub use crate::types::*; +pub use self::functions::*; +pub use self::types::*; diff --git a/rustler_sys/src/functions.rs b/rustler/src/sys/functions.rs similarity index 95% rename from rustler_sys/src/functions.rs rename to rustler/src/sys/functions.rs index 27117639..a81cdbe1 100644 --- a/rustler_sys/src/functions.rs +++ b/rustler/src/sys/functions.rs @@ -1,5 +1,7 @@ -use crate::nif_filler::{self, DynNifFiller}; -use crate::types::*; +use super::{ + nif_filler::{self, DynNifFiller}, + types::*, +}; static mut DYN_NIF_CALLBACKS: DynNifCallbacks = unsafe { std::mem::MaybeUninit::zeroed().assume_init() }; diff --git a/rustler_sys/src/nif_filler.rs b/rustler/src/sys/nif_filler.rs similarity index 100% rename from rustler_sys/src/nif_filler.rs rename to rustler/src/sys/nif_filler.rs diff --git a/rustler_sys/src/types.rs b/rustler/src/sys/types.rs similarity index 100% rename from rustler_sys/src/types.rs rename to rustler/src/sys/types.rs diff --git a/rustler/src/term.rs b/rustler/src/term.rs index 02e8c1cc..99ceaa2b 100644 --- a/rustler/src/term.rs +++ b/rustler/src/term.rs @@ -1,3 +1,4 @@ +use crate::sys::*; use crate::types::binary::OwnedBinary; use crate::wrapper::env::term_to_binary; use crate::wrapper::NIF_TERM; @@ -53,12 +54,7 @@ impl<'a> Term<'a> { // lifetimes of two .run() calls on the same OwnedEnv.) unsafe { Term::new(env, self.as_c_arg()) } } else { - unsafe { - Term::new( - env, - rustler_sys::enif_make_copy(env.as_c_arg(), self.as_c_arg()), - ) - } + unsafe { Term::new(env, enif_make_copy(env.as_c_arg(), self.as_c_arg())) } } } @@ -103,8 +99,8 @@ impl<'a> Term<'a> { /// It takes 32-bit salt values and generates hashes within 0..2^32-1. pub fn hash_internal(&self, salt: u32) -> u32 { unsafe { - rustler_sys::enif_hash( - rustler_sys::ErlNifHash::ERL_NIF_INTERNAL_HASH, + enif_hash( + ErlNifHash::ERL_NIF_INTERNAL_HASH, self.as_c_arg(), salt as u64, ) as u32 @@ -116,27 +112,24 @@ impl<'a> Term<'a> { /// /// It generates hashes within 0..2^27-1. pub fn hash_phash2(&self) -> u32 { - unsafe { - rustler_sys::enif_hash(rustler_sys::ErlNifHash::ERL_NIF_PHASH2, self.as_c_arg(), 0) - as u32 - } + unsafe { enif_hash(ErlNifHash::ERL_NIF_PHASH2, self.as_c_arg(), 0) as u32 } } #[cfg(feature = "nif_version_2_15")] - pub fn get_erl_type(&self) -> rustler_sys::ErlNifTermType { - unsafe { rustler_sys::enif_term_type(self.env.as_c_arg(), self.as_c_arg()) } + pub fn get_erl_type(&self) -> ErlNifTermType { + unsafe { enif_term_type(self.env.as_c_arg(), self.as_c_arg()) } } } impl<'a> PartialEq for Term<'a> { fn eq(&self, other: &Term) -> bool { - unsafe { rustler_sys::enif_is_identical(self.as_c_arg(), other.as_c_arg()) == 1 } + unsafe { enif_is_identical(self.as_c_arg(), other.as_c_arg()) == 1 } } } impl<'a> Eq for Term<'a> {} fn cmp(lhs: &Term, rhs: &Term) -> Ordering { - let ord = unsafe { rustler_sys::enif_compare(lhs.as_c_arg(), rhs.as_c_arg()) }; + let ord = unsafe { enif_compare(lhs.as_c_arg(), rhs.as_c_arg()) }; match ord { 0 => Ordering::Equal, n if n < 0 => Ordering::Less, diff --git a/rustler/src/thread.rs b/rustler/src/thread.rs index 0c2ea2eb..5f968798 100644 --- a/rustler/src/thread.rs +++ b/rustler/src/thread.rs @@ -1,4 +1,5 @@ use crate::env::OwnedEnv; +use crate::sys::enif_thread_type; use crate::{Atom, Encoder, Env, Term}; use std::panic; use std::thread; @@ -66,5 +67,5 @@ where /// /// This is relevant for (e.g.) `enif_send` or `enif_monitor_process` as pub fn is_scheduler_thread() -> bool { - unsafe { rustler_sys::enif_thread_type() > 0 } + unsafe { enif_thread_type() > 0 } } diff --git a/rustler/src/types/binary.rs b/rustler/src/types/binary.rs index 09ae5ce6..8cfa9328 100644 --- a/rustler/src/types/binary.rs +++ b/rustler/src/types/binary.rs @@ -86,6 +86,10 @@ //! [`OwnedBinary`]: struct.OwnedBinary.html use crate::{ + sys::{ + enif_inspect_binary, enif_inspect_iolist_as_binary, enif_make_binary, enif_make_sub_binary, + enif_release_binary, + }, wrapper::binary::{alloc, new_binary, realloc, ErlNifBinary}, Decoder, Encoder, Env, Error, NifResult, Term, }; @@ -225,7 +229,7 @@ impl PartialEq> for OwnedBinary { impl Drop for OwnedBinary { fn drop(&mut self) { - unsafe { rustler_sys::enif_release_binary(&mut self.0) }; + unsafe { enif_release_binary(&mut self.0) }; } } @@ -251,12 +255,7 @@ impl<'a> Binary<'a> { // to `mem::forget()`) is to wrap `owned` in a `ManuallyDrop` and EXPLICITLY // NOT CALL `ManuallyDrop::drop()`. let mut owned = std::mem::ManuallyDrop::new(owned); - let term = unsafe { - Term::new( - env, - rustler_sys::enif_make_binary(env.as_c_arg(), &mut owned.0), - ) - }; + let term = unsafe { Term::new(env, enif_make_binary(env.as_c_arg(), &mut owned.0)) }; Binary { buf: owned.0.data, size: owned.0.size, @@ -282,7 +281,7 @@ impl<'a> Binary<'a> { pub fn from_term(term: Term<'a>) -> Result { let mut binary = MaybeUninit::uninit(); if unsafe { - rustler_sys::enif_inspect_binary( + enif_inspect_binary( term.get_env().as_c_arg(), term.as_c_arg(), binary.as_mut_ptr(), @@ -319,7 +318,7 @@ impl<'a> Binary<'a> { pub fn from_iolist(term: Term<'a>) -> Result { let mut binary = MaybeUninit::uninit(); if unsafe { - rustler_sys::enif_inspect_iolist_as_binary( + enif_inspect_iolist_as_binary( term.get_env().as_c_arg(), term.as_c_arg(), binary.as_mut_ptr(), @@ -377,7 +376,7 @@ impl<'a> Binary<'a> { #[allow(unused_unsafe)] pub unsafe fn make_subbinary_unchecked(&self, offset: usize, length: usize) -> Binary<'a> { let raw_term = unsafe { - rustler_sys::enif_make_sub_binary( + enif_make_sub_binary( self.term.get_env().as_c_arg(), self.term.as_c_arg(), offset, diff --git a/rustler/src/types/local_pid.rs b/rustler/src/types/local_pid.rs index 34ceee1d..d46726ad 100644 --- a/rustler/src/types/local_pid.rs +++ b/rustler/src/types/local_pid.rs @@ -1,3 +1,4 @@ +use crate::sys::{enif_compare_pids, enif_is_process_alive, enif_self}; use crate::wrapper::{pid, ErlNifPid}; use crate::{Decoder, Encoder, Env, Error, NifResult, Term}; use std::cmp::Ordering; @@ -39,7 +40,7 @@ impl Encoder for LocalPid { impl PartialEq for LocalPid { fn eq(&self, other: &Self) -> bool { - unsafe { rustler_sys::enif_compare_pids(self.as_c_arg(), other.as_c_arg()) == 0 } + unsafe { enif_compare_pids(self.as_c_arg(), other.as_c_arg()) == 0 } } } @@ -53,7 +54,7 @@ impl PartialOrd for LocalPid { impl Ord for LocalPid { fn cmp(&self, other: &Self) -> Ordering { - let cmp = unsafe { rustler_sys::enif_compare_pids(self.as_c_arg(), other.as_c_arg()) }; + let cmp = unsafe { enif_compare_pids(self.as_c_arg(), other.as_c_arg()) }; cmp.cmp(&0) } } @@ -68,7 +69,7 @@ impl<'a> Env<'a> { /// called is always associated with the calling Erlang process.) pub fn pid(self) -> LocalPid { let mut pid = MaybeUninit::uninit(); - if unsafe { rustler_sys::enif_self(self.as_c_arg(), pid.as_mut_ptr()) }.is_null() { + if unsafe { enif_self(self.as_c_arg(), pid.as_mut_ptr()) }.is_null() { panic!("environment is process-independent"); } LocalPid { @@ -78,7 +79,7 @@ impl<'a> Env<'a> { /// Checks whether the given process is alive pub fn is_process_alive(self, pid: LocalPid) -> bool { - let res = unsafe { rustler_sys::enif_is_process_alive(self.as_c_arg(), pid.as_c_arg()) }; + let res = unsafe { enif_is_process_alive(self.as_c_arg(), pid.as_c_arg()) }; res != 0 } } diff --git a/rustler/src/types/primitive.rs b/rustler/src/types/primitive.rs index 89072c30..2ec0b7c8 100644 --- a/rustler/src/types/primitive.rs +++ b/rustler/src/types/primitive.rs @@ -7,7 +7,7 @@ macro_rules! erl_make { unsafe { Term::new( $env, - rustler_sys::$encode_fun($env.as_c_arg(), $self as $type), + crate::sys::$encode_fun($env.as_c_arg(), $self as $type), ) } }; @@ -15,9 +15,7 @@ macro_rules! erl_make { macro_rules! erl_get { ($decode_fun:ident, $term:ident, $dest:ident) => { - unsafe { - rustler_sys::$decode_fun($term.get_env().as_c_arg(), $term.as_c_arg(), &mut $dest) - } + unsafe { crate::sys::$decode_fun($term.get_env().as_c_arg(), $term.as_c_arg(), &mut $dest) } }; } diff --git a/rustler/src/wrapper.rs b/rustler/src/wrapper.rs index 1e5f8cda..3f800644 100644 --- a/rustler/src/wrapper.rs +++ b/rustler/src/wrapper.rs @@ -18,35 +18,35 @@ pub mod pid; pub mod term; pub mod tuple; -pub use rustler_sys::{ +pub use crate::sys::{ enif_clear_env, enif_free_env, enif_get_local_pid, enif_make_pid, enif_map_iterator_create, enif_map_iterator_destroy, enif_map_iterator_get_pair, enif_map_iterator_next, enif_self, ErlNifMapIterator, ErlNifMapIteratorEntry, ErlNifPid, ERL_NIF_THR_DIRTY_CPU_SCHEDULER, ERL_NIF_THR_DIRTY_IO_SCHEDULER, ERL_NIF_THR_NORMAL_SCHEDULER, ERL_NIF_THR_UNDEFINED, }; -pub use rustler_sys::{c_char, c_double, c_int, c_uchar, c_uint, c_void}; +pub use crate::sys::{c_char, c_double, c_int, c_uchar, c_uint, c_void}; pub type size_t = usize; -pub type NIF_ENV = *mut rustler_sys::ErlNifEnv; +pub type NIF_ENV = *mut crate::sys::ErlNifEnv; pub type NIF_TERM = size_t; -pub type NIF_RESOURCE_TYPE = *const rustler_sys::ErlNifResourceType; +pub type NIF_RESOURCE_TYPE = *const crate::sys::ErlNifResourceType; pub fn get_nif_resource_type_init_size() -> usize { - std::mem::size_of::() + std::mem::size_of::() } pub enum NIF_ERROR { BAD_ARG, } -pub type DEF_NIF_FUNC = rustler_sys::ErlNifFunc; -pub type DEF_NIF_ENTRY = rustler_sys::ErlNifEntry; -pub use rustler_sys::NIF_MAJOR_VERSION; -pub use rustler_sys::NIF_MINOR_VERSION; +pub type DEF_NIF_FUNC = crate::sys::ErlNifFunc; +pub type DEF_NIF_ENTRY = crate::sys::ErlNifEntry; +pub use crate::sys::NIF_MAJOR_VERSION; +pub use crate::sys::NIF_MINOR_VERSION; -pub use rustler_sys::ErlNifBinaryToTerm as NIF_BINARY_TO_TERM_OPTS; -pub use rustler_sys::ERL_NIF_BIN2TERM_SAFE; +pub use crate::sys::ErlNifBinaryToTerm as NIF_BINARY_TO_TERM_OPTS; +pub use crate::sys::ERL_NIF_BIN2TERM_SAFE; #[repr(C)] pub enum ErlNifTaskFlags { diff --git a/rustler/src/wrapper/atom.rs b/rustler/src/wrapper/atom.rs index 919afa3c..144d174f 100644 --- a/rustler/src/wrapper/atom.rs +++ b/rustler/src/wrapper/atom.rs @@ -1,14 +1,17 @@ +use crate::sys::ErlNifCharEncoding::ERL_NIF_LATIN1; +use crate::sys::{ + enif_get_atom, enif_get_atom_length, enif_make_atom_len, enif_make_existing_atom_len, +}; use crate::wrapper::{c_char, c_uint, NIF_ENV, NIF_TERM}; use crate::Error; -use rustler_sys::ErlNifCharEncoding::ERL_NIF_LATIN1; pub unsafe fn make_atom(env: NIF_ENV, name: &[u8]) -> NIF_TERM { - rustler_sys::enif_make_atom_len(env, name.as_ptr() as *const c_char, name.len()) + enif_make_atom_len(env, name.as_ptr() as *const c_char, name.len()) } pub unsafe fn make_existing_atom(env: NIF_ENV, name: &[u8]) -> Option { let mut atom_out: NIF_TERM = 0; - let success = rustler_sys::enif_make_existing_atom_len( + let success = enif_make_existing_atom_len( env, name.as_ptr() as *const c_char, name.len(), @@ -33,7 +36,7 @@ pub unsafe fn make_existing_atom(env: NIF_ENV, name: &[u8]) -> Option pub unsafe fn get_atom(env: NIF_ENV, term: NIF_TERM) -> Result { // Determine the length of the atom, in bytes. let mut len = 0; - let success = rustler_sys::enif_get_atom_length(env, term, &mut len, ERL_NIF_LATIN1); + let success = enif_get_atom_length(env, term, &mut len, ERL_NIF_LATIN1); if success == 0 { return Err(Error::BadArg); } @@ -42,7 +45,7 @@ pub unsafe fn get_atom(env: NIF_ENV, term: NIF_TERM) -> Result { // enif_get_atom() writes a null terminated string, // so add 1 to the atom's length to make room for it. let mut bytes: Vec = Vec::with_capacity(len as usize + 1); - let nbytes = rustler_sys::enif_get_atom(env, term, bytes.as_mut_ptr(), len + 1, ERL_NIF_LATIN1); + let nbytes = enif_get_atom(env, term, bytes.as_mut_ptr(), len + 1, ERL_NIF_LATIN1); assert!(nbytes as c_uint == len + 1); // This is safe unless the VM is lying to us. diff --git a/rustler/src/wrapper/binary.rs b/rustler/src/wrapper/binary.rs index f33a27fe..18827b7c 100644 --- a/rustler/src/wrapper/binary.rs +++ b/rustler/src/wrapper/binary.rs @@ -1,12 +1,16 @@ -use crate::{wrapper::size_t, Env, Term}; -pub(crate) use rustler_sys::ErlNifBinary; +pub(crate) use crate::sys::ErlNifBinary; +use crate::{ + sys::{enif_alloc_binary, enif_make_new_binary, enif_realloc_binary}, + wrapper::size_t, + Env, Term, +}; use std::mem::MaybeUninit; -pub use rustler_sys::enif_make_sub_binary as make_subbinary; +pub use crate::sys::enif_make_sub_binary as make_subbinary; pub unsafe fn alloc(size: size_t) -> Option { let mut binary = MaybeUninit::uninit(); - let success = rustler_sys::enif_alloc_binary(size, binary.as_mut_ptr()); + let success = enif_alloc_binary(size, binary.as_mut_ptr()); if success == 0 { return None; } @@ -14,13 +18,13 @@ pub unsafe fn alloc(size: size_t) -> Option { } pub unsafe fn realloc(binary: &mut ErlNifBinary, size: size_t) -> bool { - let success = rustler_sys::enif_realloc_binary(binary, size); + let success = enif_realloc_binary(binary, size); success != 0 } pub unsafe fn new_binary(env: Env, size: size_t) -> (*mut u8, Term) { let mut term = MaybeUninit::uninit(); - let buf = rustler_sys::enif_make_new_binary(env.as_c_arg(), size, term.as_mut_ptr()); + let buf = enif_make_new_binary(env.as_c_arg(), size, term.as_mut_ptr()); if buf.is_null() { panic!("enif_make_new_binary: allocation failed"); } diff --git a/rustler/src/wrapper/check.rs b/rustler/src/wrapper/check.rs index ae9883bb..4ba6b0bd 100644 --- a/rustler/src/wrapper/check.rs +++ b/rustler/src/wrapper/check.rs @@ -1,21 +1,21 @@ use crate::wrapper::{NIF_ENV, NIF_TERM}; macro_rules! impl_check_fun { - ($name:ident, $inner:path) => { + ($name:ident, $inner:ident) => { pub unsafe fn $name(env: NIF_ENV, term: NIF_TERM) -> bool { - $inner(env, term) == 1 + crate::sys::$inner(env, term) == 1 } }; } -impl_check_fun!(is_atom, rustler_sys::enif_is_atom); -impl_check_fun!(is_binary, rustler_sys::enif_is_binary); -impl_check_fun!(is_empty_list, rustler_sys::enif_is_empty_list); -impl_check_fun!(is_fun, rustler_sys::enif_is_fun); -impl_check_fun!(is_list, rustler_sys::enif_is_list); -impl_check_fun!(is_map, rustler_sys::enif_is_map); -impl_check_fun!(is_number, rustler_sys::enif_is_number); -impl_check_fun!(is_pid, rustler_sys::enif_is_pid); -impl_check_fun!(is_port, rustler_sys::enif_is_port); -impl_check_fun!(is_ref, rustler_sys::enif_is_ref); -impl_check_fun!(is_tuple, rustler_sys::enif_is_tuple); +impl_check_fun!(is_atom, enif_is_atom); +impl_check_fun!(is_binary, enif_is_binary); +impl_check_fun!(is_empty_list, enif_is_empty_list); +impl_check_fun!(is_fun, enif_is_fun); +impl_check_fun!(is_list, enif_is_list); +impl_check_fun!(is_map, enif_is_map); +impl_check_fun!(is_number, enif_is_number); +impl_check_fun!(is_pid, enif_is_pid); +impl_check_fun!(is_port, enif_is_port); +impl_check_fun!(is_ref, enif_is_ref); +impl_check_fun!(is_tuple, enif_is_tuple); diff --git a/rustler/src/wrapper/env.rs b/rustler/src/wrapper/env.rs index 77f3b990..a7315b5a 100644 --- a/rustler/src/wrapper/env.rs +++ b/rustler/src/wrapper/env.rs @@ -1,3 +1,4 @@ +use crate::sys::{enif_binary_to_term, enif_term_to_binary}; use crate::wrapper::binary::ErlNifBinary; use crate::wrapper::{ERL_NIF_BIN2TERM_SAFE, NIF_ENV, NIF_TERM}; use std::mem::MaybeUninit; @@ -6,8 +7,7 @@ pub unsafe fn binary_to_term(env: NIF_ENV, data: &[u8], safe: bool) -> Option<(N let opts = if safe { ERL_NIF_BIN2TERM_SAFE } else { 0 }; let mut result = MaybeUninit::uninit(); - let read_count = - rustler_sys::enif_binary_to_term(env, data.as_ptr(), data.len(), result.as_mut_ptr(), opts); + let read_count = enif_binary_to_term(env, data.as_ptr(), data.len(), result.as_mut_ptr(), opts); if read_count == 0 { return None; @@ -18,7 +18,7 @@ pub unsafe fn binary_to_term(env: NIF_ENV, data: &[u8], safe: bool) -> Option<(N pub unsafe fn term_to_binary(env: NIF_ENV, term: NIF_TERM) -> Option { let mut binary = MaybeUninit::uninit(); - let success = rustler_sys::enif_term_to_binary(env, term, binary.as_mut_ptr()); + let success = enif_term_to_binary(env, term, binary.as_mut_ptr()); if success == 0 { return None; diff --git a/rustler/src/wrapper/exception.rs b/rustler/src/wrapper/exception.rs index 9e1e6e8c..f6acbcd6 100644 --- a/rustler/src/wrapper/exception.rs +++ b/rustler/src/wrapper/exception.rs @@ -1,4 +1,7 @@ -use crate::wrapper::{NIF_ENV, NIF_TERM}; +use crate::{ + sys::{enif_make_badarg, enif_raise_exception}, + wrapper::{NIF_ENV, NIF_TERM}, +}; /// Raise an "error exception". /// @@ -10,7 +13,7 @@ use crate::wrapper::{NIF_ENV, NIF_TERM}; /// /// And of course the usual rules about `env` and `term` still apply. pub unsafe fn raise_exception(env: NIF_ENV, term: NIF_TERM) -> NIF_TERM { - rustler_sys::enif_raise_exception(env, term) + enif_raise_exception(env, term) } /// Raise a `badarg` exception. @@ -23,5 +26,5 @@ pub unsafe fn raise_exception(env: NIF_ENV, term: NIF_TERM) -> NIF_TERM { /// /// And of course `env` must be a valid environment. pub unsafe fn raise_badarg(env: NIF_ENV) -> NIF_TERM { - rustler_sys::enif_make_badarg(env) + enif_make_badarg(env) } diff --git a/rustler/src/wrapper/list.rs b/rustler/src/wrapper/list.rs index af62a47e..33078586 100644 --- a/rustler/src/wrapper/list.rs +++ b/rustler/src/wrapper/list.rs @@ -1,10 +1,16 @@ -use crate::wrapper::{NIF_ENV, NIF_TERM}; +use crate::{ + sys::{ + enif_get_list_cell, enif_get_list_length, enif_make_list_cell, enif_make_list_from_array, + enif_make_reverse_list, + }, + wrapper::{NIF_ENV, NIF_TERM}, +}; use std::mem::MaybeUninit; pub unsafe fn get_list_cell(env: NIF_ENV, list: NIF_TERM) -> Option<(NIF_TERM, NIF_TERM)> { let mut head = MaybeUninit::uninit(); let mut tail = MaybeUninit::uninit(); - let success = rustler_sys::enif_get_list_cell(env, list, head.as_mut_ptr(), tail.as_mut_ptr()); + let success = enif_get_list_cell(env, list, head.as_mut_ptr(), tail.as_mut_ptr()); if success != 1 { return None; @@ -14,7 +20,7 @@ pub unsafe fn get_list_cell(env: NIF_ENV, list: NIF_TERM) -> Option<(NIF_TERM, N pub unsafe fn get_list_length(env: NIF_ENV, list: NIF_TERM) -> Option { let mut len: u32 = 0; - let success = rustler_sys::enif_get_list_length(env, list, &mut len); + let success = enif_get_list_length(env, list, &mut len); if success != 1 { return None; @@ -23,16 +29,16 @@ pub unsafe fn get_list_length(env: NIF_ENV, list: NIF_TERM) -> Option { } pub unsafe fn make_list(env: NIF_ENV, arr: &[NIF_TERM]) -> NIF_TERM { - rustler_sys::enif_make_list_from_array(env, arr.as_ptr(), arr.len() as u32) + enif_make_list_from_array(env, arr.as_ptr(), arr.len() as u32) } pub unsafe fn make_list_cell(env: NIF_ENV, head: NIF_TERM, tail: NIF_TERM) -> NIF_TERM { - rustler_sys::enif_make_list_cell(env, head, tail) + enif_make_list_cell(env, head, tail) } pub unsafe fn make_reverse_list(env: NIF_ENV, list: NIF_TERM) -> Option { let mut list_out = MaybeUninit::uninit(); - let success = rustler_sys::enif_make_reverse_list(env, list, list_out.as_mut_ptr()); + let success = enif_make_reverse_list(env, list, list_out.as_mut_ptr()); if success != 1 { return None; diff --git a/rustler/src/wrapper/map.rs b/rustler/src/wrapper/map.rs index 9a0a81e9..639925ee 100644 --- a/rustler/src/wrapper/map.rs +++ b/rustler/src/wrapper/map.rs @@ -1,10 +1,21 @@ pub use crate::wrapper::ErlNifMapIterator; -use crate::wrapper::{ErlNifMapIteratorEntry, NIF_ENV, NIF_TERM}; +use crate::{ + sys::{ + enif_get_map_size, enif_get_map_value, enif_make_map_from_arrays, enif_make_map_put, + enif_make_map_remove, enif_make_map_update, enif_make_new_map, enif_map_iterator_prev, + }, + wrapper::{ErlNifMapIteratorEntry, NIF_ENV, NIF_TERM}, +}; use std::mem::MaybeUninit; +use super::{ + enif_map_iterator_create, enif_map_iterator_destroy, enif_map_iterator_get_pair, + enif_map_iterator_next, +}; + pub unsafe fn get_map_value(env: NIF_ENV, map: NIF_TERM, key: NIF_TERM) -> Option { let mut result = MaybeUninit::uninit(); - let success = rustler_sys::enif_get_map_value(env, map, key, result.as_mut_ptr()); + let success = enif_get_map_value(env, map, key, result.as_mut_ptr()); if success != 1 { return None; @@ -14,7 +25,7 @@ pub unsafe fn get_map_value(env: NIF_ENV, map: NIF_TERM, key: NIF_TERM) -> Optio pub unsafe fn get_map_size(env: NIF_ENV, map: NIF_TERM) -> Option { let mut size = MaybeUninit::uninit(); - let success = rustler_sys::enif_get_map_size(env, map, size.as_mut_ptr()); + let success = enif_get_map_size(env, map, size.as_mut_ptr()); if success != 1 { return None; @@ -23,7 +34,7 @@ pub unsafe fn get_map_size(env: NIF_ENV, map: NIF_TERM) -> Option { } pub unsafe fn map_new(env: NIF_ENV) -> NIF_TERM { - rustler_sys::enif_make_new_map(env) + enif_make_new_map(env) } pub unsafe fn map_put( @@ -33,7 +44,7 @@ pub unsafe fn map_put( value: NIF_TERM, ) -> Option { let mut result = MaybeUninit::uninit(); - let success = rustler_sys::enif_make_map_put(env, map, key, value, result.as_mut_ptr()); + let success = enif_make_map_put(env, map, key, value, result.as_mut_ptr()); if success != 1 { return None; @@ -43,7 +54,7 @@ pub unsafe fn map_put( pub unsafe fn map_remove(env: NIF_ENV, map: NIF_TERM, key: NIF_TERM) -> Option { let mut result = MaybeUninit::uninit(); - let success = rustler_sys::enif_make_map_remove(env, map, key, result.as_mut_ptr()); + let success = enif_make_map_remove(env, map, key, result.as_mut_ptr()); if success != 1 { return None; @@ -58,7 +69,7 @@ pub unsafe fn map_update( new_value: NIF_TERM, ) -> Option { let mut result = MaybeUninit::uninit(); - let success = rustler_sys::enif_make_map_update(env, map, key, new_value, result.as_mut_ptr()); + let success = enif_make_map_update(env, map, key, new_value, result.as_mut_ptr()); if success != 1 { return None; @@ -78,7 +89,7 @@ pub unsafe fn map_iterator_create( entry: MapIteratorEntry, ) -> Option { let mut iter = MaybeUninit::uninit(); - let success = rustler_sys::enif_map_iterator_create( + let success = enif_map_iterator_create( env, map, iter.as_mut_ptr(), @@ -95,7 +106,7 @@ pub unsafe fn map_iterator_create( } pub unsafe fn map_iterator_destroy(env: NIF_ENV, iter: &mut ErlNifMapIterator) { - rustler_sys::enif_map_iterator_destroy(env, iter); + enif_map_iterator_destroy(env, iter); } pub unsafe fn map_iterator_get_pair( @@ -104,8 +115,7 @@ pub unsafe fn map_iterator_get_pair( ) -> Option<(NIF_TERM, NIF_TERM)> { let mut key = MaybeUninit::uninit(); let mut value = MaybeUninit::uninit(); - if rustler_sys::enif_map_iterator_get_pair(env, iter, key.as_mut_ptr(), value.as_mut_ptr()) == 0 - { + if enif_map_iterator_get_pair(env, iter, key.as_mut_ptr(), value.as_mut_ptr()) == 0 { None } else { Some((key.assume_init(), value.assume_init())) @@ -113,11 +123,11 @@ pub unsafe fn map_iterator_get_pair( } pub unsafe fn map_iterator_next(env: NIF_ENV, iter: &mut ErlNifMapIterator) { - rustler_sys::enif_map_iterator_next(env, iter); + enif_map_iterator_next(env, iter); } pub unsafe fn map_iterator_prev(env: NIF_ENV, iter: &mut ErlNifMapIterator) { - rustler_sys::enif_map_iterator_prev(env, iter); + enif_map_iterator_prev(env, iter); } pub unsafe fn make_map_from_arrays( @@ -126,7 +136,7 @@ pub unsafe fn make_map_from_arrays( values: &[NIF_TERM], ) -> Option { let mut map = MaybeUninit::uninit(); - if rustler_sys::enif_make_map_from_arrays( + if enif_make_map_from_arrays( env, keys.as_ptr(), values.as_ptr(), diff --git a/rustler/src/wrapper/pid.rs b/rustler/src/wrapper/pid.rs index 1a28992b..71cf017e 100644 --- a/rustler/src/wrapper/pid.rs +++ b/rustler/src/wrapper/pid.rs @@ -1,18 +1,20 @@ use crate::wrapper::{ErlNifPid, NIF_ENV, NIF_TERM}; use std::mem::MaybeUninit; +use crate::sys::{enif_get_local_pid, enif_make_pid}; + pub unsafe fn get_local_pid(env: NIF_ENV, term: NIF_TERM) -> Option { let mut pid = MaybeUninit::uninit(); - if rustler_sys::enif_get_local_pid(env, term, pid.as_mut_ptr()) == 0 { + if enif_get_local_pid(env, term, pid.as_mut_ptr()) == 0 { return None; } Some(pid.assume_init()) } // pub unsafe fn is_process_alive(env: NIF_ENV, pid: &ErlNifPid) -> bool { -// rustler_sys::enif_is_process_alive(env, pid) != 0 +// enif_is_process_alive(env, pid) != 0 // } pub unsafe fn make_pid(env: NIF_ENV, pid: ErlNifPid) -> NIF_TERM { - rustler_sys::enif_make_pid(env, pid) + enif_make_pid(env, pid) } diff --git a/rustler/src/wrapper/term.rs b/rustler/src/wrapper/term.rs index 3fd0f3d1..14019b27 100644 --- a/rustler/src/wrapper/term.rs +++ b/rustler/src/wrapper/term.rs @@ -1,6 +1,6 @@ +use crate::sys::{c_char, enif_snprintf}; use crate::wrapper::NIF_TERM; use std::fmt; -use std::os::raw::c_char; pub fn fmt(term: NIF_TERM, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { const SIZE: usize = 1024; @@ -9,7 +9,7 @@ pub fn fmt(term: NIF_TERM, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let mut n = 0; for _ in 0..10 { let i = unsafe { - rustler_sys::enif_snprintf!( + enif_snprintf!( bytes.as_mut_ptr() as *mut c_char, bytes.capacity(), b"%T\x00" as *const u8 as *const c_char, diff --git a/rustler/src/wrapper/tuple.rs b/rustler/src/wrapper/tuple.rs index 233ae74f..6aa0cc50 100644 --- a/rustler/src/wrapper/tuple.rs +++ b/rustler/src/wrapper/tuple.rs @@ -1,10 +1,11 @@ +use crate::sys::{enif_get_tuple, enif_make_tuple_from_array}; use crate::wrapper::{c_int, NIF_ENV, NIF_ERROR, NIF_TERM}; use std::mem::MaybeUninit; pub unsafe fn get_tuple<'a>(env: NIF_ENV, term: NIF_TERM) -> Result<&'a [NIF_TERM], NIF_ERROR> { let mut arity: c_int = 0; let mut array_ptr = MaybeUninit::uninit(); - let success = rustler_sys::enif_get_tuple(env, term, &mut arity, array_ptr.as_mut_ptr()); + let success = enif_get_tuple(env, term, &mut arity, array_ptr.as_mut_ptr()); if success != 1 { return Err(NIF_ERROR::BAD_ARG); } @@ -13,5 +14,5 @@ pub unsafe fn get_tuple<'a>(env: NIF_ENV, term: NIF_TERM) -> Result<&'a [NIF_TER } pub unsafe fn make_tuple(env: NIF_ENV, terms: &[NIF_TERM]) -> NIF_TERM { - rustler_sys::enif_make_tuple_from_array(env, terms.as_ptr(), terms.len() as u32) + enif_make_tuple_from_array(env, terms.as_ptr(), terms.len() as u32) } diff --git a/rustler_sys/.travis.yml b/rustler_sys/.travis.yml deleted file mode 100644 index c6c13d26..00000000 --- a/rustler_sys/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: erlang -otp_release: - - 21.0 - -sudo: required - -script: cargo test - -before_install: - - curl https://sh.rustup.rs -sSf | sh -s -- -y - -before_script: - - source $HOME/.cargo/env - - kerl list installations - - kerl list releases diff --git a/rustler_sys/Cargo.toml b/rustler_sys/Cargo.toml deleted file mode 100644 index 59c065f5..00000000 --- a/rustler_sys/Cargo.toml +++ /dev/null @@ -1,48 +0,0 @@ -[package] -name = "rustler_sys" - -# In order to ensure no breakage occurs for depending -# crates, Semver is used in a non-standard way for -# this crate. -# -# The MAJOR version of this crate represents the -# major version of the NIF api. -# The following versions are defined: -# 0: ^0 (0.1) -# 1: ^1 (1.0) -# 2: ^2 (2.0, 2.1, 2.2, etc) -# -# The MINOR version of this crate represents breaking -# changes in the API of the crate. -# -# The PATCH version is as standard. -# -# When depending on this crate, you should ALWAYS -# use a tilde requirements with AT LEAST `~MAJOR.MINOR`. -# Example: "~2.0" -version = "2.4.3" - -authors = ["Daniel Goertzen "] -description = "Create Erlang NIF modules in Rust using the C NIF API." -documentation = "https://docs.rs/rustler_sys" -repository = "https://github.com/rusterlium/rustler" -license = "MIT/Apache-2.0" -keywords = ["FFI", "Erlang", "NIF"] -edition = "2021" - -build = "build.rs" - -categories = ["external-ffi-bindings"] - -[features] -default = ["nif_version_2_15"] -nif_version_2_14 = [] -nif_version_2_15 = ["nif_version_2_14"] -nif_version_2_16 = ["nif_version_2_15"] -nif_version_2_17 = ["nif_version_2_16"] - -[dependencies] -libloading = "0.8" - -[build-dependencies] -regex-lite = "0.1" diff --git a/rustler_sys/appveyor.yml b/rustler_sys/appveyor.yml deleted file mode 100644 index 493c65ba..00000000 --- a/rustler_sys/appveyor.yml +++ /dev/null @@ -1,133 +0,0 @@ -# Appveyor configuration template for Rust using rustup for Rust installation -# https://github.com/starkat99/appveyor-rust - -## Operating System (VM environment) ## - -# Rust needs at least Visual Studio 2013 Appveyor OS for MSVC targets. -os: Visual Studio 2015 - -## Build Matrix ## - -# This configuration will setup a build for each channel & target combination (12 windows -# combinations in all). -# -# There are 3 channels: stable, beta, and nightly. -# -# Alternatively, the full version may be specified for the channel to build using that specific -# version (e.g. channel: 1.5.0) -# -# The values for target are the set of windows Rust build targets. Each value is of the form -# -# ARCH-pc-windows-TOOLCHAIN -# -# Where ARCH is the target architecture, either x86_64 or i686, and TOOLCHAIN is the linker -# toolchain to use, either msvc or gnu. See https://www.rust-lang.org/downloads.html#win-foot for -# a description of the toolchain differences. -# See https://github.com/rust-lang-nursery/rustup.rs/#toolchain-specification for description of -# toolchains and host triples. -# -# Comment out channel/target combos you do not wish to build in CI. -# -# You may use the `cargoflags` and `RUSTFLAGS` variables to set additional flags for cargo commands -# and rustc, respectively. For instance, you can uncomment the cargoflags lines in the nightly -# channels to enable unstable features when building for nightly. Or you could add additional -# matrix entries to test different combinations of features. -environment: - matrix: - -### MSVC Toolchains ### - - # Stable 64-bit MSVC - - channel: stable - target: x86_64-pc-windows-msvc - # Stable 32-bit MSVC - # - channel: stable - # target: i686-pc-windows-msvc - # # Beta 64-bit MSVC - # - channel: beta - # target: x86_64-pc-windows-msvc - # # Beta 32-bit MSVC - # - channel: beta - # target: i686-pc-windows-msvc - # # Nightly 64-bit MSVC - # - channel: nightly - # target: x86_64-pc-windows-msvc - # #cargoflags: --features "unstable" - # # Nightly 32-bit MSVC - # - channel: nightly - # target: i686-pc-windows-msvc - # #cargoflags: --features "unstable" - -### GNU Toolchains ### - - # # Stable 64-bit GNU - # - channel: stable - # target: x86_64-pc-windows-gnu - # # Stable 32-bit GNU - # - channel: stable - # target: i686-pc-windows-gnu - # # Beta 64-bit GNU - # - channel: beta - # target: x86_64-pc-windows-gnu - # # Beta 32-bit GNU - # - channel: beta - # target: i686-pc-windows-gnu - # # Nightly 64-bit GNU - # - channel: nightly - # target: x86_64-pc-windows-gnu - # #cargoflags: --features "unstable" - # # Nightly 32-bit GNU - # - channel: nightly - # target: i686-pc-windows-gnu - # #cargoflags: --features "unstable" - -### Allowed failures ### - -# See Appveyor documentation for specific details. In short, place any channel or targets you wish -# to allow build failures on (usually nightly at least is a wise choice). This will prevent a build -# or test failure in the matching channels/targets from failing the entire build. -matrix: - allow_failures: - - channel: nightly - -# If you only care about stable channel build failures, uncomment the following line: - #- channel: beta - -## Install Script ## - -# This is the most important part of the Appveyor configuration. This installs the version of Rust -# specified by the 'channel' and 'target' environment variables from the build matrix. This uses -# rustup to install Rust. -# -# For simple configurations, instead of using the build matrix, you can simply set the -# default-toolchain and default-host manually here. -install: - - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - - rustup-init -yv --default-toolchain %channel% --default-host %target% - - set PATH=%PATH%;%USERPROFILE%\.cargo\bin - - rustc -vV - - cargo -vV - - ################################# - # Download and install Erlang - ################################# - - - echo Downloading Erlang... - - ps: (new-object System.Net.WebClient).Downloadfile('http://www.erlang.org/download/otp_win64_21.0.exe', 'C:\Users\appveyor\erlang.exe') - - echo Installing Erlang... - - start /B /WAIT C:\Users\appveyor\erlang.exe /S /D=C:\Users\appveyor\erlang - - set ERLANG_HOME=C:\Users\appveyor\erlang - - set PATH=C:\Users\appveyor\erlang\bin;%PATH% - - echo %PATH% - -## Build Script ## - -# 'cargo test' takes care of building for us, so disable Appveyor's build stage. This prevents -# the "directory does not contain a project or solution file" error. -build: false - -# Uses 'cargo test' to run tests and build. Alternatively, the project may call compiled programs -#directly or perform other testing commands. Rust will automatically be placed in the PATH -# environment variable. -test_script: - - cargo test --verbose %cargoflags% diff --git a/rustler_sys/get_erts_path.erl b/rustler_sys/get_erts_path.erl deleted file mode 100644 index b0e038d8..00000000 --- a/rustler_sys/get_erts_path.erl +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/escript - -main(_) -> - io:format("~s/erts-~s/include/", - [code:root_dir(), - erlang:system_info(version)]). diff --git a/rustler_sys/tests/struct_size.c b/rustler_sys/tests/struct_size.c deleted file mode 100644 index 7575492f..00000000 --- a/rustler_sys/tests/struct_size.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -int main() { - printf("ERL_NIF_UINT %lu\n", sizeof(ERL_NIF_UINT)); - printf("ERL_NIF_TERM %lu\n", sizeof(ERL_NIF_TERM)); - printf("ErlNifFunc %lu\n", sizeof(ErlNifFunc)); - printf("ErlNifEntry %lu\n", sizeof(ErlNifEntry)); - printf("ErlNifBinary %lu\n", sizeof(ErlNifBinary)); - printf("ErlNifPid %lu\n", sizeof(ErlNifPid)); - printf("ErlNifSysInfo %lu\n", sizeof(ErlNifSysInfo)); - printf("ErlNifMapIterator %lu\n", sizeof(ErlNifMapIterator)); - - return 0; -} diff --git a/rustler_sys/tests/struct_size.rs b/rustler_sys/tests/struct_size.rs deleted file mode 100644 index 8fd577e1..00000000 --- a/rustler_sys/tests/struct_size.rs +++ /dev/null @@ -1,111 +0,0 @@ -#[cfg(unix)] -#[test] -fn test1() { - use rustler_sys::*; - use std::collections::HashMap; - use std::env; - use std::mem::size_of; - use std::path::Path; - use std::process::Command; - - let out_dir = env::var("OUT_DIR").unwrap(); - - // use environment escript and cc if available - let escript = env::var("ESCRIPT").unwrap_or_else(|_| "escript".to_string()); - let cc = env::var("CC").unwrap_or_else(|_| "cc".to_string()); - - // get erts include path - let erts_include = Command::new(escript) - .arg("get_erts_path.erl") - .output() - .map_err(|_| "Can't run escript") - .map(|out| { - if out.status.success() { - out.stdout - } else { - panic!("Can't run get_erts_path.erl") - } - }) - .map(String::from_utf8) - .unwrap() - .unwrap(); - - //println!("include {:?}", erts_include); - - // Compile C program - let exe = Path::new(&out_dir).join("struct_size"); - if !Command::new(cc) - .arg("-o") - .arg(&exe) - .arg("-I") - .arg(erts_include) - .arg("tests/struct_size.c") - .status() - .map_err(|_| "Can't find c compiler (cc or value of environment CC)") - .unwrap() - .success() - { - panic!("Can't compile struct_size.c") - } - - // Run C program that lists C NIF runtime information. - let stdout: Vec = Command::new(&exe) - .output() - .map_err(|_| "Can't run C runtime information program") - .unwrap() - .stdout; - - let output: &str = std::str::from_utf8(&stdout).unwrap(); - - // Parse C program output into hashmap of (&str, u32) - let sizemap: HashMap<&str, usize> = output - .lines() - .map(|ln| ln.split(' ')) - .map(|mut it| (it.next().unwrap(), it.next().unwrap().parse().unwrap())) - .collect(); - - /* types to check are: - - ERL_NIF_UINT - ERL_NIF_TERM - ErlNifFunc - ErlNifEntry - ErlNifBinary - ErlNifPid - ErlNifSysInfo - ErlNifMapIterator - - */ - - assert_eq!( - &size_of::(), - sizemap.get("ERL_NIF_UINT").unwrap() - ); - assert_eq!( - &size_of::(), - sizemap.get("ERL_NIF_TERM").unwrap() - ); - assert_eq!(&size_of::(), sizemap.get("ErlNifFunc").unwrap()); - - // Disabling this test because struct size grew in otp-20 but remains - // backwards compatible. - - //assert_eq!( - //&size_of::(), - //sizemap.get("ErlNifEntry").unwrap() - //); - - //assert_eq!( - //&size_of::(), - //sizemap.get("ErlNifBinary").unwrap() - //); - assert_eq!(&size_of::(), sizemap.get("ErlNifPid").unwrap()); - assert_eq!( - &size_of::(), - sizemap.get("ErlNifSysInfo").unwrap() - ); - assert_eq!( - &size_of::(), - sizemap.get("ErlNifMapIterator").unwrap() - ); -}