Repository navigation
Generate const fn accessors with const_fn - #125
Merged
Merged
Conversation
Accessors that only move a `Copy` field out of `&self`, or hand back a
`&mut` to it, are valid in a const context, but fieldwork always emitted a
plain `fn`. A type whose getters are the only way to read its fields could
therefore not be read from a `const` item at all, and hand-written const
accessors had to be kept alongside the derived ones.
`const_fn` is an ordinary cascading boolean setting, so it resolves the same
way as `copy` or `chain`: field-method, field, item-method, item. It is
opt-in and defaults to false, so no existing generated code changes.
#[derive(fieldwork::Fieldwork)]
#[fieldwork(get, const_fn)]
struct Counters {
count: usize,
}
The key is spelled `const_fn` rather than `const` because `const` is a
reserved word and does not parse as a path in the attribute's expression
list.
Whether the body is actually const is left to the compiler: `deref` and
`into` call trait methods, and a setter drops the old value, so those
combinations report a normal const-eval error at the generated method.
Owner
|
Looks good, thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Adds a
const_fnsetting that makes fieldwork generateconst fnaccessors.It is an ordinary cascading boolean, so it resolves through the usual four
levels — field-method, field, item-method, item — and can be turned on for a
whole type and off for one field, or the other way round:
Why
An accessor that copies a
Copyfield out of&self, or returns&muttoit, is already inside the const subset — but the generated method is a plain
fn, so a type whose fields are private cannot be read from aconstitemat all through its own API. Today that means keeping hand-written const
accessors next to the derived ones, which is exactly the duplication the
derive exists to remove.
Notes
false: no existing generated code changes.const_fn, notconst, becauseconstis a reserved word anddoes not parse as a path inside the attribute's expression list.
pre-validated by the macro:
derefcallsDeref::deref,intocallsInto::into, and a setter drops the previous value, so those combinationsproduce a normal const-eval error pointing at the generated method.
docs/const_fn.mdspells this out.Tests
tests/expand/expand_29_const_fn.rscovering theitem-level and field-level forms over
getandget_mut.tests/ui/invalid_config.stderrupdated:const_fnnow appears in therecognized-key list.
docs/const_fn.md, included under#[cfg(doc)]like the other topicpages;
cargo run -p docs-gen -- --verifyis clean.cargo test --allpasses.cargo clippy --all-targets -- -Dwarningsreportsthe same 13 findings before and after this change on my toolchain (all in
files this PR does not touch), so I left them alone.