Skip to main content

Condition

Enum Condition 

Source
pub enum Condition {
    KeyValue {
        key: ConditionKey,
        value: String,
    },
    Feature(String),
    All(Vec<Condition>),
    Any(Vec<Condition>),
    Not(Box<Condition>),
}
Expand description

Typed AST for a cfg(...) target condition.

The wire format matches the manifest text: a key/value (key = "value") leaf, or one of the all / any / not combinators. Equality and ordering are structural, so identical expressions always compare equal regardless of whitespace or quote style in the original source.

Variants§

§

KeyValue

key = "value". The key is restricted to the ConditionKey set; the value is a free-form ASCII string interpreted by evaluate.

Fields

§value: String
§

Feature(String)

feature = "name". Evaluates against the enabled-feature set of the package the condition belongs to, not the platform. Feature conditions are only meaningful — and only accepted — in flag tables ([target.'cfg(...)'.profile]); the manifest layer rejects a feature-referencing cfg that gates a dependency table, because feature resolution itself runs over the dependency graph and a feature→dependency edge would be circular.

§

All(Vec<Condition>)

all(<conditions>). Empty all() is rejected at parse time.

§

Any(Vec<Condition>)

any(<conditions>). Empty any() is rejected at parse time.

§

Not(Box<Condition>)

not(<single condition>).

Implementations§

Source§

impl Condition

Source

pub fn parse_cfg(input: &str) -> Result<Self, ConditionParseError>

Parse a full cfg(...) expression. The wrapping cfg(...) is required so the parser is symmetric with the manifest text users write.

§Errors

Returns ConditionParseError::ExpectedCfgPrefix when the input is not wrapped in cfg(, ConditionParseError::UnbalancedParens when the trailing ) is missing, and propagates any ConditionParseError from parsing the inner expression.

Source

pub fn parse_inner(input: &str) -> Result<Self, ConditionParseError>

Parse the inner expression of a cfg(...) form (no cfg( prefix or trailing )). Useful for the metadata round-trip path, where we store the inner form.

§Errors

Returns a ConditionParseError when the expression is malformed — e.g. an unsupported key, a missing = or quoted value, an empty all()/any(), a not() of wrong arity, unbalanced parentheses, or trailing input after the expression.

Source

pub fn evaluate( &self, platform: &TargetPlatform, features: &BTreeSet<String>, ) -> bool

Evaluate this condition against platform and the set of features enabled on the owning package. The result is fully determined by those inputs and the condition’s AST — no global state, no environment lookup, no I/O.

Platform contexts that carry no feature information (every dependency-gating call) pass an empty set; this is correct-by-construction because a feature-referencing cfg is rejected on dependency tables at manifest-load time, so a Feature leaf can only be reached here through a flag table that threaded the real enabled-feature set in.

Source

pub fn references_feature(&self) -> bool

Whether this condition references any feature = "..." leaf. Used by the manifest layer to reject feature conditions on dependency tables (where they would be circular) while allowing them on flag tables.

Trait Implementations§

Source§

impl Clone for Condition

Source§

fn clone(&self) -> Condition

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<'de> Deserialize<'de> for Condition

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Condition

Source§

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

Canonical string form. Round-trips through Condition::parse_inner.

Source§

impl Eq for Condition

Source§

impl Hash for Condition

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Condition

Source§

fn cmp(&self, other: &Condition) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Condition

Source§

fn eq(&self, other: &Condition) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Condition

Source§

fn partial_cmp(&self, other: &Condition) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Condition

Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Condition

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.