pub struct JsBox<T: 'static>(/* private fields */);
Expand description
A JavaScript smart pointer object that owns Rust data.
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§
Source§impl<T: Finalize + 'static> JsBox<T>
Values contained by a JsBox
must be Finalize + 'static
impl<T: Finalize + 'static> JsBox<T>
Values contained by a JsBox
must be Finalize + 'static
§Finalize
The sys::prelude::Finalize
trait provides a finalize
method that will be called
immediately before the JsBox
is garbage collected.
§`‘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.
Source§impl<T: 'static> JsBox<T>
impl<T: 'static> JsBox<T>
Sourcepub fn deref<'cx>(v: &Handle<'cx, Self>) -> &'cx T
pub fn deref<'cx>(v: &Handle<'cx, Self>) -> &'cx T
Gets a reference to the inner value of a JsBox
. This method is similar to
dereferencing a JsBox
(e.g., &*boxed
), but the lifetime
is safely extended to 'cx
.
See also Handle<JsBox>::as_inner
.
Trait Implementations§
Source§impl<T: 'static> Object for JsBox<T>
impl<T: 'static> Object for JsBox<T>
Source§fn prop<'a, 'cx: 'a, K: PropertyKey>(
&self,
cx: &'a mut Cx<'cx>,
key: K,
) -> PropOptions<'a, 'cx, Self, K>
fn prop<'a, 'cx: 'a, K: PropertyKey>( &self, cx: &'a mut Cx<'cx>, key: K, ) -> PropOptions<'a, 'cx, Self, K>
PropOptions
for accessing a property. Read moreSource§fn method<'a, 'cx: 'a, K: PropertyKey>(
&self,
cx: &'a mut Cx<'cx>,
key: K,
) -> NeonResult<BindOptions<'a, 'cx>>
fn method<'a, 'cx: 'a, K: PropertyKey>( &self, cx: &'a mut Cx<'cx>, key: K, ) -> NeonResult<BindOptions<'a, 'cx>>
this
to the object. Read moreSource§fn get_opt<'a, V: Value, C: Context<'a>, K: PropertyKey>(
&self,
cx: &mut C,
key: K,
) -> NeonResult<Option<Handle<'a, V>>>
fn get_opt<'a, V: Value, C: Context<'a>, K: PropertyKey>( &self, cx: &mut C, key: K, ) -> NeonResult<Option<Handle<'a, V>>>
Object::prop()
insteadSource§fn get_value<'a, C: Context<'a>, K: PropertyKey>(
&self,
cx: &mut C,
key: K,
) -> NeonResult<Handle<'a, JsValue>>
fn get_value<'a, C: Context<'a>, K: PropertyKey>( &self, cx: &mut C, key: K, ) -> NeonResult<Handle<'a, JsValue>>
Object::prop()
insteadSource§fn get<'a, V: Value, C: Context<'a>, K: PropertyKey>(
&self,
cx: &mut C,
key: K,
) -> NeonResult<Handle<'a, V>>
fn get<'a, V: Value, C: Context<'a>, K: PropertyKey>( &self, cx: &mut C, key: K, ) -> NeonResult<Handle<'a, V>>
Object::prop()
insteadSource§fn get_own_property_names<'a, C: Context<'a>>(
&self,
cx: &mut C,
) -> JsResult<'a, JsArray>
fn get_own_property_names<'a, C: Context<'a>>( &self, cx: &mut C, ) -> JsResult<'a, JsArray>
napi-6
only.fn freeze<'a, C: Context<'a>>(&self, cx: &mut C) -> NeonResult<&Self>
fn seal<'a, C: Context<'a>>(&self, cx: &mut C) -> NeonResult<&Self>
Source§fn set<'a, C: Context<'a>, K: PropertyKey, W: Value>(
&self,
cx: &mut C,
key: K,
val: Handle<'_, W>,
) -> NeonResult<bool>
fn set<'a, C: Context<'a>, K: PropertyKey, W: Value>( &self, cx: &mut C, key: K, val: Handle<'_, W>, ) -> NeonResult<bool>
Object::prop()
insteadfn root<'a, C: Context<'a>>(&self, cx: &mut C) -> Root<Self>
Source§fn call_method_with<'a, C, K>(
&self,
cx: &mut C,
method: K,
) -> NeonResult<CallOptions<'a>>where
C: Context<'a>,
K: PropertyKey,
fn call_method_with<'a, C, K>(
&self,
cx: &mut C,
method: K,
) -> NeonResult<CallOptions<'a>>where
C: Context<'a>,
K: PropertyKey,
Object::method()
insteadSource§impl<T: 'static> Value for JsBox<T>
impl<T: 'static> Value for JsBox<T>
Auto Trait Implementations§
impl<T> Freeze for JsBox<T>
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§
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