|
|
|
@ -3,11 +3,11 @@ |
|
|
|
|
`Cargo.toml` is the manifest file for Rust's package manager, `cargo`. This file contains |
|
|
|
|
metadata such as name, version, and dependencies for packages, which are call "crates" in Rust. |
|
|
|
|
|
|
|
|
|
There's a bunch of metadata that the template gives us, but there are 3 key parts to discuss: |
|
|
|
|
There's a bunch of metadata that the template gives us, but there are three key parts to discuss: |
|
|
|
|
|
|
|
|
|
- [`crate-type`](#a1-crate-type) |
|
|
|
|
- [`wasm-bindgen` dependency](#a2-wasm-bindgen-dependency) |
|
|
|
|
- [`[features]` and `wee-alloc`, `console-error-panic-hook` dependencies](#a3-features-and-wee-alloc-console-error-panic-hook-dependencies) |
|
|
|
|
1. [`crate-type`](#a1-crate-type) |
|
|
|
|
2. [`wasm-bindgen` dependency](#a2-wasm-bindgen-dependency) |
|
|
|
|
3. [`[features]` and `wee_alloc`, `console_error_panic_hook` dependencies](#a3-features-and-wee_alloc-console_error_panic_hook-dependencies) |
|
|
|
|
|
|
|
|
|
<hr/> |
|
|
|
|
|
|
|
|
@ -15,7 +15,7 @@ There's a bunch of metadata that the template gives us, but there are 3 key part |
|
|
|
|
|
|
|
|
|
```toml |
|
|
|
|
[lib] |
|
|
|
|
crate-type = ["cdylib"] |
|
|
|
|
crate-type = ["cdylib", "rlib"] |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
A Rust-`wasm` crate is a bit different from a normal crate, and as a result, we need to note |
|
|
|
@ -33,6 +33,9 @@ as a dynamic library to be loaded from another language. In our case, we'll be c |
|
|
|
|
`.wasm` file, but this output type will create `*.so` files on Linux, `*.dylib` files on |
|
|
|
|
macOS, and `*.dll` files on Windows in non-`wasm` circumstances. |
|
|
|
|
|
|
|
|
|
`#[crate_type = "rlib"]` signifies that an intermediate "Rust library" file will be produced. |
|
|
|
|
This allows tests to use the main crate. |
|
|
|
|
|
|
|
|
|
You can read more about linking and crate types, [here](https://doc.rust-lang.org/reference/linkage.html). |
|
|
|
|
|
|
|
|
|
## 2. `wasm-bindgen` dependency |
|
|
|
@ -52,23 +55,24 @@ We'll see more about how to use this library when we discuss what has been gener |
|
|
|
|
there is no `^` or `~` symbol- it looks like we're locking to the `0.2` version. |
|
|
|
|
However, that's not the case! In Rust, the `^` is implied. |
|
|
|
|
|
|
|
|
|
## 3. `[features]` and `wee-alloc`, `console-error-panic-hook` dependencies |
|
|
|
|
## 3. `[features]` and `wee_alloc`, `console_error_panic_hook` dependencies |
|
|
|
|
|
|
|
|
|
As part of our effort to design a template that helps people discover useful crates |
|
|
|
|
for their particular use case, this template includes 2 dependencies that can be |
|
|
|
|
for their particular use case, this template includes two dependencies that can be |
|
|
|
|
very useful for folks developing Rust-`wasm` crates: `console-error-panic-hook` and |
|
|
|
|
`wee-alloc`. |
|
|
|
|
|
|
|
|
|
Because these dependencies are useful primarily in a specifc portion of the Rust-`wasm` |
|
|
|
|
Because these dependencies are useful primarily in a specific portion of the Rust-`wasm` |
|
|
|
|
crate development workflow, we've also set up a bit of glue code that allows us to include |
|
|
|
|
them both as dependences, but allowing for them to be optionally included. |
|
|
|
|
them both as dependencies, but also allows them to be optionally included. |
|
|
|
|
|
|
|
|
|
```toml |
|
|
|
|
[features] |
|
|
|
|
default-features = ["console_error_panic_hook", "wee_alloc"] |
|
|
|
|
default = ["console_error_panic_hook"] |
|
|
|
|
|
|
|
|
|
[dependencies] |
|
|
|
|
cfg-if = "0.1.2" |
|
|
|
|
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 |
|
|
|
@ -79,12 +83,22 @@ 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. |
|
|
|
|
wee_alloc = { version = "0.4.1", optional = true } |
|
|
|
|
# |
|
|
|
|
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. |
|
|
|
|
wee_alloc = { version = "0.4.2", optional = true } |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
[`cfg-if`] allows us to check if certain features are enabled on a rust crate. We'll |
|
|
|
|
use this crate in `utils.rs` to optionally enable `console_error_panic_hook` or |
|
|
|
|
`wee_alloc`. By default, we have them enabled. To disable them, we can remove their |
|
|
|
|
entry from the `default-features` vector. |
|
|
|
|
[`cfg-if`] allows us to check if certain features are enabled on a Rust crate. We'll |
|
|
|
|
use this crate later to optionally enable `console_error_panic_hook` or |
|
|
|
|
`wee_alloc`. |
|
|
|
|
|
|
|
|
|
By default, only `console_error_panic_hook` is enabled. To disable either |
|
|
|
|
feature, we can remove its name from the `default` vector. |
|
|
|
|
|
|
|
|
|
To learn more about these features, we discuss them in-depth in the `src/lib.rs` and |
|
|
|
|
`src/utils.rs` sections. |
|
|
|
|
|
|
|
|
|
Briefly, they include: |
|
|
|
|
|
|
|
|
|
To learn more about these features, we discuss them in depth in the `utils.rs` section. |
|
|
|
|
+ **console_error_panic_hook** for logging panic messages to the developer console. |
|
|
|
|
+ **wee_alloc**, an allocator optimized for small code size. |
|
|
|
|