use std::ops::Deref; use std::ptr; use crate::check_status; use crate::{sys, Env, NapiValue, Result}; pub struct EscapableHandleScope { handle_scope: sys::napi_escapable_handle_scope, value: T, } impl EscapableHandleScope { #[inline] pub fn open(env: sys::napi_env, value: T) -> Result { let mut handle_scope = ptr::null_mut(); check_status!(unsafe { sys::napi_open_escapable_handle_scope(env, &mut handle_scope) })?; let mut result = ptr::null_mut(); check_status!(unsafe { sys::napi_escape_handle(env, handle_scope, NapiValue::raw(&value), &mut result) })?; Ok(Self { value, handle_scope, }) } pub fn close(self, env: Env) -> Result<()> { check_status!(unsafe { sys::napi_close_escapable_handle_scope(env.0, self.handle_scope) }) } } impl Deref for EscapableHandleScope { type Target = T; fn deref(&self) -> &T { &self.value } }