Modernize code examples in guide (mostly remove extern crate) (#2233)

This commit is contained in:
Veetaha 2020-07-13 17:05:52 +03:00 committed by GitHub
parent 17950202ca
commit 954a3c4fae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 8 additions and 33 deletions

View File

@ -19,8 +19,6 @@ time how long they take to execute with [`Date.now()`][mdn-date-now], and we
don't need to write any JS imports ourselves:
```rust
extern crate js_sys;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]

View File

@ -14,22 +14,8 @@ To enable the `"serde-serialize"` feature, do two things in `Cargo.toml`:
```toml
[dependencies]
serde = "^1.0.59"
serde_derive = "^1.0.59"
[dependencies.wasm-bindgen]
version = "^0.2"
features = ["serde-serialize"]
```
## Import Serde's Custom-Derive Macros
In your top-level Rust file (e.g. `lib.rs` or `main.rs`), enable the `Serialize`
and `Deserialize` custom-derive macros:
```rust
#[macro_use]
extern crate serde_derive;
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
```
## Derive the `Serialize` and `Deserialize` Traits
@ -46,7 +32,9 @@ ABI naively, but all of them implement Serde's `Serialize` and `Deserialize`.
Note that we do not need to use the `#[wasm_bindgen]` macro.
```rust
#[derive(Serialize)]
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Example {
pub field1: HashMap<u32, String>,
pub field2: Vec<Vec<f32>>,
@ -100,7 +88,7 @@ import { send_example_to_js, receive_example_from_js } from "example";
let example = send_example_to_js();
// Add another "Vec" element to the end of the "Vec<Vec<f32>>"
example.field2.push([5,6]);
example.field2.push([5, 6]);
// Send the example object back to wasm.
receive_example_from_js(example);

View File

@ -27,8 +27,6 @@ Rust iterator will yield items of type `Result<JsValue>`. If it yields an
`Err(...)`, then the JS iterator protocol threw an exception.
```rust
extern crate js_sys;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
@ -67,8 +65,6 @@ For example, we can write a function that collects the numbers from any JS
iterable and returns them as an `Array`:
```rust
extern crate js_sys;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]

View File

@ -8,9 +8,6 @@ For example, we can wrap a `Vec<u32>` in a new type, export it to JavaScript,
and invoke a JavaScript closure on each member of the `Vec`:
```rust
extern crate js_sys;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
@ -21,8 +18,8 @@ pub struct VecU32 {
#[wasm_bindgen]
impl VecU32 {
pub fn each(&self, f: &js_sys::Function) {
let this = JsValue::NULL;
for x in &self.xs {
let this = JsValue::null();
for &x in &self.xs {
let x = JsValue::from(x);
let _ = f.call1(&this, &x);
}

View File

@ -16,7 +16,6 @@ older compilers use the `0.2.*` track of `wasm-bindgen-test`.
Create a `$MY_CRATE/tests/wasm.rs` file:
```rust
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]

View File

@ -37,9 +37,6 @@ features = [
## Call the method!
```rust
extern crate web_sys;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use web_sys::Window;