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.

Sections

ToV8 and FromV8

ToV8 and FromV8 are deno_core's builtin conversion traits.

By default:

  • Function arguments must implement ToV8;

  • Function return types, property accessor return types, and iterator item types must implement FromV8.

In addition to existing implementors,

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: