Skip to content

Commit 86fa77f

Browse files
committed
fix(derive): fix lifetimes for functions without args
1 parent 8b0ccc6 commit 86fa77f

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

example/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
const addon = require('./build/Release/example.node');
44

5-
addon.hello(undefined);
5+
addon.hello();
66
console.log(addon.add(1, 2));

example/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ extern crate napi_derive;
66
use napi::{NapiArgs, NapiEnv, NapiNumber, NapiResult, NapiUndefined};
77

88
#[derive(NapiArgs)]
9-
struct HelloArgs<'a>(NapiUndefined<'a>);
9+
struct HelloArgs;
1010

11-
fn hello<'a>(
12-
env: &'a NapiEnv,
13-
_: HelloArgs<'a>,
14-
) -> NapiResult<NapiUndefined<'a>> {
11+
fn hello(env: &NapiEnv, _: HelloArgs) -> NapiResult<NapiUndefined> {
1512
println!("Hello from the Rust land!");
1613
NapiUndefined::new(env)
1714
}

napi-derive/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,16 @@ fn impl_napi_args(
7474
quote! { #name }
7575
};
7676

77+
let (gen_lifetime, ref_lifetime) = if count > 0 {
78+
(quote! { <'env> }, quote! { 'env })
79+
} else {
80+
(quote!{}, quote!{})
81+
};
82+
7783
Ok(quote! {
78-
impl<'env> NapiArgs<'env> for #name<'env> {
84+
impl<'env> NapiArgs<'env> for #name #gen_lifetime {
7985
fn from_cb_info(
80-
env: &'env ::napi::NapiEnv,
86+
env: & #ref_lifetime ::napi::NapiEnv,
8187
cb_info: ::napi::sys::napi_callback_info,
8288
) -> ::napi::NapiResult<Self> {
8389
use ::napi::sys;

0 commit comments

Comments
 (0)