diff --git a/napi/src/env.rs b/napi/src/env.rs index 2588a838..73a294d2 100644 --- a/napi/src/env.rs +++ b/napi/src/env.rs @@ -318,12 +318,14 @@ impl Env { /// Mostly the same with `create_buffer_with_data` /// /// Provided `finalize_callback` will be called when `Buffer` got dropped. + /// + /// You can pass in `noop_finalize` if you have nothing to do in finalize phase. pub unsafe fn create_buffer_with_borrowed_data( &self, data: *const u8, length: usize, hint: Hint, - finalize_callback: Option, + finalize_callback: Finalize, ) -> Result where Finalize: FnOnce(Hint, Env), @@ -1129,8 +1131,6 @@ unsafe extern "C" fn raw_finalize_with_custom_callback( ) where Finalize: FnOnce(Hint, Env), { - let (hint, maybe_callback) = *Box::from_raw(finalize_hint as *mut (Hint, Option)); - if let Some(callback) = maybe_callback { - callback(hint, Env::from_raw(env)); - }; + let (hint, callback) = *Box::from_raw(finalize_hint as *mut (Hint, Finalize)); + callback(hint, Env::from_raw(env)); } diff --git a/test_module/src/buffer.rs b/test_module/src/buffer.rs index 0b7dd157..622dabab 100644 --- a/test_module/src/buffer.rs +++ b/test_module/src/buffer.rs @@ -33,10 +33,8 @@ pub fn create_borrowed_buffer_with_noop_finalize(env: Env) -> ContextlessResult< let length = data.len(); let manually_drop = ManuallyDrop::new(data); - unsafe { - env.create_buffer_with_borrowed_data(data_ptr, length, manually_drop, Some(noop_finalize)) - } - .map(|b| Some(b.into_raw())) + unsafe { env.create_buffer_with_borrowed_data(data_ptr, length, manually_drop, noop_finalize) } + .map(|b| Some(b.into_raw())) } #[contextless_function] @@ -51,9 +49,9 @@ pub fn create_borrowed_buffer_with_finalize(env: Env) -> ContextlessResult>, _| { + |mut hint: ManuallyDrop>, _| { ManuallyDrop::drop(&mut hint); - }), + }, ) } .map(|b| Some(b.into_raw()))