|
| 1 | +# Field functions |
| 2 | + |
| 3 | +Field functions are special operations which operate on fields. These are |
| 4 | +distinct from associated functions, because they are invoked by using the |
| 5 | +operation associated with the kind of the field function. |
| 6 | + |
| 7 | +The most common forms of fields functions are *getters* and *setters*, which are |
| 8 | +defined through the [`Protocol::GET`] and [`Protocol::SET`] protocols. |
| 9 | + |
| 10 | +The `Any` derive can also generate default implementations of these through |
| 11 | +various `#[rune(...)]` attributes: |
| 12 | + |
| 13 | +```rust,noplaypen |
| 14 | +#[derive(Any)] |
| 15 | +struct External { |
| 16 | + #[rune(get, set, add_assign, copy)] |
| 17 | + number: i64, |
| 18 | + #[rune(get, set)] |
| 19 | + string: String, |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +Once registered, this allows `External` to be used like this in Rune: |
| 24 | + |
| 25 | +```rune |
| 26 | +pub fn main(external) { |
| 27 | + external.number = external.number + 1; |
| 28 | + external.number += 1; |
| 29 | + external.string = `${external.string} World`; |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +The full list of available field functions and their corresponding attributes |
| 34 | +are: |
| 35 | + |
| 36 | +| Protocol | Attribute | | |
| 37 | +|-|-|-| |
| 38 | +| [`Protocol::GET`] | `#[rune(get)]` | For getters, like `external.field`. | |
| 39 | +| [`Protocol::SET`] | `#[rune(set)]` | For setters, like `external.field = 42`. | |
| 40 | +| [`Protocol::ADD_ASSIGN`] | `#[rune(add_assign)]` | The `+=` operation. | |
| 41 | +| [`Protocol::SUB_ASSIGN`] | `#[rune(sub_assign)]` | The `-=` operation. | |
| 42 | +| [`Protocol::MUL_ASSIGN`] | `#[rune(mul_assign)]` | The `*=` operation. | |
| 43 | +| [`Protocol::DIV_ASSIGN`] | `#[rune(div_assign)]` | The `/=` operation. | |
| 44 | +| [`Protocol::BIT_AND_ASSIGN`] | `#[rune(bit_and_assign)]` | The `&=` operation. | |
| 45 | +| [`Protocol::BIT_OR_ASSIGN`] | `#[rune(bit_or_assign)]` | The bitwise or operation. | |
| 46 | +| [`Protocol::BIT_XOR_ASSIGN`] | `#[rune(bit_xor_assign)]` | The `^=` operation. | |
| 47 | +| [`Protocol::SHL_ASSIGN`] | `#[rune(shl_assign)]` | The `<<=` operation. | |
| 48 | +| [`Protocol::SHR_ASSIGN`] | `#[rune(shr_assign)]` | The `>>=` operation. | |
| 49 | + |
| 50 | +The manual way to register these functions is to use the new `Module::field_fn` |
| 51 | +function. This clearly showcases that there's no relationship between the field |
| 52 | +used and the function registered: |
| 53 | + |
| 54 | +```rust,noplaypen |
| 55 | +use runestick::{Any, Module}; |
| 56 | +
|
| 57 | +#[derive(Any)] |
| 58 | +struct External { |
| 59 | +} |
| 60 | +
|
| 61 | +impl External { |
| 62 | + fn field_get(&self) -> String { |
| 63 | + String::from("Hello World") |
| 64 | + } |
| 65 | +} |
| 66 | +
|
| 67 | +let mut module = Module::empty(); |
| 68 | +module.field_fn(Protocol::GET, "field", External::field_get)?; |
| 69 | +``` |
| 70 | + |
| 71 | +Would allow for this in Rune: |
| 72 | + |
| 73 | +```rune |
| 74 | +pub fn main(external) { |
| 75 | + println!("{}", external.field); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +[`Protocol::GET`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.GET |
| 80 | +[`Protocol::SET`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.SET |
| 81 | +[`Protocol::ADD_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.ADD_ASSIGN |
| 82 | +[`Protocol::SUB_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.SUB_ASSIGN |
| 83 | +[`Protocol::MUL_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.MUL_ASSIGN |
| 84 | +[`Protocol::DIV_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.DIV_ASSIGN |
| 85 | +[`Protocol::BIT_AND_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.BIT_AND_ASSIGN |
| 86 | +[`Protocol::BIT_OR_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.BIT_OR_ASSIGN |
| 87 | +[`Protocol::BIT_XOR_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.BIT_XOR_ASSIGN |
| 88 | +[`Protocol::SHL_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.SHL_ASSIGN |
| 89 | +[`Protocol::SHR_ASSIGN`]: https://docs.rs/runestick/0/runestick/struct.Protocol.html#associatedconstant.SHR_ASSIGN |
0 commit comments