Adds cast and try_cast methods to v8::Local, to make casting more ergonomic. There was already an existing cast function on Local, which did an unchecked cast. This PR renames the old cast to cast_unchecked for clarity (and so we can repurpose cast).
Currently, to go from v8::Local<A> to v8::Local<B>, in order to guide type inference you often end up writing
v8::Local::<v8::Function>::try_from(v8_value);
// try_into is no better because you can't easily give a type annotation
let func: v8::Local<v8::Function> = v8_value.try_into().unwrap();
We have > 100 instances of this in test_api.rs alone, and deno is similar.
With this PR, you can instead use the turbofish the give the type annotation
if let Ok(func) = v8_value.try_cast::<v8::Function>() {
// do stuff
}
// shorthand, panics if cast fails
v8_value.cast::<v8::Function>();
This plays much more nicely with chaining methods, and is less verbose.
Adds
cast
andtry_cast
methods tov8::Local
, to make casting more ergonomic. There was already an existingcast
function onLocal
, which did an unchecked cast. This PR renames the oldcast
tocast_unchecked
for clarity (and so we can repurposecast
).Currently, to go from
v8::Local<A>
tov8::Local<B>
, in order to guide type inference you often end up writingWe have > 100 instances of this in
test_api.rs
alone, anddeno
is similar.With this PR, you can instead use the turbofish the give the type annotation
This plays much more nicely with chaining methods, and is less verbose.