We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I'm working on porting dominator to use wasm-bindgen, and I ran into this big issue.
Dominator contains a struct DomBuilder<A> which allows it to work on any type. Then I implement methods only for certain types:
struct DomBuilder<A>
impl<A> DomBuilder<A> where A: AsRef<Node> { pub fn children(...) { ... } ... } impl<A> DomBuilder<A> where A: AsRef<Element> { pub fn class(...) { ... } ... } impl<A> DomBuilder<A> where A: AsRef<HtmlElement> { pub fn style(...) { ... } ... }
The idea is that you can use DomBuilder on any type, but at compile-time it will statically restrict which methods you can use depending on the type.
DomBuilder
So you can't use HtmlElement methods on a Node, for example. But you can use Node methods on a Node, Element, or HtmlElement.
HtmlElement
Node
Element
This works great in stdweb, but it doesn't work in wasm-bindgen, because wasm-bindgen implements these...
impl AsRef<JsValue> for HtmlElement impl AsRef<Object> for HtmlElement impl AsRef<EventTarget> for HtmlElement impl AsRef<Node> for HtmlElement impl AsRef<Element> for HtmlElement
...but it does not implement impl AsRef<HtmlElement> for HtmlElement
impl AsRef<HtmlElement> for HtmlElement
That's a big problem, since it means that you cannot write code which is generic over the type.
Implement impl AsRef<Foo> for Foo for all types created by the #[wasm_bindgen] macro.
impl AsRef<Foo> for Foo
#[wasm_bindgen]
Create a new trait that is similar to AsRef (seems redundant).
AsRef
Don't do this (makes writing generic code impossible).
There is precedence for this, since the stdlib defines these:
impl AsRef<CStr> for CStr impl AsRef<OsStr> for OsStr impl AsRef<Path> for Path impl<T> AsRef<Vec<T>> for Vec<T>
And wasm-bindgen itself defines impl AsRef<JsValue> for JsValue
impl AsRef<JsValue> for JsValue
The text was updated successfully, but these errors were encountered:
Successfully merging a pull request may close this issue.
Motivation
I'm working on porting dominator to use wasm-bindgen, and I ran into this big issue.
Dominator contains a
struct DomBuilder<A>
which allows it to work on any type. Then I implement methods only for certain types:The idea is that you can use
DomBuilder
on any type, but at compile-time it will statically restrict which methods you can use depending on the type.So you can't use
HtmlElement
methods on aNode
, for example. But you can useNode
methods on aNode
,Element
, orHtmlElement
.This works great in stdweb, but it doesn't work in wasm-bindgen, because wasm-bindgen implements these...
...but it does not implement
impl AsRef<HtmlElement> for HtmlElement
That's a big problem, since it means that you cannot write code which is generic over the type.
Proposed Solution
Implement
impl AsRef<Foo> for Foo
for all types created by the#[wasm_bindgen]
macro.Alternatives
Create a new trait that is similar to
AsRef
(seems redundant).Don't do this (makes writing generic code impossible).
Additional Context
There is precedence for this, since the stdlib defines these:
And wasm-bindgen itself defines
impl AsRef<JsValue> for JsValue
The text was updated successfully, but these errors were encountered: