Skip to content
Draft
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
18 changes: 0 additions & 18 deletions rustler/src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,3 @@ pub use monitor::Monitor;
pub use registration::Registration;
pub use traits::Resource;
use traits::ResourceExt;

/// Deprecated resource registration method
///
/// This macro will create a local `impl Resource` for the passed type and is thus incompatible
/// with upcoming Rust language changes. Please implement the `Resource` trait directly and
/// register it either using the `resource_impl` attribute or using the `Env::register` method:
/// ```ignore
/// fn on_load(env: Env) -> bool {
/// env.register::<ResourceType>().is_ok()
/// }
/// ```
#[macro_export]
macro_rules! resource {
($struct_name:ty, $env: ident) => {{
impl $crate::Resource for $struct_name {}
$env.register::<$struct_name>().is_ok()
}};
}
31 changes: 2 additions & 29 deletions rustler_codegen/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned};
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{Ident, Result, Token};

#[derive(Debug)]
pub struct InitMacroInput {
name: syn::Lit,
load: TokenStream,
maybe_warning: TokenStream,
}

impl Parse for InitMacroInput {
fn parse(input: ParseStream) -> Result<Self> {
let name = syn::Lit::parse(input)?;

let maybe_warning = if input.peek(syn::token::Comma) && input.peek2(syn::token::Bracket) {
// peeked, must be there
let _ = syn::token::Comma::parse(input).unwrap();
if let Ok(funcs) = syn::ExprArray::parse(input) {
quote_spanned!(funcs.span() =>
#[allow(dead_code)]
fn rustler_init() {
#[deprecated(
since = "0.34.0",
note = "Passing NIF functions explicitly is deprecated and this argument is ignored, please remove it"
)]
#[allow(non_upper_case_globals)]
const explicit_nif_functions: () = ();
let _ = explicit_nif_functions;
}
)
} else {
quote!()
}
} else {
quote!()
};

let options = parse_expr_assigns(input);
let load = extract_option(options, "load");

Ok(InitMacroInput {
name,
load,
maybe_warning,
load
})
}
}
Expand Down Expand Up @@ -81,7 +55,6 @@ impl From<InitMacroInput> for proc_macro2::TokenStream {
fn from(input: InitMacroInput) -> Self {
let name = input.name;
let load = input.load;
let maybe_warning = input.maybe_warning;

let inner = quote! {
static mut NIF_ENTRY: Option<rustler::codegen_runtime::DEF_NIF_ENTRY> = None;
Expand Down
3 changes: 1 addition & 2 deletions rustler_tests/native/rustler_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ mod test_term;
mod test_thread;
mod test_tuple;

// Intentional usage of the explicit form (in an "invalid" way, listing a wrong set of functions) to ensure that the warning stays alive
rustler::init!("Elixir.RustlerTest", [deprecated, usage], load = load);
rustler::init!("Elixir.RustlerTest", load = load);

fn load(env: rustler::Env, _: rustler::Term) -> bool {
test_resource::on_load(env)
Expand Down
7 changes: 4 additions & 3 deletions rustler_tests/native/rustler_test/src/test_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub struct TestResource {
test_field: RwLock<i32>,
}

#[rustler::resource_impl]
impl Resource for TestResource {}

struct TestMonitorResourceInner {
mon: Option<Monitor>,
down_called: bool,
Expand Down Expand Up @@ -42,9 +45,7 @@ pub struct WithBinaries {
impl Resource for WithBinaries {}

pub fn on_load(env: Env) -> bool {
rustler::resource!(TestResource, env)
&& env.register::<WithBinaries>().is_ok()
&& env.register::<ImmutableResource>().is_ok()
env.register::<WithBinaries>().is_ok() && env.register::<ImmutableResource>().is_ok()
}

#[rustler::nif]
Expand Down
Loading