Add support for field functions and deriving default implementations #169
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This introduces "field functions", which are functions that can be registered to operate over field accesses. The most famous of which are getters and setters.
The
Any
derive can also generate default implementations of these field functions through various#[rune(...)]
attributes:Once registered, this allows
Foo
to be used like this in Rune:The full list of available field functions and their corresponding derives are:
#[rune(get)]
- for field getters (Protocol::GET
).#[rune(set)]
- for field setters (Protocol::SET
).#[rune(add_assign)]
- the+=
operation (Protocol::ADD_ASSIGN
).#[rune(sub_assign)]
- the-=
operation (Protocol::SUB_ASSIGN
).#[rune(mul_assign)]
- the*=
operation (Protocol::MUL_ASSIGN
).#[rune(div_assign)]
- the/=
operation (Protocol::DIV_ASSIGN
).#[rune(bit_and_assign)]
- the&=
operation (Protocol::BIT_AND_ASSIGN
).#[rune(bit_or_assign)]
- the|=
operation (Protocol::BIT_OR_ASSIGN
).#[rune(bit_xor_assign)]
- the^=
operation (Protocol::BIT_XOR_ASSIGN
).#[rune(shl_assign)]
- the<<=
operation (Protocol::SHL_ASSIGN
).#[rune(shr_assign)]
- the>>=
operation (Protocol::SHR_ASSIGN
).The manual way to register these functions is to use the new
Module::field_fn
function. This clearly showcases that there's no relationship between the field used and the function registered:Would allow for this in Rune:
TODO
Make it possible to overload the implementation function by specifying an argument to the given operation, like:
#[rune(add_assign = "my_add_assign")]
.