Merge pull request #246 from dtolnay/pragma

#pragma once needs to be above includes
This commit is contained in:
David Tolnay 2020-07-31 11:16:02 -07:00 committed by GitHub
commit a05bb4ecd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 18 deletions

View File

@ -127,9 +127,6 @@ impl Display for Includes {
writeln!(f, "#include <BaseTsd.h>")?;
writeln!(f, "#endif")?;
}
if *self != Self::default() {
writeln!(f)?;
}
Ok(())
}
}

View File

@ -51,16 +51,15 @@ impl OutFile {
}
}
pub fn prepend(&mut self, section: String) {
let content = self.content.get_mut();
content.bytes.splice(..0, section.into_bytes());
}
pub fn write_fmt(&self, args: Arguments) {
let content = &mut *self.content.borrow_mut();
Write::write_fmt(content, args).unwrap();
}
pub fn extend(&self, other: &Self) {
self.content.borrow_mut().write_bytes(&other.content.borrow().bytes);
}
pub fn content(&self) -> Vec<u8> {
self.content.borrow().bytes.clone()
}
@ -68,7 +67,14 @@ impl OutFile {
impl Write for Content {
fn write_str(&mut self, s: &str) -> fmt::Result {
if !s.is_empty() {
self.write_bytes(s.as_bytes());
Ok(())
}
}
impl Content {
fn write_bytes(&mut self, b: &[u8]) {
if !b.is_empty() {
if !self.blocks_pending.is_empty() {
if !self.bytes.is_empty() {
self.bytes.push(b'\n');
@ -84,8 +90,7 @@ impl Write for Content {
}
self.section_pending = false;
}
self.bytes.extend_from_slice(s.as_bytes());
self.bytes.extend_from_slice(b);
}
Ok(())
}
}

View File

@ -17,10 +17,6 @@ pub(super) fn gen(
let mut out_file = OutFile::new(namespace.clone(), header);
let out = &mut out_file;
if header {
writeln!(out, "#pragma once");
}
out.include.extend(opt.include);
for api in apis {
if let Api::Include(include) = api {
@ -114,9 +110,17 @@ pub(super) fn gen(
write_generic_instantiations(out, types);
}
out.prepend(out.include.to_string());
out_file
// We collected necessary includes lazily while generating the above. Now
// put it all together.
let mut full_file = OutFile::new(namespace.clone(), header);
let full = &mut full_file;
if header {
writeln!(full, "#pragma once");
}
write!(full, "{}", out.include);
full.next_section();
full.extend(out);
full_file
}
fn write_includes(out: &mut OutFile, types: &Types) {