mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 14:12:13 +00:00
This commit implements the first half of [RFC #5] where the `Deref` trait is implemented for all imported types. The target of `Deref` is either the first entry of the list of `extends` attribute or `JsValue`. All examples using `.as_ref()` with various `web-sys` types have been updated to the more ergonomic deref casts now. Additionally the `web-sys` generation of the `extends` array has been fixed slightly to explicitly list implementatoins in the hierarchy order to ensure the correct target for `Deref` is chosen. [RFC #5]: https://github.com/rustwasm/rfcs/blob/master/text/005-structural-and-deref.md
23 lines
694 B
Rust
23 lines
694 B
Rust
extern crate wasm_bindgen;
|
|
extern crate web_sys;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
// Called by our JS entry point to run the example
|
|
#[wasm_bindgen]
|
|
pub fn run() -> Result<(), JsValue> {
|
|
// Use `web_sys`'s global `window` function to get a handle on the global
|
|
// window object.
|
|
let window = web_sys::window().expect("no global `window` exists");
|
|
let document = window.document().expect("should have a document on window");
|
|
let body = document.body().expect("document should have a body");
|
|
|
|
// Manufacture the element we're gonna append
|
|
let val = document.create_element("p")?;
|
|
val.set_inner_html("Hello from Rust!");
|
|
|
|
body.append_child(&val)?;
|
|
|
|
Ok(())
|
|
}
|