Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge rustler-sys into rustler::sys #653

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -16,5 +15,11 @@ members = [
default-members = [
"rustler",
"rustler_codegen",
"rustler_sys",
]

[profile.dev]
panic = "unwind"

[profile.release]
panic = "unwind"

17 changes: 7 additions & 10 deletions rustler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@ 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 }

[profile.dev]
panic = "unwind"

[profile.release]
panic = "unwind"
[build-dependencies]
regex-lite = "0.1"

[package.metadata.release]

Expand Down
6 changes: 3 additions & 3 deletions rustler_sys/build.rs → rustler/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ impl<'a> ApiBuilder for ForwardersApiBuilder<'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();
Expand All @@ -123,6 +122,7 @@ impl<'a> ApiBuilder for ForwardersApiBuilder<'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);
Expand Down
12 changes: 7 additions & 5 deletions rustler/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -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::<usize>();
const MAX_ALIGN: usize = 8;

Expand All @@ -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);
Expand All @@ -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
}
}

Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions rustler/src/codegen_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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;
Expand Down Expand Up @@ -83,7 +83,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,
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/dynamic.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}
}

Expand Down
17 changes: 7 additions & 10 deletions rustler/src/env.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(())
Expand All @@ -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(),
Expand Down Expand Up @@ -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() }),
}
}

Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
35 changes: 17 additions & 18 deletions rustler/src/resource/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -40,7 +43,7 @@ where
pub fn new(data: T) -> Self {
let alloc_size = get_alloc_size_struct::<T>();
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::<T>(mem_raw) as *mut T };

unsafe { ptr::write(aligned_mem, data) };
Expand Down Expand Up @@ -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,
Expand All @@ -93,17 +96,12 @@ where

fn from_term(term: Term) -> Result<Self, Error> {
let (raw, inner) = unsafe { term.try_get_resource_ptrs::<T>() }.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 {
Expand All @@ -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 {
Expand All @@ -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 }
}
}

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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()) };
}
}

Expand Down
4 changes: 2 additions & 2 deletions rustler/src/resource/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustler_sys::ErlNifMonitor;
use crate::sys::{enif_compare_monitors, ErlNifMonitor};

/// Handle for a monitor created using `ResourceArc<T>::monitor`.
///
Expand All @@ -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 }
}
}
15 changes: 7 additions & 8 deletions rustler/src/resource/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,7 +115,7 @@ impl Registration {
if T::IMPLEMENTS_DYNCALL {
Self {
init: ErlNifResourceTypeInit {
dyncall: resource_dyncall::<T> as *const rustler_sys::ErlNifResourceDynCall,
dyncall: resource_dyncall::<T> as *const crate::sys::ErlNifResourceDynCall,
members: max(self.init.members, 4),
..self.init
},
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/resource/term.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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,
Expand Down
Loading
Loading