Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.

Commit ca435e8

Browse files
authored
Merge pull request #16 from fitzgen/fill-out-examples
Fill out examples
2 parents 8c4eb4c + b179f91 commit ca435e8

29 files changed

+1528
-220
lines changed

Cargo.lock

Lines changed: 65 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ edition = "2018"
88
default = []
99

1010
[dependencies]
11-
bumpalo = "2.0.0"
12-
futures = "0.1"
11+
bumpalo = "2.1.0"
12+
futures = "0.1.25"
1313
wasm-bindgen = "0.2.34"
14-
wasm-bindgen-futures = "0.3.10"
15-
js-sys = "0.3.10"
14+
wasm-bindgen-futures = "0.3.11"
15+
js-sys = "0.3.11"
1616
log = "0.4.6"
1717

1818
[dependencies.web-sys]
19-
version = "0.3.10"
19+
version = "0.3.11"
2020
features = [
2121
"console",
2222
"Document",
@@ -27,11 +27,11 @@ features = [
2727
]
2828

2929
[dev-dependencies]
30-
wasm-bindgen-test = "0.2"
30+
wasm-bindgen-test = "0.2.34"
3131
console_log = "0.1.2"
3232

3333
[dev-dependencies.web-sys]
34-
version = "0.3.10"
34+
version = "0.3.11"
3535
features = [
3636
"Attr",
3737
"EventTarget",
@@ -41,12 +41,14 @@ features = [
4141
]
4242

4343
[profile.release]
44-
# Tell `rustc` to optimize for small code size.
44+
incremental = false
45+
lto = true
4546
opt-level = "s"
4647

4748
[workspace]
4849
members = [
4950
"./examples/counter",
5051
"./examples/hello-world",
5152
"./examples/input-form",
53+
"./examples/todomvc",
5254
]

examples/counter/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ authors = ["Nick Fitzgerald <[email protected]>"]
55
edition = "2018"
66

77
[lib]
8-
crate-type = ["cdylib", "rlib"]
8+
crate-type = ["cdylib"]
99

1010
[features]
1111

1212
[dependencies]
13-
console_error_panic_hook = "0.1.1"
13+
console_error_panic_hook = "0.1.5"
1414
console_log = "0.1.2"
1515
dodrio = { path = "../.." }
1616
log = "0.4.6"
17-
wasm-bindgen = "0.2"
17+
wasm-bindgen = "0.2.34"
1818

1919
[dependencies.web-sys]
20-
version = "0.3.8"
20+
version = "0.3.11"
2121
features = [
2222
"console",
2323
"Document",
@@ -30,4 +30,4 @@ features = [
3030
]
3131

3232
[dev-dependencies]
33-
wasm-bindgen-test = "0.2"
33+
wasm-bindgen-test = "0.2.34"

examples/counter/README.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
# 🦀🕸 `rust-webpack-template`
1+
# Counter
22

3-
> **Kickstart your Rust, WebAssembly, and Webpack project!**
3+
A counter that can be incremented and decremented.
44

5-
This template is designed for creating monorepo-style Web applications with
6-
Rust-generated WebAssembly and Webpack without publishing your wasm to NPM.
5+
## Source
76

8-
* Want to create and publish NPM packages with Rust and WebAssembly? [Check out
9-
`wasm-pack-template`.](https://github.com/rustwasm/wasm-pack-template)
7+
See `src/lib.rs`.
108

11-
## 🔋 Batteries Included
9+
## Build
1210

13-
This template comes pre-configured with all the boilerplate for compiling Rust
14-
to WebAssembly and hooking into a Webpack build pipeline.
15-
16-
* `npm run start` -- Serve the project locally for development at
17-
`http://localhost:8080`.
18-
19-
* `npm run build` -- Bundle the project (in production mode).
20-
21-
## 🚴 Using This Template
11+
```
12+
wasm-pack build --target no-modules
13+
```
2214

23-
First, [install `wasm-pack`!](https://rustwasm.github.io/wasm-pack/installer/)
15+
## Serve
2416

25-
Then, use `npm init` to clone this template:
17+
Use any HTTP server, for example:
2618

27-
```sh
28-
npm init rust-webpack my-app
19+
```
20+
python -m SimpleHTTPServer
2921
```

examples/counter/src/lib.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,37 @@ use dodrio::{on, Node, Render};
33
use log::*;
44
use wasm_bindgen::prelude::*;
55

6+
/// A counter that can be incremented and decrmented!
67
struct Counter {
7-
val: isize,
8+
count: isize,
89
}
910

1011
impl Counter {
12+
/// Construct a new, zeroed counter.
1113
fn new() -> Counter {
12-
Counter { val: 0 }
14+
Counter { count: 0 }
1315
}
1416

17+
/// Increment this counter's count.
1518
fn increment(&mut self) {
16-
self.val += 1;
19+
self.count += 1;
1720
}
1821

22+
/// Decrement this counter's count.
1923
fn decrement(&mut self) {
20-
self.val -= 1;
24+
self.count -= 1;
2125
}
2226
}
2327

28+
// The `Render` implementation for `Counter`s displays the current count and has
29+
// buttons to increment and decrement the count.
2430
impl Render for Counter {
2531
fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> dodrio::Node<'bump>
2632
where
2733
'a: 'bump,
2834
{
29-
let val = bumpalo::format!(in bump, "{}", self.val);
35+
// Stringify the count as a bump-allocated string.
36+
let count = bumpalo::format!(in bump, "{}", self.count);
3037

3138
Node::element(
3239
bump,
@@ -38,17 +45,26 @@ impl Render for Counter {
3845
bump,
3946
"button",
4047
[on(bump, "click", |root, vdom, _event| {
41-
root.unwrap_mut::<Counter>().increment();
48+
// Cast the root render component to a `Counter`, since
49+
// we know that's what it is.
50+
let counter = root.unwrap_mut::<Counter>();
51+
52+
// Increment the counter.
53+
counter.increment();
54+
55+
// Since the count has updated, we should re-render the
56+
// counter on the next animation frame.
4257
vdom.schedule_render();
4358
})],
4459
[],
4560
[Node::text("+")],
4661
),
47-
Node::text(val.into_bump_str()),
62+
Node::text(count.into_bump_str()),
4863
Node::element(
4964
bump,
5065
"button",
5166
[on(bump, "click", |root, vdom, _event| {
67+
// Same as above, but decrementing instead of incrementing.
5268
root.unwrap_mut::<Counter>().decrement();
5369
vdom.schedule_render();
5470
})],
@@ -62,9 +78,11 @@ impl Render for Counter {
6278

6379
#[wasm_bindgen(start)]
6480
pub fn run() {
81+
// Initialize debug logging for if/when things go wrong.
6582
console_error_panic_hook::set_once();
6683
console_log::init_with_level(Level::Trace).expect("should initialize logging OK");
6784

85+
// Get the document's `<body>`.
6886
let window = web_sys::window().unwrap();
6987
let document = window.document().unwrap();
7088
let body = document.body().unwrap();

examples/hello-world/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ authors = ["Nick Fitzgerald <[email protected]>"]
55
edition = "2018"
66

77
[lib]
8-
crate-type = ["cdylib", "rlib"]
8+
crate-type = ["cdylib"]
99

1010
[features]
1111

1212
[dependencies]
1313
dodrio = { path = "../.." }
14-
wasm-bindgen = "0.2"
15-
console_error_panic_hook = "0.1.1"
14+
wasm-bindgen = "0.2.34"
15+
console_error_panic_hook = "0.1.5"
1616

1717
[dependencies.web-sys]
18-
version = "0.3.8"
18+
version = "0.3.11"
1919
features = [
2020
"Document",
2121
"HtmlElement",
@@ -24,4 +24,4 @@ features = [
2424
]
2525

2626
[dev-dependencies]
27-
wasm-bindgen-test = "0.2"
27+
wasm-bindgen-test = "0.2.34"

0 commit comments

Comments
 (0)