diff --git a/src/bindgen/items.rs b/src/bindgen/items.rs index e4d41cc011df9763e277fb1b29e9e83ff7e29b7d..12cee9f46c9ac775087c4987d230ff290a0c1400 100644 --- a/src/bindgen/items.rs +++ b/src/bindgen/items.rs @@ -422,20 +422,7 @@ impl Function { out.write(&format!("{} {}(", &ret, name)); - for (i, arg) in args.iter().enumerate() { - out.write(arg); - if i != args.len() - 1 { - out.write(","); - if i == 0 { - let align_length = ret.len() + name.len() + 2; - out.push_set_spaces(align_length); - } - out.new_line(); - } - } - if args.len() > 1 { - out.pop_tab(); - } + out.write_aligned_list(args, format!(",")); out.write(")"); if let Some(ref postfix) = postfix { out.new_line(); @@ -501,20 +488,21 @@ impl Struct { pub fn write<F: Write>(&self, config: &Config, out: &mut Writer<F>) { assert!(self.generic_params.is_empty()); - // Calculate overriden fields, should be done in convert + // This needs to be done here because `type` specialization may + // provide an alternative list of directives to use for renaming let fields = match self.directives.list("field-names") { Some(overrides) => { - let mut fields = Vec::new(); + let mut overriden_fields = Vec::new(); for (i, &(ref name, ref ty)) in self.fields.iter().enumerate() { if i >= overrides.len() { - fields.push((name.clone(), ty.clone())); + overriden_fields.push((name.clone(), ty.clone())); } else { - fields.push((overrides[i].clone(), ty.clone())); + overriden_fields.push((overrides[i].clone(), ty.clone())); } } - fields + overriden_fields } _ => self.fields.clone(), }; @@ -539,39 +527,30 @@ impl Struct { out.write(&format!("bool operator{}(const {}& aOther) const", op, self.name)); out.open_brace(); out.write("return "); - for (i, field) in fields.iter().enumerate() { - if i == 1 { - out.push_tab(); - } - if i != 0 { - out.write(&format!(" {}", conjuc)); - out.new_line(); - } - out.write(&format!("{} {} aOther.{}", field.0, op, field.0)); - } + out.write_aligned_list(fields.iter() + .map(|x| format!("{} {} aOther.{}", x.0, op, x.0)) + .collect(), + format!(" {}", conjuc)); out.write(";"); - if fields.len() > 1 { - out.pop_tab(); - } out.close_brace(false); }; - if config.structure.derive_eq(&self.directives) && !self.fields.is_empty() { + if config.structure.derive_eq(&self.directives) && !fields.is_empty() { emit_op("==", "&&"); } - if config.structure.derive_neq(&self.directives) && !self.fields.is_empty() { + if config.structure.derive_neq(&self.directives) && !fields.is_empty() { emit_op("!=", "||"); } - if config.structure.derive_lt(&self.directives) && self.fields.len() == 1 { + if config.structure.derive_lt(&self.directives) && fields.len() == 1 { emit_op("<", "&&"); } - if config.structure.derive_lte(&self.directives) && self.fields.len() == 1 { + if config.structure.derive_lte(&self.directives) && fields.len() == 1 { emit_op("<=", "&&"); } - if config.structure.derive_gt(&self.directives) && self.fields.len() == 1 { + if config.structure.derive_gt(&self.directives) && fields.len() == 1 { emit_op(">", "&&"); } - if config.structure.derive_gte(&self.directives) && self.fields.len() == 1 { + if config.structure.derive_gte(&self.directives) && fields.len() == 1 { emit_op(">=", "&&"); } } diff --git a/src/bindgen/writer.rs b/src/bindgen/writer.rs index 0947a086ab5ab15163991b1ed510b8412f247dc9..8716eb594e64828f1678838d39d641ff931f30e1 100644 --- a/src/bindgen/writer.rs +++ b/src/bindgen/writer.rs @@ -5,8 +5,8 @@ pub struct Writer<'a, 'f, F: 'f + Write> { out: &'f mut F, config: &'a Config, spaces: Vec<usize>, - pending_spaces: usize, - has_written: bool, + line_started: bool, + line_length: usize, } impl<'a, 'f, F: Write> Writer<'a, 'f, F> { @@ -15,8 +15,8 @@ impl<'a, 'f, F: Write> Writer<'a, 'f, F> { out: out, config: config, spaces: vec![0], - pending_spaces: 0, - has_written: false, + line_started: false, + line_length: 0, } } @@ -24,49 +24,39 @@ impl<'a, 'f, F: Write> Writer<'a, 'f, F> { *self.spaces.last().unwrap() } + + fn push_set_spaces(&mut self, spaces: usize) { + self.spaces.push(spaces); + } + pub fn push_tab(&mut self) { let spaces = self.spaces() - (self.spaces() % self.config.tab_width) + self.config.tab_width; self.spaces.push(spaces); - - if !self.has_written { - self.pending_spaces = self.spaces(); - } - } - pub fn push_set_spaces(&mut self, spaces: usize) { - self.spaces.push(spaces); - - if !self.has_written { - self.pending_spaces = self.spaces(); - } } pub fn pop_tab(&mut self) { assert!(!self.spaces.is_empty()); self.spaces.pop(); - - if !self.has_written { - self.pending_spaces = self.spaces(); - } } pub fn new_line(&mut self) { write!(self.out, "\n").unwrap(); - self.pending_spaces = self.spaces(); - self.has_written = false; + self.line_started = false; + self.line_length = 0; } pub fn open_brace(&mut self) { match self.config.braces { Braces::SameLine => { - write!(self.out, " {{").unwrap(); + self.write(" {"); self.push_tab(); self.new_line(); } Braces::NextLine => { self.new_line(); - write!(self.out, "{{").unwrap(); + self.write("{"); self.push_tab(); self.new_line(); } @@ -84,12 +74,28 @@ impl<'a, 'f, F: Write> Writer<'a, 'f, F> { } pub fn write(&mut self, text: &str) { - for _ in 0..self.pending_spaces { - write!(self.out, " ").unwrap(); + if !self.line_started { + for _ in 0..self.spaces() { + write!(self.out, " ").unwrap(); + } + self.line_started = true; + self.line_length += self.spaces(); } - self.pending_spaces = 0; - self.has_written = true; - write!(self.out, "{}", text).unwrap() + write!(self.out, "{}", text).unwrap(); + self.line_length += text.len(); + } + + pub fn write_aligned_list(&mut self, items: Vec<String>, join: String) { + let align_length = self.line_length; + self.push_set_spaces(align_length); + for (i, item) in items.iter().enumerate() { + self.write(item); + if i != items.len() - 1 { + self.write(&join); + self.new_line(); + } + } + self.pop_tab(); } }