Skip to content

Commit 954a3c4

Browse files
authored
Modernize code examples in guide (mostly remove extern crate) (#2233)
1 parent 1795020 commit 954a3c4

File tree

6 files changed

+8
-33
lines changed

6 files changed

+8
-33
lines changed

guide/src/contributing/js-sys/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ time how long they take to execute with [`Date.now()`][mdn-date-now], and we
1919
don't need to write any JS imports ourselves:
2020

2121
```rust
22-
extern crate js_sys;
23-
extern crate wasm_bindgen;
2422
use wasm_bindgen::prelude::*;
2523

2624
#[wasm_bindgen]

guide/src/reference/arbitrary-data-with-serde.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,8 @@ To enable the `"serde-serialize"` feature, do two things in `Cargo.toml`:
1414

1515
```toml
1616
[dependencies]
17-
serde = "^1.0.59"
18-
serde_derive = "^1.0.59"
19-
20-
[dependencies.wasm-bindgen]
21-
version = "^0.2"
22-
features = ["serde-serialize"]
23-
```
24-
25-
## Import Serde's Custom-Derive Macros
26-
27-
In your top-level Rust file (e.g. `lib.rs` or `main.rs`), enable the `Serialize`
28-
and `Deserialize` custom-derive macros:
29-
30-
```rust
31-
#[macro_use]
32-
extern crate serde_derive;
17+
serde = { version = "1.0", features = ["derive"] }
18+
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
3319
```
3420

3521
## Derive the `Serialize` and `Deserialize` Traits
@@ -46,7 +32,9 @@ ABI naively, but all of them implement Serde's `Serialize` and `Deserialize`.
4632
Note that we do not need to use the `#[wasm_bindgen]` macro.
4733

4834
```rust
49-
#[derive(Serialize)]
35+
use serde::{Serialize, Deserialize};
36+
37+
#[derive(Serialize, Deserialize)]
5038
pub struct Example {
5139
pub field1: HashMap<u32, String>,
5240
pub field2: Vec<Vec<f32>>,
@@ -100,7 +88,7 @@ import { send_example_to_js, receive_example_from_js } from "example";
10088
let example = send_example_to_js();
10189

10290
// Add another "Vec" element to the end of the "Vec<Vec<f32>>"
103-
example.field2.push([5,6]);
91+
example.field2.push([5, 6]);
10492

10593
// Send the example object back to wasm.
10694
receive_example_from_js(example);

guide/src/reference/iterating-over-js-values.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ Rust iterator will yield items of type `Result<JsValue>`. If it yields an
2727
`Err(...)`, then the JS iterator protocol threw an exception.
2828

2929
```rust
30-
extern crate js_sys;
31-
extern crate wasm_bindgen;
3230
use wasm_bindgen::prelude::*;
3331

3432
#[wasm_bindgen]
@@ -67,8 +65,6 @@ For example, we can write a function that collects the numbers from any JS
6765
iterable and returns them as an `Array`:
6866

6967
```rust
70-
extern crate js_sys;
71-
extern crate wasm_bindgen;
7268
use wasm_bindgen::prelude::*;
7369

7470
#[wasm_bindgen]

guide/src/reference/receiving-js-closures-in-rust.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ For example, we can wrap a `Vec<u32>` in a new type, export it to JavaScript,
88
and invoke a JavaScript closure on each member of the `Vec`:
99

1010
```rust
11-
extern crate js_sys;
12-
extern crate wasm_bindgen;
13-
1411
use wasm_bindgen::prelude::*;
1512

1613
#[wasm_bindgen]
@@ -21,8 +18,8 @@ pub struct VecU32 {
2118
#[wasm_bindgen]
2219
impl VecU32 {
2320
pub fn each(&self, f: &js_sys::Function) {
24-
let this = JsValue::NULL;
25-
for x in &self.xs {
21+
let this = JsValue::null();
22+
for &x in &self.xs {
2623
let x = JsValue::from(x);
2724
let _ = f.call1(&this, &x);
2825
}

guide/src/wasm-bindgen-test/usage.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ older compilers use the `0.2.*` track of `wasm-bindgen-test`.
1616
Create a `$MY_CRATE/tests/wasm.rs` file:
1717

1818
```rust
19-
extern crate wasm_bindgen_test;
2019
use wasm_bindgen_test::*;
2120

2221
#[wasm_bindgen_test]

guide/src/web-sys/using-web-sys.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ features = [
3737
## Call the method!
3838

3939
```rust
40-
extern crate web_sys;
41-
extern crate wasm_bindgen;
42-
4340
use wasm_bindgen::prelude::*;
4441
use web_sys::Window;
4542

0 commit comments

Comments
 (0)