From 0aa7cd3d325f1e798885a4048c8286c5688b54e9 Mon Sep 17 00:00:00 2001 From: h-a-n-a Date: Sat, 11 Dec 2021 13:42:46 +0800 Subject: [PATCH] feat(napi): support external deref/derefMut --- .../src/bindgen_runtime/js_values/external.rs | 19 ++++++++++++++++++- examples/napi/src/external.rs | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/napi/src/bindgen_runtime/js_values/external.rs b/crates/napi/src/bindgen_runtime/js_values/external.rs index 66f790ca..47ce4ef2 100644 --- a/crates/napi/src/bindgen_runtime/js_values/external.rs +++ b/crates/napi/src/bindgen_runtime/js_values/external.rs @@ -1,4 +1,7 @@ -use std::any::TypeId; +use std::{ + any::TypeId, + ops::{Deref, DerefMut}, +}; use crate::{check_status, Error, Status, TaggedObject}; @@ -73,6 +76,20 @@ impl AsMut for External { } } +impl Deref for External { + type Target = T; + + fn deref(&self) -> &Self::Target { + self.as_ref() + } +} + +impl DerefMut for External { + fn deref_mut(&mut self) -> &mut Self::Target { + self.as_mut() + } +} + impl ToNapiValue for External { unsafe fn to_napi_value( env: napi_sys::napi_env, diff --git a/examples/napi/src/external.rs b/examples/napi/src/external.rs index 699a8f80..581b2710 100644 --- a/examples/napi/src/external.rs +++ b/examples/napi/src/external.rs @@ -12,10 +12,10 @@ pub fn create_external_string(content: String) -> External { #[napi] pub fn get_external(external: External) -> u32 { - *external.as_ref() + *external } #[napi] pub fn mutate_external(mut external: External, new_val: u32) { - *external.as_mut() = new_val; + *external = new_val; }