From 5439fa195bbece443d261d40bf08d77012d44e58 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 3 Nov 2020 18:45:01 -0800 Subject: [PATCH] Preserve relative order of structs and enums --- gen/src/write.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/gen/src/write.rs b/gen/src/write.rs index fd2a815ab..b8d1dd48f 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -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 { let mut out_file = OutFile::new(header, opt, types); @@ -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) { @@ -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 {