6 changed files with 152 additions and 0 deletions
-
7tutorial/wasm-game-of-life/.gitignore
-
34tutorial/wasm-game-of-life/Cargo.toml
-
69tutorial/wasm-game-of-life/README.md
-
19tutorial/wasm-game-of-life/src/lib.rs
-
10tutorial/wasm-game-of-life/src/utils.rs
-
13tutorial/wasm-game-of-life/tests/web.rs
@ -0,0 +1,7 @@ |
|||
/target |
|||
**/*.rs.bk |
|||
Cargo.lock |
|||
bin/ |
|||
pkg/ |
|||
wasm-pack/ |
|||
wasm-pack.log |
|||
@ -0,0 +1,34 @@ |
|||
[package] |
|||
name = "wasm-game-of-life" |
|||
version = "0.1.0" |
|||
authors = ["hopp@silpion.de"] |
|||
edition = "2018" |
|||
|
|||
[lib] |
|||
crate-type = ["cdylib", "rlib"] |
|||
|
|||
[features] |
|||
default = ["console_error_panic_hook"] |
|||
|
|||
[dependencies] |
|||
wasm-bindgen = "0.2" |
|||
|
|||
# The `console_error_panic_hook` crate provides better debugging of panics by |
|||
# logging them with `console.error`. This is great for development, but requires |
|||
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for |
|||
# code size when deploying. |
|||
console_error_panic_hook = { version = "0.1.1", optional = true } |
|||
|
|||
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size |
|||
# compared to the default allocator's ~10K. It is slower than the default |
|||
# allocator, however. |
|||
# |
|||
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. |
|||
wee_alloc = { version = "0.4.2", optional = true } |
|||
|
|||
[dev-dependencies] |
|||
wasm-bindgen-test = "0.2" |
|||
|
|||
[profile.release] |
|||
# Tell `rustc` to optimize for small code size. |
|||
opt-level = "s" |
|||
@ -0,0 +1,69 @@ |
|||
<div align="center"> |
|||
|
|||
<h1><code>wasm-pack-template</code></h1> |
|||
|
|||
<strong>A template for kick starting a Rust and WebAssembly project using <a href="https://github.com/rustwasm/wasm-pack">wasm-pack</a>.</strong> |
|||
|
|||
<p> |
|||
<a href="https://travis-ci.org/rustwasm/wasm-pack-template"><img src="https://img.shields.io/travis/rustwasm/wasm-pack-template.svg?style=flat-square" alt="Build Status" /></a> |
|||
</p> |
|||
|
|||
<h3> |
|||
<a href="https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html">Tutorial</a> |
|||
<span> | </span> |
|||
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a> |
|||
</h3> |
|||
|
|||
<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub> |
|||
</div> |
|||
|
|||
## About |
|||
|
|||
[**📚 Read this template tutorial! 📚**][template-docs] |
|||
|
|||
This template is designed for compiling Rust libraries into WebAssembly and |
|||
publishing the resulting package to NPM. |
|||
|
|||
Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other |
|||
templates and usages of `wasm-pack`. |
|||
|
|||
[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html |
|||
[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html |
|||
|
|||
## 🚴 Usage |
|||
|
|||
### 🐑 Use `cargo generate` to Clone this Template |
|||
|
|||
[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) |
|||
|
|||
``` |
|||
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project |
|||
cd my-project |
|||
``` |
|||
|
|||
### 🛠️ Build with `wasm-pack build` |
|||
|
|||
``` |
|||
wasm-pack build |
|||
``` |
|||
|
|||
### 🔬 Test in Headless Browsers with `wasm-pack test` |
|||
|
|||
``` |
|||
wasm-pack test --headless --firefox |
|||
``` |
|||
|
|||
### 🎁 Publish to NPM with `wasm-pack publish` |
|||
|
|||
``` |
|||
wasm-pack publish |
|||
``` |
|||
|
|||
## 🔋 Batteries Included |
|||
|
|||
* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating |
|||
between WebAssembly and JavaScript. |
|||
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) |
|||
for logging panic messages to the developer console. |
|||
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized |
|||
for small code size. |
|||
@ -0,0 +1,19 @@ |
|||
mod utils;
|
|||
|
|||
use wasm_bindgen::prelude::*;
|
|||
|
|||
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
|||
// allocator.
|
|||
#[cfg(feature = "wee_alloc")]
|
|||
#[global_allocator]
|
|||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|||
|
|||
#[wasm_bindgen]
|
|||
extern {
|
|||
fn alert(s: &str);
|
|||
}
|
|||
|
|||
#[wasm_bindgen]
|
|||
pub fn greet() {
|
|||
alert("Hello, {{project-name}}!");
|
|||
}
|
|||
@ -0,0 +1,10 @@ |
|||
pub fn set_panic_hook() {
|
|||
// When the `console_error_panic_hook` feature is enabled, we can call the
|
|||
// `set_panic_hook` function at least once during initialization, and then
|
|||
// we will get better error messages if our code ever panics.
|
|||
//
|
|||
// For more details see
|
|||
// https://github.com/rustwasm/console_error_panic_hook#readme
|
|||
#[cfg(feature = "console_error_panic_hook")]
|
|||
console_error_panic_hook::set_once();
|
|||
}
|
|||
@ -0,0 +1,13 @@ |
|||
//! Test suite for the Web and headless browsers.
|
|||
|
|||
#![cfg(target_arch = "wasm32")]
|
|||
|
|||
extern crate wasm_bindgen_test;
|
|||
use wasm_bindgen_test::*;
|
|||
|
|||
wasm_bindgen_test_configure!(run_in_browser);
|
|||
|
|||
#[wasm_bindgen_test]
|
|||
fn pass() {
|
|||
assert_eq!(1 + 1, 2);
|
|||
}
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue