mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-24 18:21:33 +00:00
Rewrite docs for getters/setters
This commit is contained in:
@ -1,33 +1,27 @@
|
|||||||
# `getter` and `setter`
|
# `getter` and `setter`
|
||||||
|
|
||||||
It is also possible to interact with `Rust` types either by using fields accessors. For example, the following:
|
The `getter` and `setter` attributes can be used in Rust `impl` blocks to define
|
||||||
|
properties in JS that act like getters and setters of a field. For example:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
|
||||||
fn check_modify_and_return_baz_in_js_fields(baz: Baz) -> Baz;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
|
||||||
fn create_and_check_baz_in_rust() {
|
|
||||||
let baz = check_modify_and_return_baz_in_js_fields(Baz { field: 123 });
|
|
||||||
assert_eq!(baz.field.unwrap(), 456);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct Baz {
|
pub struct Baz {
|
||||||
field: i32,
|
field: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl Baz {
|
impl Baz {
|
||||||
|
#[wasm_bindgen(constructor)]
|
||||||
|
pub fn new(field: i32) -> Baz {
|
||||||
|
Baz { field }
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen(getter)]
|
#[wasm_bindgen(getter)]
|
||||||
pub fn field(&self) -> i32 {
|
pub fn field(&self) -> i32 {
|
||||||
self.field
|
self.field
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen(setter = field)]
|
#[wasm_bindgen(setter)]
|
||||||
pub fn set_field(&mut self, field: i32) {
|
pub fn set_field(&mut self, field: i32) {
|
||||||
self.field = field;
|
self.field = field;
|
||||||
}
|
}
|
||||||
@ -37,9 +31,34 @@ impl Baz {
|
|||||||
Can be combined in `JavaScript` like in this snippet:
|
Can be combined in `JavaScript` like in this snippet:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
check_modify_and_return_baz_in_js_fields = (baz) => {
|
const obj = new Baz(3);
|
||||||
console.log(baz.field, 123);
|
assert.equal(obj.field, 3);
|
||||||
baz.field = 456;
|
obj.field = 4;
|
||||||
return baz;
|
assert.equal(obj.field, 4);
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also configure the name of the property that is exported in JS like so:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl Baz {
|
||||||
|
#[wasm_bindgen(getter = anotherName)]
|
||||||
|
pub fn field(&self) -> i32 {
|
||||||
|
self.field
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen(setter = anotherName)]
|
||||||
|
pub fn set_field(&mut self, field: i32) {
|
||||||
|
self.field = field;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Getters are expected to take no arguments other than `&self` and return the
|
||||||
|
field's type. Setters are expected to take one argument other than `&mut self`
|
||||||
|
(or `&self`) and return no values.
|
||||||
|
|
||||||
|
The name for a `getter` is by default inferred from the function name it's
|
||||||
|
attached to. The default name for a `setter` is the function's name minus the
|
||||||
|
`set_` prefix, and if `set_` isn't a prefix of the function it's an error to not
|
||||||
|
provide the name explicitly.
|
||||||
|
Reference in New Issue
Block a user