Skip to content

Commit

Permalink
Preserve relative order of structs and enums
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 4, 2020
1 parent 818004f commit 5439fa1
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::syntax::{
Var,
};
use proc_macro2::Ident;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

pub(super) fn gen(apis: &[Api], types: &Types, opt: &Opt, header: bool) -> Vec<u8> {
let mut out_file = OutFile::new(header, opt, types);
Expand Down Expand Up @@ -76,8 +76,22 @@ fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
}
}

let mut structs_written = HashSet::new();
let mut toposorted_structs = out.types.toposorted_structs.iter();
for api in apis {
match api {
Api::Struct(strct) if !structs_written.contains(&strct.name.rust) => {
for next in &mut toposorted_structs {
if !out.types.cxx.contains(&strct.name.rust) {
out.next_section();
write_struct(out, next);
}
structs_written.insert(&next.name.rust);
if next.name.rust == strct.name.rust {
break;
}
}
}
Api::Enum(enm) => {
out.next_section();
if out.types.cxx.contains(&enm.name.rust) {
Expand All @@ -96,13 +110,6 @@ fn write_data_structures<'a>(out: &mut OutFile<'a>, apis: &'a [Api]) {
}
}

for strct in &out.types.toposorted_structs {
out.next_section();
if !out.types.cxx.contains(&strct.name.rust) {
write_struct(out, strct);
}
}

out.next_section();
for api in apis {
if let Api::TypeAlias(ety) = api {
Expand Down

0 comments on commit 5439fa1

Please sign in to comment.