add support of feature flag for runnables #4464

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
Benjamin Coenen 2020-05-22 09:23:31 +02:00
parent a7c8aa7c60
commit 43339058e3
2 changed files with 9 additions and 21 deletions

View file

@ -35,14 +35,11 @@ impl CfgExpr {
} }
/// Return minimal features needed /// Return minimal features needed
pub fn minimal_features_needed(&self) -> Option<Vec<SmolStr>> { pub fn minimal_features_needed(&self) -> Vec<SmolStr> {
let mut features = vec![]; let mut features = vec![];
self.collect_minimal_features_needed(&mut features); self.collect_minimal_features_needed(&mut features);
if features.is_empty() {
None features
} else {
Some(features)
}
} }
fn collect_minimal_features_needed(&self, features: &mut Vec<SmolStr>) { fn collect_minimal_features_needed(&self, features: &mut Vec<SmolStr>) {
@ -169,14 +166,14 @@ mod tests {
let (subtree, _) = get_token_tree_generated(r#"#![cfg(feature = "baz")]"#); let (subtree, _) = get_token_tree_generated(r#"#![cfg(feature = "baz")]"#);
let cfg_expr = parse_cfg(&subtree); let cfg_expr = parse_cfg(&subtree);
assert_eq!(cfg_expr.minimal_features_needed().unwrap(), vec![SmolStr::new("baz")]); assert_eq!(cfg_expr.minimal_features_needed(), vec![SmolStr::new("baz")]);
let (subtree, _) = let (subtree, _) =
get_token_tree_generated(r#"#![cfg(all(feature = "baz", feature = "foo"))]"#); get_token_tree_generated(r#"#![cfg(all(feature = "baz", feature = "foo"))]"#);
let cfg_expr = parse_cfg(&subtree); let cfg_expr = parse_cfg(&subtree);
assert_eq!( assert_eq!(
cfg_expr.minimal_features_needed().unwrap(), cfg_expr.minimal_features_needed(),
vec![SmolStr::new("baz"), SmolStr::new("foo")] vec![SmolStr::new("baz"), SmolStr::new("foo")]
); );
@ -184,11 +181,11 @@ mod tests {
get_token_tree_generated(r#"#![cfg(any(feature = "baz", feature = "foo", unix))]"#); get_token_tree_generated(r#"#![cfg(any(feature = "baz", feature = "foo", unix))]"#);
let cfg_expr = parse_cfg(&subtree); let cfg_expr = parse_cfg(&subtree);
assert_eq!(cfg_expr.minimal_features_needed().unwrap(), vec![SmolStr::new("baz")]); assert_eq!(cfg_expr.minimal_features_needed(), vec![SmolStr::new("baz")]);
let (subtree, _) = get_token_tree_generated(r#"#![cfg(foo)]"#); let (subtree, _) = get_token_tree_generated(r#"#![cfg(foo)]"#);
let cfg_expr = parse_cfg(&subtree); let cfg_expr = parse_cfg(&subtree);
assert!(cfg_expr.minimal_features_needed().is_none()); assert!(cfg_expr.minimal_features_needed().is_empty());
} }
} }

View file

@ -190,17 +190,8 @@ fn runnable_mod(
fn get_features_needed(attrs: Attrs) -> Option<Vec<SmolStr>> { fn get_features_needed(attrs: Attrs) -> Option<Vec<SmolStr>> {
let cfg_expr = attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)); let cfg_expr = attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree));
let features_needed = cfg_expr.fold(vec![], |mut acc, cfg| { let features_needed = cfg_expr.map(|cfg| cfg.minimal_features_needed()).flatten().collect();
if let Some(features_needed) = cfg.minimal_features_needed() { Some(features_needed).filter(|it: &Vec<SmolStr>| !it.is_empty())
acc.extend(features_needed);
}
acc
});
if features_needed.is_empty() {
None
} else {
Some(features_needed)
}
} }
#[cfg(test)] #[cfg(test)]