Struct neon::types::JsBox[][src]

pub struct JsBox<T: Send + 'static> { /* fields omitted */ }
Expand description

A smart pointer for Rust data managed by the JavaScript engine.

The type JsBox<T> provides shared ownership of a value of type T, allocated in the heap. The data is owned by the JavaScript engine and the lifetime is managed by the JavaScript garbage collector.

Shared references in Rust disallow mutation by default, and JsBox is no exception: you cannot generally obtain a mutable reference to something inside a JsBox. If you need to mutate through a JsBox, use Cell, RefCell, or one of the other types that provide interior mutability.

Values contained by a JsBox must implement the Finalize trait. Finalize::finalize will execute with the value in a JsBox immediately before the JsBox is garbage collected. If no additional finalization is necessary, an emply implementation may be provided.

Deref behavior

JsBox<T> automatically dereferences to T (via the Deref trait), so you can call T’s method on a value of type JsBox<T>.

let vec: Handle<JsBox<Vec<_>>> = cx.boxed(vec![1, 2, 3]);

println!("Length: {}", vec.len());

Examples

Passing some immutable data between Rust and JavaScript.

fn create_path(mut cx: FunctionContext) -> JsResult<JsBox<PathBuf>> {
    let path = cx.argument::<JsString>(0)?.value(&mut cx);
    let path = Path::new(&path).to_path_buf();

    Ok(cx.boxed(path))
}

fn print_path(mut cx: FunctionContext) -> JsResult<JsUndefined> {
    let path = cx.argument::<JsBox<PathBuf>>(0)?;

    println!("{}", path.display());

    Ok(cx.undefined())
}

Passing a user defined struct wrapped in a RefCell for mutability. This pattern is useful for creating classes in JavaScript.


type BoxedPerson = JsBox<RefCell<Person>>;

struct Person {
     name: String,
}

impl Finalize for Person {}

impl Person {
    pub fn new(name: String) -> Self {
        Person { name }
    }

    pub fn set_name(&mut self, name: String) {
        self.name = name;
    }

    pub fn greet(&self) -> String {
        format!("Hello, {}!", self.name)
    }
}

fn person_new(mut cx: FunctionContext) -> JsResult<BoxedPerson> {
    let name = cx.argument::<JsString>(0)?.value(&mut cx);
    let person = RefCell::new(Person::new(name));

    Ok(cx.boxed(person))
}

fn person_set_name(mut cx: FunctionContext) -> JsResult<JsUndefined> {
    let person = cx.argument::<BoxedPerson>(0)?;
    let mut person = person.borrow_mut();
    let name = cx.argument::<JsString>(1)?.value(&mut cx);

    person.set_name(name);

    Ok(cx.undefined())
}

fn person_greet(mut cx: FunctionContext) -> JsResult<JsString> {
    let person = cx.argument::<BoxedPerson>(0)?;
    let person = person.borrow();
    let greeting = person.greet();

    Ok(cx.string(greeting))
}

Implementations

impl<T: Finalize + Send + 'static> JsBox<T>[src]

Values contained by a JsBox must be Finalize + Send + 'static

Finalize

The neon::prelude::Finalize trait provides a finalize method that will be called immediately before the JsBox is garbage collected.

Send

JsBox may be moved across threads. It is important to guarantee that the contents is also safe to move across threads.

`‘static’

The lifetime of a JsBox is managed by the JavaScript garbage collector. Since Rust is unable to verify the lifetime of the contents, references must be valid for the entire duration of the program. This does not mean that the JsBox will be valid until the application terminates, only that its lifetime is indefinite.

pub fn new<'a, C>(cx: &mut C, value: T) -> Handle<'a, JsBox<T>> where
    C: Context<'a>,
    T: Send + 'static, 
[src]

Constructs a new JsBox containing value.

Trait Implementations

impl<T: Send + 'static> Clone for JsBox<T>[src]

fn clone(&self) -> Self[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Send + 'static> Debug for JsBox<T>[src]

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

Formats the value using the given formatter. Read more

impl<'a, T: Send + 'static> Deref for JsBox<T>[src]

type Target = T

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Dereferences the value.

impl<T: Send + 'static> Managed for JsBox<T>[src]

fn to_raw(self) -> Local[src]

fn from_raw(env: Env, local: Local) -> Self[src]

impl<T: Send + 'static> Object for JsBox<T>[src]

fn get<'a, C: Context<'a>, K: PropertyKey>(
    self,
    cx: &mut C,
    key: K
) -> NeonResult<Handle<'a, JsValue>>
[src]

fn get_own_property_names<'a, C: Context<'a>>(
    self,
    cx: &mut C
) -> JsResult<'a, JsArray>
[src]

fn set<'a, C: Context<'a>, K: PropertyKey, W: Value>(
    self,
    cx: &mut C,
    key: K,
    val: Handle<'_, W>
) -> NeonResult<bool>
[src]

fn root<'a, C: Context<'a>>(&self, cx: &mut C) -> Root<Self>[src]

impl<T: Send + 'static> Value for JsBox<T>[src]

fn to_string<'a, C: Context<'a>>(self, cx: &mut C) -> JsResult<'a, JsString>[src]

fn as_value<'a, C: Context<'a>>(self, _: &mut C) -> Handle<'a, JsValue>[src]

impl<T: Send + 'static> Copy for JsBox<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for JsBox<T> where
    T: RefUnwindSafe

impl<T> !Send for JsBox<T>

impl<T> !Sync for JsBox<T>

impl<T> Unpin for JsBox<T>

impl<T> UnwindSafe for JsBox<T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.