dtolnay / typetag

Serde serializable and deserializable trait objects
Apache License 2.0
1.19k stars 38 forks source link

Support runtime registration for wasm #36

Closed AaronFriel closed 3 years ago

AaronFriel commented 3 years ago

Problem

WASM targets do not support ctor crate, a dependency of inventory. Static/pre-main registration also goes against the Rust guidelines (covered elsewhere) that there ought to be no code before main. (Part of the philosophy of explicit is better than implicit, I take it?)

This is an issue and has been reported in #30, #15, and others.

Possible Solution

Runtime, explicit registration functions.

This adds a feature runtime which enables runtime registration of types and removes inventory.

This likely isn't up to the coding standard you'd use or how features ought to be used, and I'd appreciate any advice there. I thought about declaring feature std = ["std", "inventory"], but I think inventory works in no_std, non-wasm environments, so I'm not sure if there's a way to turn off inventory as a dependency and enable runtime as a feature in wasm environments automatically. If there is, that would be fantastic.

The two things I'm most uncertain about are:

AaronFriel commented 3 years ago

I've added CI changes here which I think may work, but I'm more of a GitLab yaml engineer myself.

https://github.com/AaronFriel/typetag/commit/3c76524c26c3ccea3c9973b87b5abe54e09bd193

AaronFriel commented 3 years ago

Hey @dtolnay, I'm not sure if that would work for dynamic linking of WASM modules. Would linkme work with WASM module support, at least as I've understood this:

https://github.com/WebAssembly/module-linking/blob/master/proposals/module-linking/Explainer.md

Admittedly, I don't know much about the state of the WASM spec or if it's possible to use a WebAssembly.Global() that is extended/modified on module instantiation.

Would linkme support that use case, or - at least - allow collecting all of the trait implementations defined in a module into a single variable, so the global type registry can be updated?

AaronFriel commented 3 years ago

Oh I think I may have misunderstood - if linkme were a language feature do you mean as a Rust RFC? I'm afraid LLVM and WASM semantics are a few steps beyond what I'm familiar with, I doubt I'd be the right person to champion that.