Skip to content
New issue

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

web-sys function bindings should be generic over AsRef<T> instead of &T #977

Closed
bgourlie opened this issue Oct 21, 2018 · 2 comments
Closed

Comments

@bgourlie
Copy link

bgourlie commented Oct 21, 2018

Take the definition of append_child for example.

pub fn append_child(&self, node: &Node) -> Result<Node, JsValue>

I find myself having to manually coerce an Element into the correct type, for example:

let document = web_sys::window().unwrap().document().unwrap();
let val = document.create_element("p").unwrap();
val.set_inner_html("Hello from Rust, WebAssembly, WebPack, and web-sys!");
AsRef::<Node>::as_ref(&document.body().unwrap()).append_child(AsRef::<Node>::as_ref(&val));
@Pauan
Copy link
Contributor

Pauan commented Oct 22, 2018

I think it's unlikely that the web-sys crate will use AsRef generics, since it's supposed to be a direct 1-to-1 translation of WebIDL.

However, there is an RFC proposal which will solve this problem (it uses Deref, not AsRef). That means that this code will work just fine:

document.body().unwrap().append_child(&val);

No AsRef needed!

@bgourlie
Copy link
Author

Oh, perfect! Thanks for the heads up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants