Skip to content

Commit

Permalink
Allow special cases for bitflags in structs (and proper truncation if…
Browse files Browse the repository at this point in the history
… bitflags)
  • Loading branch information
Luke Jones committed Jul 6, 2017
1 parent c39bdde commit 0165ee3
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/codegen/sys/lib_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ fn generate_enums(w: &mut Write, env: &Env, items: &[&Enumeration]) -> Result<()
}

fn generate_unions(w: &mut Write, env: &Env, items: &[&Union]) -> Result<()> {

if !items.is_empty() {
try!(writeln!(w, "// Unions"));
}
Expand Down Expand Up @@ -363,8 +364,11 @@ fn generate_unions(w: &mut Write, env: &Env, items: &[&Union]) -> Result<()> {
}
}
}
if !items.is_empty() {
try!(writeln!(w, ""));
#[cfg(not(feature = "use_unions"))]
{
if !items.is_empty() {
try!(writeln!(w, ""));
}
}

Ok(())
Expand Down Expand Up @@ -530,7 +534,7 @@ fn generate_fields(env: &Env, struct_name: &str, fields: &[Field]) -> (Vec<Strin
}
}

if !cfg!(feature = "use_unions") || is_bits {
if !cfg!(feature = "use_unions") || (is_bits && !truncated) {
if !is_gweakref && !truncated && !is_ptr &&
(is_union || is_bits) &&
!is_union_special_case(&field.c_type)
Expand All @@ -543,24 +547,24 @@ fn generate_fields(env: &Env, struct_name: &str, fields: &[Field]) -> (Vec<Strin
lines.push("\t_truncated_record_marker: c_void,".to_owned());
truncated = true;
}
}

if truncated {
if is_union {
lines.push("\t//union,".to_owned());
} else {
let bits = field
.bits
.map(|n| format!(": {}", n))
.unwrap_or_else(|| "".to_owned());
lines.push(format!(
"\t//{}: {}{},",
field.name,
field.c_type.as_ref().map(|s| &s[..]).unwrap_or("fn"),
bits
));
};
continue 'fields;
}
if truncated {
if is_union {
lines.push("\t//union,".to_owned());
} else {
let bits = field
.bits
.map(|n| format!(": {}", n))
.unwrap_or_else(|| "".to_owned());
lines.push(format!(
"\t//{}: {}{},",
field.name,
field.c_type.as_ref().map(|s| &s[..]).unwrap_or("fn"),
bits
));
};
continue 'fields;
}

if let Some(ref c_type) = field.c_type {
Expand Down

0 comments on commit 0165ee3

Please sign in to comment.