Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 20 additions & 38 deletions src/bindgen/ir/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,41 +217,19 @@ impl Cfg {
}

pub trait ToCondition: Sized {
type Output;

fn to_condition(self, config: &Config) -> Option<Self::Output>;
}

impl<'a> ToCondition for &'a Option<Cfg> {
type Output = Condition;

fn to_condition(self, config: &Config) -> Option<Self::Output> {
self.clone().and_then(|cfg| cfg.to_condition(config))
}
fn to_condition(&self, config: &Config) -> Option<Condition>;
}

impl ToCondition for Option<Cfg> {
type Output = Condition;

fn to_condition(self, config: &Config) -> Option<Self::Output> {
self.and_then(|cfg| cfg.to_condition(config))
impl<'a> ToCondition for Option<Cfg> {
fn to_condition(&self, config: &Config) -> Option<Condition> {
self.as_ref()?.to_condition(config)
}
}

impl<'a> ToCondition for &'a Cfg {
type Output = Condition;

fn to_condition(self, config: &Config) -> Option<Self::Output> {
self.clone().to_condition(config)
}
}

impl ToCondition for Cfg {
type Output = Condition;

fn to_condition(self, config: &Config) -> Option<Self::Output> {
match self {
Cfg::Boolean(cfg_name) => {
impl<'a> ToCondition for Cfg {
fn to_condition(&self, config: &Config) -> Option<Condition> {
match *self {
Cfg::Boolean(ref cfg_name) => {
let define = config
.defines
.iter()
Expand All @@ -261,12 +239,12 @@ impl ToCondition for Cfg {
} else {
warn!(
"Missing `[defines]` entry for `{}` in cbindgen config.",
Cfg::Boolean(cfg_name)
self,
);
None
}
}
Cfg::Named(cfg_name, cfg_value) => {
Cfg::Named(ref cfg_name, ref cfg_value) => {
let define = config.defines.iter().find(|(key, ..)| {
DefineKey::Named(&cfg_name, &cfg_value) == DefineKey::load(key)
});
Expand All @@ -275,14 +253,14 @@ impl ToCondition for Cfg {
} else {
warn!(
"Missing `[defines]` entry for `{}` in cbindgen config.",
Cfg::Named(cfg_name, cfg_value)
self,
);
None
}
}
Cfg::Any(children) => {
Cfg::Any(ref children) => {
let conditions: Vec<_> = children
.into_iter()
.iter()
.filter_map(|x| x.to_condition(config))
.collect();
match conditions.len() {
Expand All @@ -291,9 +269,9 @@ impl ToCondition for Cfg {
_ => Some(Condition::Any(conditions)),
}
}
Cfg::All(children) => {
Cfg::All(ref children) => {
let cfgs: Vec<_> = children
.into_iter()
.iter()
.filter_map(|x| x.to_condition(config))
.collect();
match cfgs.len() {
Expand All @@ -302,7 +280,7 @@ impl ToCondition for Cfg {
_ => Some(Condition::All(cfgs)),
}
}
Cfg::Not(child) => child
Cfg::Not(ref child) => child
.to_condition(config)
.map(|cfg| Condition::Not(Box::new(cfg))),
}
Expand Down Expand Up @@ -361,16 +339,20 @@ pub trait ConditionWrite {
impl ConditionWrite for Option<Condition> {
fn write_before<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
if let Some(ref cfg) = *self {
out.push_set_spaces(0);
out.write("#if ");
cfg.write(config, out);
out.pop_set_spaces();
out.new_line();
}
}

fn write_after<F: Write>(&self, _config: &Config, out: &mut SourceWriter<F>) {
if self.is_some() {
out.new_line();
out.push_set_spaces(0);
out.write("#endif");
out.pop_set_spaces();
}
}
}
2 changes: 1 addition & 1 deletion src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ impl Constant {
&& config.constant.allow_static_const
&& !associated_to_transparent;

let condition = (&self.cfg).to_condition(config);
let condition = self.cfg.to_condition(config);
condition.write_before(config, out);

let name = if in_body {
Expand Down
Loading