Specifying types
To be able to interact with JavaScript, code generated by this crate must convert between Rust types and V8 data types. Function arguments and return types must implement specific conversion traits.
ToV8
and FromV8
ToV8
and FromV8
are deno_core
's builtin conversion traits.
By default:
-
Function arguments must implement
ToV8
;- In the case of variadic functions, the
argument must implement
Iterator<Item = T>
, andT
must implementToV8
;
- In the case of variadic functions, the
argument must implement
-
Function return types, property accessor return types, and iterator item types must implement
FromV8
.
In addition to existing implementors,
-
Types derived with
js(value)
,js(module)
, andjs(global_this)
implementToV8
. This means you can pass such values to JS functions as arguments. -
Types derived with
js(value)
implementFromV8
. This means you can return such values from JS functions.
Serialize
and DeserializeOwned
Alternatively, you can opt in to data conversion using serde_v8
. To do so, wrap the
type in serde<...>
:
use ferrosaur::js;
#[js(value)]
struct Foo;
use deno_core::serde::{Serialize, de::DeserializeOwned};
#[js(interface)]
impl Foo {
#[js(func)]
fn bar<T, U>(&self, baz: serde<T>) -> serde<U>
where
T: Serialize,
U: DeserializeOwned,
{}
}
In this case:
-
Function arguments must implement
Serialize
;- In the case of variadic functions, the
argument must implement
Iterator<Item = T>
, andT
must implementSerialize
;
- In the case of variadic functions, the
argument must implement
-
Function return types, property accessor return types, and iterator item types must implement
DeserializeOwned
.