Skip to main content

TryFromJsRef

Trait TryFromJsRef 

Source
pub trait TryFromJsRef<'cx>
where Self: Sealed + Sized,
{ type Guard: Deref<Target = Self>; type Error: TryIntoJs<'cx>; // Required method fn try_from_js_ref( cx: &mut Cx<'cx>, v: Handle<'cx, JsValue>, ) -> NeonResult<Result<Self::Guard, Self::Error>>; // Provided method fn from_js_ref( cx: &mut Cx<'cx>, v: Handle<'cx, JsValue>, ) -> NeonResult<Self::Guard> { ... } }
Expand description

Extract a borrowed reference to Rust data from a JavaScript value

This trait is similar to TryFromJs, but instead of extracting an owned value, it returns a guard that dereferences to a borrowed reference. This is useful for efficiently passing class instances by reference in method calls.

§Example

// In a class method, accept another instance by reference
pub fn distance(&self, other: &Point) -> f64 {
    // other is borrowed, not cloned
}

The macro will automatically use TryFromJsRef when it sees &T parameters.

Required Associated Types§

Source

type Guard: Deref<Target = Self>

A guard type that dereferences to &Self and keeps the borrow alive

Source

type Error: TryIntoJs<'cx>

The error type returned when extraction fails

Required Methods§

Source

fn try_from_js_ref( cx: &mut Cx<'cx>, v: Handle<'cx, JsValue>, ) -> NeonResult<Result<Self::Guard, Self::Error>>

Extract a borrowed reference from a JavaScript value

Provided Methods§

Source

fn from_js_ref( cx: &mut Cx<'cx>, v: Handle<'cx, JsValue>, ) -> NeonResult<Self::Guard>

Same as TryFromJsRef::try_from_js_ref, but all errors are converted to JavaScript exceptions

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§