mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 01:11:06 +00:00
Merge pull request #1100 from elpiel/port-examples-to-rust-2018
Port examples - console-log, closures and duck-typed-interfaces to Rust 2018
This commit is contained in:
commit
639724a85e
@ -2,6 +2,7 @@
|
|||||||
name = "closures"
|
name = "closures"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["The wasm-bindgen Developers"]
|
authors = ["The wasm-bindgen Developers"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
extern crate js_sys;
|
|
||||||
extern crate wasm_bindgen;
|
|
||||||
extern crate web_sys;
|
|
||||||
|
|
||||||
use js_sys::{Array, Date};
|
use js_sys::{Array, Date};
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
@ -67,7 +63,7 @@ fn setup_clock(window: &Window, document: &Document) -> Result<(), JsValue> {
|
|||||||
.get_element_by_id("current-time")
|
.get_element_by_id("current-time")
|
||||||
.expect("should have #current-time on the page");
|
.expect("should have #current-time on the page");
|
||||||
update_time(¤t_time);
|
update_time(¤t_time);
|
||||||
let a = Closure::wrap(Box::new(move || update_time(¤t_time)) as Box<Fn()>);
|
let a = Closure::wrap(Box::new(move || update_time(¤t_time)) as Box<dyn Fn()>);
|
||||||
window
|
window
|
||||||
.set_interval_with_callback_and_timeout_and_arguments_0(a.as_ref().unchecked_ref(), 1000)?;
|
.set_interval_with_callback_and_timeout_and_arguments_0(a.as_ref().unchecked_ref(), 1000)?;
|
||||||
fn update_time(current_time: &Element) {
|
fn update_time(current_time: &Element) {
|
||||||
@ -103,7 +99,7 @@ fn setup_clicker(document: &Document) {
|
|||||||
let a = Closure::wrap(Box::new(move || {
|
let a = Closure::wrap(Box::new(move || {
|
||||||
clicks += 1;
|
clicks += 1;
|
||||||
num_clicks.set_inner_html(&clicks.to_string());
|
num_clicks.set_inner_html(&clicks.to_string());
|
||||||
}) as Box<FnMut()>);
|
}) as Box<dyn FnMut()>);
|
||||||
document
|
document
|
||||||
.get_element_by_id("green-square")
|
.get_element_by_id("green-square")
|
||||||
.expect("should have #green-square on the page")
|
.expect("should have #green-square on the page")
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
name = "console_log"
|
name = "console_log"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["The wasm-bindgen Developers"]
|
authors = ["The wasm-bindgen Developers"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
extern crate wasm_bindgen;
|
|
||||||
extern crate web_sys;
|
|
||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
#[wasm_bindgen(start)]
|
#[wasm_bindgen(start)]
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
name = "rust-duck-typed-interfaces"
|
name = "rust-duck-typed-interfaces"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["The wasm-bindgen Developers"]
|
authors = ["The wasm-bindgen Developers"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
extern crate wasm_bindgen;
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
/// Here is a duck-typed interface for any JavaScript object that has a `quack`
|
/// Here is a duck-typed interface for any JavaScript object that has a `quack`
|
||||||
@ -18,6 +17,6 @@ extern "C" {
|
|||||||
/// Next, we can export a function that takes any object that quacks:
|
/// Next, we can export a function that takes any object that quacks:
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn make_em_quack_to_this(duck: &Quacks) {
|
pub fn make_em_quack_to_this(duck: &Quacks) {
|
||||||
let s = duck.quack();
|
let _s = duck.quack();
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user