Context
While implementing #963 we found that it's impossible to define in terms of #[napi] something along the lines of
export interface MyInterface {
some(): string;
methods(): string;
or use a better suited way to expose Rust trait implementations to the TypeScript side.
Alternatives
One idea explored to provide a shim of
#[napi]
struct MyTraitInterface(Box<dyn MyTrait>);
but this forces us to provide explicit conversion methods for the N-API-exposed types that wrap a type implementing a Rust trait into this shim, making it less ergonomic and potentially more costly due to cloning involved whenever we needed to access the trait methods.
Solution
This is probably blocked on napi-rs/napi-rs#1164, which seems to be planned. The solution would be to:
- expose the trait methods via
#[napi] directly rather than having to introduce our own custom interface in the .ts file;
- being able to conveniently expose select methods for a given
#[napi] decorated trait interface or
- use planned class inheritance feature to implement and expose that for us.
Context
While implementing #963 we found that it's impossible to define in terms of
#[napi]something along the lines ofor use a better suited way to expose Rust trait implementations to the TypeScript side.
Alternatives
One idea explored to provide a shim of
but this forces us to provide explicit conversion methods for the N-API-exposed types that wrap a type implementing a Rust trait into this shim, making it less ergonomic and potentially more costly due to cloning involved whenever we needed to access the trait methods.
Solution
This is probably blocked on napi-rs/napi-rs#1164, which seems to be planned. The solution would be to:
#[napi]directly rather than having to introduce our own custom interface in the .ts file;#[napi]decorated trait interface or