| 1 |
# `extends = Class` |
| 2 |
|
| 3 |
The `extends` attribute can be used to say that an imported type extends (in the |
| 4 |
JS class hierarchy sense) another type. This will generate `AsRef`, `AsMut`, and |
| 5 |
`From` impls for converting a type into another given that we statically know |
| 6 |
the inheritance hierarchy: |
| 7 |
|
| 8 |
```rust |
| 9 |
#[wasm_bindgen] |
| 10 |
extern "C" { |
| 11 |
type Foo; |
| 12 |
|
| 13 |
#[wasm_bindgen(extends = Foo)] |
| 14 |
type Bar; |
| 15 |
} |
| 16 |
|
| 17 |
let x: &Bar = ...; |
| 18 |
let y: &Foo = x.as_ref(); // zero cost cast |
| 19 |
``` |
| 20 |
|
| 21 |
The trait implementations generated for the above block are: |
| 22 |
|
| 23 |
```rust |
| 24 |
impl From<Bar> for Foo { ... } |
| 25 |
impl AsRef<Foo> for Bar { ... } |
| 26 |
impl AsMut<Foo> for Bar { ... } |
| 27 |
``` |
| 28 |
|
| 29 |
|
| 30 |
The `extends = ...` attribute can be specified multiple times for longer |
| 31 |
inheritance chains, and `AsRef` and such impls will be generated for each of |
| 32 |
the types. |
| 33 |
|
| 34 |
```rust |
| 35 |
#[wasm_bindgen] |
| 36 |
extern "C" { |
| 37 |
type Foo; |
| 38 |
|
| 39 |
#[wasm_bindgen(extends = Foo)] |
| 40 |
type Bar; |
| 41 |
|
| 42 |
#[wasm_bindgen(extends = Foo, extends = Bar)] |
| 43 |
type Baz; |
| 44 |
} |
| 45 |
|
| 46 |
let x: &Baz = ...; |
| 47 |
let y1: &Bar = x.as_ref(); |
| 48 |
let y2: &Foo = y1.as_ref(); |
| 49 |
``` |