How to generate wasm for Generic Types for a given specific Type

I have

struct S<T> { /*..*/ }
impl<T> S<T> { /* .. */ }

wasm_bindgen doesn't generate because struct S has generic Type T. However I just want wasm_bindgen to generate on specific type like S<u8>. Whatever the methods declared in S<T> should be available to S<u8> by wasm_bindgen. What is the syntax for achieving like this?

Sorry, I don't know the answer. I would guess that this is also why other people have not answered.

If the way you do it for non-generic structs is with a derive for some trait, you may be able to manually impl the trait for S<u8>.


If I understand it correctly, I think the problem is that this is about generating bindings for a foreign interface, so the types need to be concrete for wasm_bindgen to work.

The way out is probably gonna be using a newtype:

struct ConcreteU8S(S<u8>)

This does, however, mean that you'll need to write the forwarding/proxying of the methods you want yourself. As in:

impl ConcreteU8S {
   fn do_something(&self) {

   /// repeat for everything you care about

There's an RFC for functionality that would help eliminate this boilerplate, but the problem space is not as simple as it may sound: newtype / generalized newtype deriving · Issue #261 · rust-lang/rfcs · GitHub


I'm no expert in with wasm_bindgen, so I didn't respond initially, but given your plea -- my impression is exactly in line with what @errado said. You'll need to make a newtype and forward the methods.

1 Like

Thanks for all the replies.

However, this kinda sucks and also error-prone. I wish there was a macro to do this.

I believe there is no macro that could be written to automate this. Why?

Because every interface is different. Just deciding which types you want to substitute for type parameters at the FFI boundary alone is something a macro is incapable of having knowledge of.

A macro could be written to reduce the amount of boilerplate, but that would make the process a lot more magick, and so I think it would be a bad idea to attempt that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.