Merge pull request #919 from h-a-n-a/feat/deref-external

feat(napi): support external deref
This commit is contained in:
LongYinan 2021-12-11 15:09:11 +08:00 committed by GitHub
commit 59a7b8621e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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;
}