TryFromJsRef trait Proposal
I found myself needing something like this when trying to pass a custom object through the FFI boundary without transferring ownership to Rust:
pub trait TryFromJsRef {
/// The type returned in the event of a conversion error.
type Error;
/// Performs the conversion.
fn try_from_js_ref(value: &JsValue) -> Result<&Self, Self::Error>;
}
When I call a method that takes a &JsValue and use try_from_js_value to get my object internally, it consumes the object, and the second call to the same function fails. So, I feel an equivalent try_from_js_ref is needed, unless there is already a canonical way to achieve that that I'm not aware of.
This PR implements it. Reviews & feedback are welcome!
Does dyn_ref not work for this use case?
How to use it with custom structs?
try_from_js_value works out of the box (but consumes the value). When I use dyn_ref, I get:
"The trait wasm_bindgen::JsCast is not implemented for T"
Ahh, custom structs don't implement instanceof, so you'd need to use the unchecked variant in that case - unchecked_ref.
Custom classes should certainly work with instanceof though...