Skip to content

Expose traits and its methods via #[napi] directly when supported upstream #987

@Xanewok

Description

@Xanewok

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions