Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(neon): Allow getting a &'cx T from a Handle<'cx, JsBox<T>> #1087

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions crates/neon/src/types_impl/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,39 @@ impl<T: Finalize + 'static> JsBox<T> {
}
}

impl<T: 'static> JsBox<T> {
/// Gets a reference to the inner value of a [`JsBox`]. This method is similar to
/// [dereferencing](JsBox::deref) a `JsBox` (e.g., `&*boxed`), but the lifetime
/// is _safely_ extended to `'cx`.
///
/// See also [`Handle<JsBox>::as_inner`].
// N.B.: This would be cleaner with https://github.com/rust-lang/rust/issues/44874
pub fn deref<'cx>(v: &Handle<'cx, Self>) -> &'cx T {
v.as_inner()
}
}

impl<'cx, T: 'static> Handle<'cx, JsBox<T>> {
/// Gets a reference to the inner value of a [`JsBox`]. This method is similar to
/// [dereferencing](JsBox::deref) a `JsBox` (e.g., `&*boxed`), but the lifetime
/// is _safely_ extended to `'cx`.
///
/// See also [`JsBox::deref`].
pub fn as_inner(&self) -> &'cx T {
// # Safety
// JS values associated with an in-scope `Context` *cannot* garbage collected. This
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// JS values associated with an in-scope `Context` *cannot* garbage collected. This
// JS values associated with an in-scope `Context` *cannot* be garbage collected. This

// value is guaranteed to live at least as long as `'cx`.
unsafe { &*self.0.raw_data }
}
}

impl<T: 'static> Deref for JsBox<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
// Safety: This depends on a `Handle<'a, JsBox<T>>` wrapper to provide
// a proper lifetime.
// # Safety
// `T` will live at least as long as `JsBox<T>` because it may not be garbage
// collected while in scope and only immutable references can be obtained.
unsafe { &*self.0.raw_data }
}
}
Expand Down
Loading