Skip to main content

Condition

Struct Condition 

Source
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

artifacthow the condition reaches it
an item, a struct field, a variant, a match arm, an array elementthe attribute verbatim — gate
a POSITION among the present columns — a bind ordinal, a fence indexpresent, 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

Source

pub fn always() -> Self

The condition a field with no #[cfg] is under: none.

Source

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.

Source

pub fn decorations(&self) -> &[Attribute]

The #[cfg_attr(…)] the field’s STAND-IN carries — the rewrite’s reader, and nothing else.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Condition

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Condition

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Condition

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.