pub struct Root<T> { /* private fields */ }
Expand description
A thread-safe handle that holds a reference to a JavaScript object and prevents it from being garbage collected.
A Root<T>
may be sent across threads, but the referenced object may
only be accessed on the JavaScript thread that created it.
Implementations§
Source§impl<T: Object> Root<T>
impl<T: Object> Root<T>
Sourcepub fn new<'a, C: Context<'a>>(cx: &mut C, value: &T) -> Self
pub fn new<'a, C: Context<'a>>(cx: &mut C, value: &T) -> Self
Create a reference to a JavaScript object. The object will not be
garbage collected until the Root
is dropped. A Root<T>
may only
be dropped on the JavaScript thread that created it.
The caller should ensure Root::into_inner
or Root::drop
is called
to properly dispose of the Root<T>
. If the value is dropped without
calling one of these methods:
- N-API < 6, Neon will
panic
to notify of the leak - N-API >= 6, Neon will drop from a global queue at a runtime cost
Sourcepub fn clone<'a, C: Context<'a>>(&self, cx: &mut C) -> Self
pub fn clone<'a, C: Context<'a>>(&self, cx: &mut C) -> Self
Clone a reference to the contained JavaScript object. This method can be considered identical to the following:
let inner = root.into_inner(&mut cx);
let cloned = inner.root(&mut cx);
let root = inner.root(&mut cx);
Sourcepub fn drop<'a, C: Context<'a>>(self, cx: &mut C)
pub fn drop<'a, C: Context<'a>>(self, cx: &mut C)
Safely drop a Root<T>
without returning the referenced JavaScript
object.
Sourcepub fn into_inner<'a, C: Context<'a>>(self, cx: &mut C) -> Handle<'a, T>
pub fn into_inner<'a, C: Context<'a>>(self, cx: &mut C) -> Handle<'a, T>
Return the referenced JavaScript object and allow it to be garbage collected.
§Panics
This method panics if it is called from a different JavaScript thread than the one in which the handle was created.
Sourcepub fn to_inner<'a, C: Context<'a>>(&self, cx: &mut C) -> Handle<'a, T>
pub fn to_inner<'a, C: Context<'a>>(&self, cx: &mut C) -> Handle<'a, T>
Access the inner JavaScript object without consuming the Root
This method aliases the reference without changing the reference count. It
can be used in place of a clone immediately followed by a call to into_inner
.
§Panics
This method panics if it is called from a different JavaScript thread than the one in which the handle was created.
Trait Implementations§
Source§impl<'cx, O> TryFromJs<'cx> for Root<O>where
O: Object,
impl<'cx, O> TryFromJs<'cx> for Root<O>where
O: Object,
type Error = TypeExpected<O>
Source§fn try_from_js(
cx: &mut Cx<'cx>,
v: Handle<'cx, JsValue>,
) -> NeonResult<Result<Self, Self::Error>>
fn try_from_js( cx: &mut Cx<'cx>, v: Handle<'cx, JsValue>, ) -> NeonResult<Result<Self, Self::Error>>
impl<T> Send for Root<T>
impl<T> Sync for Root<T>
Auto Trait Implementations§
impl<T> Freeze for Root<T>
impl<T> RefUnwindSafe for Root<T>where
T: RefUnwindSafe,
impl<T> Unpin for Root<T>where
T: Unpin,
impl<T> UnwindSafe for Root<T>where
T: UnwindSafe,
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more