diff --git a/crates/neon/src/types_impl/boxed.rs b/crates/neon/src/types_impl/boxed.rs index ac3418838..5283bf456 100644 --- a/crates/neon/src/types_impl/boxed.rs +++ b/crates/neon/src/types_impl/boxed.rs @@ -234,14 +234,10 @@ impl ValueInternal for JsBox { /// until the application terminates, only that its lifetime is indefinite. impl JsBox { /// Constructs a new `JsBox` containing `value`. - pub fn new<'a, C>(cx: &mut C, value: T) -> Handle<'a, JsBox> - where - C: Context<'a>, - T: 'static, - { + pub fn new<'cx, C: Context<'cx>>(cx: &mut C, value: T) -> Handle<'cx, JsBox> { // This function will execute immediately before the `JsBox` is garbage collected. // It unwraps the `napi_external`, downcasts the `BoxAny` and moves the type - // out of the `Box`. Lastly, it calls the trait method `Finalize::fianlize` of the + // out of the `Box`. Lastly, it calls the trait method `Finalize::finalize` of the // contained value `T`. fn finalizer(env: raw::Env, data: BoxAny) { let data = *data.downcast::().unwrap(); @@ -250,29 +246,29 @@ impl JsBox { Cx::with_context(env, move |mut cx| data.finalize(&mut cx)); } - let v = Box::new(value) as BoxAny; - // Since this value was just constructed, we know it is `T` - let raw_data = &*v as *const dyn Any as *const T; - let local = unsafe { external::create(cx.env().to_raw(), v, finalizer::) }; - - Handle::new_internal(Self(JsBoxInner { local, raw_data })) + Self::create_external(cx, value, finalizer::) } } impl JsBox { - pub(crate) fn manually_finalize<'a, C>(cx: &mut C, value: T) -> Handle<'a, JsBox> - where - C: Context<'a>, - T: 'static, - { + pub(crate) fn manually_finalize<'cx>(cx: &mut Cx<'cx>, value: T) -> Handle<'cx, JsBox> { fn finalizer(_env: raw::Env, _data: BoxAny) {} + Self::create_external(cx, value, finalizer) + } + + fn create_external<'cx, C: Context<'cx>>( + cx: &mut C, + value: T, + finalizer: fn(raw::Env, BoxAny), + ) -> Handle<'cx, JsBox> { let v = Box::new(value) as BoxAny; + // Since this value was just constructed, we know it is `T` let raw_data = &*v as *const dyn Any as *const T; let local = unsafe { external::create(cx.env().to_raw(), v, finalizer) }; - Handle::new_internal(Self(JsBoxInner { local, raw_data })) + Handle::new_internal(JsBox(JsBoxInner { local, raw_data })) } } diff --git a/crates/neon/src/types_impl/extract/container.rs b/crates/neon/src/types_impl/extract/container.rs index 8c399b551..e31678e7d 100644 --- a/crates/neon/src/types_impl/extract/container.rs +++ b/crates/neon/src/types_impl/extract/container.rs @@ -83,7 +83,7 @@ impl<'cx, T: 'static> TryFromJs<'cx> for Rc { v: Handle<'cx, JsValue>, ) -> NeonResult> { match v.downcast::>, _>(cx) { - Ok(v) => Ok(Ok(JsBox::deref(&v).clone())), + Ok(v) => Ok(Ok(Rc::clone(&v))), Err(_) => Ok(Err(TypeExpected::new())), } } @@ -108,7 +108,7 @@ impl<'cx, T: 'static> TryFromJs<'cx> for Arc { v: Handle<'cx, JsValue>, ) -> NeonResult> { match v.downcast::>, _>(cx) { - Ok(v) => Ok(Ok(JsBox::deref(&v).clone())), + Ok(v) => Ok(Ok(Arc::clone(&v))), Err(_) => Ok(Err(TypeExpected::new())), } } diff --git a/crates/neon/src/types_impl/extract/private.rs b/crates/neon/src/types_impl/extract/private.rs index b6dafbc97..211b5fe99 100644 --- a/crates/neon/src/types_impl/extract/private.rs +++ b/crates/neon/src/types_impl/extract/private.rs @@ -59,8 +59,8 @@ impl Sealed for Arc {} impl Sealed for Rc {} -impl<'a, T> Sealed for Ref<'a, T> {} +impl Sealed for Ref<'_, T> {} -impl<'a, T> Sealed for RefMut<'a, T> {} +impl Sealed for RefMut<'_, T> {} impl_sealed!(u8, u16, u32, i8, i16, i32, f32, f64, bool, String, Date, Throw, Error,);