Struct neon::handle::Root

source ·
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>

source

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
source

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);
source

pub fn drop<'a, C: Context<'a>>(self, cx: &mut C)

Safely drop a Root<T> without returning the referenced JavaScript object.

source

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.

source

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<T> Debug for Root<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Drop for Root<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: Object> Finalize for Root<T>

source§

fn finalize<'a, C: Context<'a>>(self, cx: &mut C)

source§

impl<T> Send for Root<T>

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.