pub struct JsRc<T: NapiRaw> { /* private fields */ }Expand description
Reference counter for JavaScript values.
Moves ownership of a JavaScript value to the Rust addon, preventing Nodejs’s GC from dropping the value.
The value will be dropped when the reference count goes to 0
§Usage:
§From Napi Function Argument
use napi::*;
use napi_derive::napi;
use napi_async_local::JsRc;
#[napi]
fn my_js_func(env: Env, callback: JsRc<JsFunction>) -> napi::Result<()> {
let inner = callback.inner(&env)?;
inner.call_without_args(None);
Ok(())
}§Apply to Existing Value
Simplified with super::JsRcExt
use napi::*;
use napi_derive::napi;
use napi_async_local::JsRc;
use napi_async_local::UtilsExt;
#[napi]
fn my_js_func(env: Env) -> napi::Result<()> {
let value = env.create_string("hello world")?;
let value_ref = JsRc::new(value)?;
let inner = value_ref.inner(&env)?;
env.console_log(&[inner])?; // From UtilsExt
Ok(())
}§Apply to Existing Value super::JsRcExt
use napi::*;
use napi_derive::napi;
use napi_async_local::JsRcExt;
use napi_async_local::UtilsExt;
#[napi]
fn my_js_func(env: Env) -> napi::Result<()> {
let value = env.create_string("hello world")?;
let value_ref = value.into_rc(&env)?; // From JsRcExt
let inner = value_ref.inner(&env)?;
env.console_log(&[inner])?; // From UtilsExt
Ok(())
}Implementations§
Trait Implementations§
Source§impl<T: NapiValue> FromNapiValue for JsRc<T>
impl<T: NapiValue> FromNapiValue for JsRc<T>
Source§unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> Result<Self>
unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> Result<Self>
Safety Read more
fn from_unknown(value: JsUnknown) -> Result<Self, Error>
Auto Trait Implementations§
impl<T> Freeze for JsRc<T>
impl<T> !RefUnwindSafe for JsRc<T>
impl<T> !Send for JsRc<T>
impl<T> !Sync for JsRc<T>
impl<T> Unpin for JsRc<T>where
T: Unpin,
impl<T> !UnwindSafe for JsRc<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more