pub struct Condition { /* private fields */ }Expand description
The CONDITION a field is DECLARED UNDER — the #[cfg(…)] attributes written on it.
§The macro never learns whether the predicate holds, and that IS the mechanism
A proc macro has no API for asking whether a cfg predicate is satisfied, and in the REWRITE
mode the field never reaches the compiler at all — so nothing here evaluates anything. What the
seat does is CARRY the author’s own tokens onto every artifact the field mints, and leave the
answer to the one party that has it. The family is stripped in a single pass, by the compiler,
or it is kept whole.
§Two kinds of artifact, and the seat serves them differently
| artifact | how the condition reaches it |
|---|---|
| an item, a struct field, a variant, a match arm, an array element | the attribute verbatim — gate |
| a POSITION among the present columns — a bind ordinal, a fence index | present, summed in a const the compiler folds AFTER stripping |
The second is the whole reason this type is not just a list of attributes. An expansion that
wrote a bind ordinal as the literal 2 would be naming a position the build no longer has the
moment a column before it is configured out, and a literal is exactly what an emitter reaching
for enumerate() produces. So where any column is conditional, every ordinal is written as a
running sum of these — 0, 0 + p₀, 0 + p₀ + p₁ — and the compiler folds it after it has
answered the predicates.
§cfg reaches the FAMILY and cfg_attr reaches ONE artifact, because they ask different things
#[cfg] is about EXISTENCE, and existence is what the whole family follows — a column, a filter
seat and a reader for a field the build removed are the disease this seat cures, so the gate has
to reach every one of them.
#[cfg_attr(pred, …)] removes nothing. It conditionally DECORATES, so it has no presence to
account for and nothing to strip, and repeating it across the family would put an author’s
serde(rename = "…") on a method. It rides the one artifact that STANDS IN for the field: the
echoed field itself in an ordinary declaration, where the host already forwards it verbatim and
this type never sees it; the READER in a rewrite, beside the doc comments that migrate there for
the same reason — see decorations.
Where its payload is this grammar’s OWN word, it is refused instead: a macro that cannot read
pred cannot know whether #[ingraph(id)] was written. See ingraph_derive::attribute.
Implementations§
Source§impl Condition
impl Condition
Sourcepub fn of(gates: Vec<Attribute>, decorations: Vec<Attribute>) -> Self
pub fn of(gates: Vec<Attribute>, decorations: Vec<Attribute>) -> Self
The condition the given attributes state — #[cfg(…)] for the family, #[cfg_attr(…)] for
the stand-in — each list in written order.
SEVERAL #[cfg] are a conjunction, which is Rust’s own rule for several on one item, and it
is why present writes all(…) rather than one predicate.
Sourcepub fn decorations(&self) -> &[Attribute]
pub fn decorations(&self) -> &[Attribute]
The #[cfg_attr(…)] the field’s STAND-IN carries — the rewrite’s reader, and nothing else.
Sourcepub fn always_present(&self) -> bool
pub fn always_present(&self) -> bool
Whether the field is present in EVERY build — the common case, and the one that has to expand to exactly what it expanded to before this seat existed.
Sourcepub fn gate(&self) -> TokenStream
pub fn gate(&self) -> TokenStream
The attributes to prefix onto ONE artifact of the field.
Empty for an unconditional field, so an emitter interpolates this unconditionally and a
declaration that wrote no cfg pays not one token for the seat.
Sourcepub fn either(&self, present: &TokenStream, absent: &TokenStream) -> TokenStream
pub fn either(&self, present: &TokenStream, absent: &TokenStream) -> TokenStream
ONE of two expressions, chosen by this field’s predicate — for the seats where an attribute cannot go.
gate serves every position Rust lets an attribute sit in: an item, a struct
literal’s field, an array element, a statement, a match arm. A TERM of an expression is not one
of them — #[cfg] on an expression is stmt_expr_attributes and unstable — and a folded
a && b && c over a list the predicates CONTRACT is exactly that seat. So the fork moves one
grain out, into a block whose two lets are STATEMENTS, and the compiler strips the arm its
answer removed.
An unconditional field gets present back untouched, so a declaration that wrote no #[cfg]
pays not one token for the seat — gate’s own discipline.
Sourcepub fn present(&self) -> TokenStream
pub fn present(&self) -> TokenStream
1 where the field is present and 0 where it is not — a const expression the COMPILER
evaluates, after it has stripped what the predicate removes.
The two arms are ITEMS inside a block rather than an attribute on an expression, which is what
makes this stable Rust: #[cfg] on an expression needs stmt_expr_attributes, and #[cfg] on
a const item inside a block needs nothing.