feat(napi): support external deref/derefMut

This commit is contained in:
h-a-n-a 2021-12-11 13:42:46 +08:00
parent 212bbe285e
commit 0aa7cd3d32
2 changed files with 20 additions and 3 deletions

View file

@ -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<T: 'static> AsMut<T> for External<T> {
}
}
impl<T: 'static> Deref for External<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl<T: 'static> DerefMut for External<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut()
}
}
impl<T: 'static> ToNapiValue for External<T> {
unsafe fn to_napi_value(
env: napi_sys::napi_env,

View file

@ -12,10 +12,10 @@ pub fn create_external_string(content: String) -> External<String> {
#[napi]
pub fn get_external(external: External<u32>) -> u32 {
*external.as_ref()
*external
}
#[napi]
pub fn mutate_external(mut external: External<u32>, new_val: u32) {
*external.as_mut() = new_val;
*external = new_val;
}