Procházet zdrojové kódy

Output the fields in sorted order

Ian Goldberg před 1 měsícem
rodič
revize
e447eee5a2
1 změnil soubory, kde provedl 17 přidání a 1 odebrání
  1. 17 1
      sigma-compiler-core/src/sigma/codegen.rs

+ 17 - 1
sigma-compiler-core/src/sigma/codegen.rs

@@ -11,6 +11,7 @@ use std::collections::HashSet;
 use syn::{Expr, Ident};
 
 /// Names and types of fields that might end up in a generated struct
+#[derive(Clone)]
 pub enum StructField {
     Scalar(Ident),
     VecScalar(Ident),
@@ -18,6 +19,17 @@ pub enum StructField {
     VecPoint(Ident),
 }
 
+impl StructField {
+    /// Extract the [`struct@Ident`] from the [`StructField`]
+    pub fn ident(&self) -> Ident {
+        match self {
+            Self::Scalar(id) | Self::VecScalar(id) | Self::Point(id) | Self::VecPoint(id) => {
+                id.clone()
+            }
+        }
+    }
+}
+
 /// A list of StructField items
 #[derive(Default)]
 pub struct StructFieldList {
@@ -65,7 +77,11 @@ impl StructFieldList {
     /// Output a ToTokens of code to dump the contents of the fields to
     /// the `std::fmt::Formatter` with the given `fmt_id`
     pub fn dump(&self, fmt_id: &Ident) -> impl ToTokens {
-        let dump_chunks = self.fields.iter().map(|f| match f {
+        // Sort the field ids
+        let mut fields = self.fields.clone();
+        fields.sort_by_key(|f| f.ident());
+
+        let dump_chunks = fields.iter().map(|f| match f {
             StructField::Scalar(id) => quote! {
                 write!(#fmt_id, "  {}: ", stringify!(#id));
                 Instance::dump_scalar(&self.#id, #fmt_id);