Struct cfg_expr::expr::Expression
source · [−]pub struct Expression { /* private fields */ }
Expand description
A parsed cfg()
expression that can evaluated
Implementations
Given a cfg() expression (the cfg( and ) are optional), attempts to parse it into a form where it can be evaluated
assert!(cfg_expr::Expression::parse(r#"cfg(all(unix, target_arch = "x86_64"))"#).is_ok());
An iterator over each predicate in the expression
Evaluates the expression, using the provided closure to determine the value of each predicate, which are then combined into a final result depending on the functions not(), all(), or any() in the expression.
eval_predicate
typically returns bool
, but may return any type that implements
the Logic
trait.
Examples
use cfg_expr::{targets::*, Expression, Predicate};
let linux_musl = get_builtin_target_by_triple("x86_64-unknown-linux-musl").unwrap();
let expr = Expression::parse(r#"all(not(windows), target_env = "musl", any(target_arch = "x86", target_arch = "x86_64"))"#).unwrap();
assert!(expr.eval(|pred| {
match pred {
Predicate::Target(tp) => tp.matches(linux_musl),
_ => false,
}
}));
Returning Option<bool>
, where None
indicates the result is unknown:
use cfg_expr::{targets::*, Expression, Predicate};
let expr = Expression::parse(r#"any(target_feature = "sse2", target_env = "musl")"#).unwrap();
let linux_gnu = get_builtin_target_by_triple("x86_64-unknown-linux-gnu").unwrap();
let linux_musl = get_builtin_target_by_triple("x86_64-unknown-linux-musl").unwrap();
fn eval(expr: &Expression, target: &TargetInfo) -> Option<bool> {
expr.eval(|pred| {
match pred {
Predicate::Target(tp) => Some(tp.matches(target)),
Predicate::TargetFeature(_) => None,
_ => panic!("unexpected predicate"),
}
})
}
// Whether the target feature is present is unknown, so the whole expression evaluates to
// None (unknown).
assert_eq!(eval(&expr, linux_gnu), None);
// Whether the target feature is present is irrelevant for musl, since the any() always
// evaluates to true.
assert_eq!(eval(&expr, linux_musl), Some(true));
The original string which has been parsed to produce this Expression
.
use cfg_expr::Expression;
assert_eq!(
Expression::parse("any()").unwrap().original(),
"any()"
);
Trait Implementations
PartialEq
will do a syntactical comparaison, so will just check if both
expressions have been parsed from the same string, not if they are semantically
equivalent.
use cfg_expr::Expression;
assert_eq!(
Expression::parse("any()").unwrap(),
Expression::parse("any()").unwrap()
);
assert_ne!(
Expression::parse("any()").unwrap(),
Expression::parse("unix").unwrap()
);
Auto Trait Implementations
impl RefUnwindSafe for Expression
impl Send for Expression
impl Sync for Expression
impl Unpin for Expression
impl UnwindSafe for Expression
Blanket Implementations
Mutably borrows from an owned value. Read more