Game of life boilerplate

This commit is contained in:
Greg Shuflin 2024-01-02 02:18:47 -08:00
parent 0d71915d5a
commit 4ea872c60f
5 changed files with 58 additions and 11 deletions

18
Cargo.lock generated
View File

@ -14,6 +14,16 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if",
"wasm-bindgen",
]
[[package]]
name = "gamarjoba"
version = "0.1.0"
@ -21,6 +31,14 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "game-of-life"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"wasm-bindgen",
]
[[package]]
name = "log"
version = "0.4.20"

View File

@ -5,4 +5,16 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
console_error_panic_hook = { version = "0.1.7", optional = true }
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

8
game-of-life/justfile Normal file
View File

@ -0,0 +1,8 @@
_default:
just --list
wasm_pack := "~/.cargo/bin/wasm-pack"
# Use wasm-pack to build
build:
{{wasm_pack}} build

View File

@ -1,14 +1,13 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
mod util;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, {{project-name}}!");
}

10
game-of-life/src/util.rs Normal file
View File

@ -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();
}