From 20f0379706417abd58ebbd988d1002aa2bf6c13f Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Fri, 10 Mar 2023 12:58:07 +0100 Subject: [PATCH] Change `template::format` to not be generic over `C` This potentially improves compile times a bit. The only thing the function needs is the `META` structure of `C`, which can be passed from the outside. The function is still generic over the formatter, but only three specific ones are passed. So maybe it's just compiled for those three already? One might also think about using dynamic dispatch for the formatter. Though compile times are likely dominated by all the generated code, especially the serde-derive code. --- src/json5.rs | 2 +- src/template.rs | 6 ++---- src/toml.rs | 2 +- src/yaml.rs | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/json5.rs b/src/json5.rs index e3c9536..11bb3ba 100644 --- a/src/json5.rs +++ b/src/json5.rs @@ -95,7 +95,7 @@ impl Default for FormatOptions { /// ``` pub fn template(options: FormatOptions) -> String { let mut out = Json5Formatter::new(&options); - template::format::(&mut out, options.general); + template::format(&C::META, &mut out, options.general); out.finish() } diff --git a/src/template.rs b/src/template.rs index ab5a8ce..6ed54f9 100644 --- a/src/template.rs +++ b/src/template.rs @@ -6,7 +6,7 @@ use std::fmt; -use crate::{Config, meta::{Meta, FieldKind, LeafKind, Expr}}; +use crate::meta::{Meta, FieldKind, LeafKind, Expr}; /// Trait abstracting over the format differences when it comes to formatting a @@ -146,9 +146,7 @@ impl Default for FormatOptions { /// If you don't need to use a custom formatter, rather look at the `format` /// functions in the format-specific modules (e.g. `toml::format`, /// `yaml::format`). -pub(crate) fn format(out: &mut impl Formatter, options: FormatOptions) { - let meta = &C::META; - +pub(crate) fn format(meta: &Meta, out: &mut impl Formatter, options: FormatOptions) { // Print root docs. if options.comments { meta.doc.iter().for_each(|doc| out.comment(doc)); diff --git a/src/toml.rs b/src/toml.rs index 9917b59..37a3ec7 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -93,7 +93,7 @@ impl Default for FormatOptions { /// ``` pub fn template(options: FormatOptions) -> String { let mut out = TomlFormatter::new(&options); - template::format::(&mut out, options.general); + template::format(&C::META, &mut out, options.general); out.finish() } diff --git a/src/yaml.rs b/src/yaml.rs index 28d280f..116e595 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -95,7 +95,7 @@ impl Default for FormatOptions { /// ``` pub fn template(options: FormatOptions) -> String { let mut out = YamlFormatter::new(&options); - template::format::(&mut out, options.general); + template::format(&C::META, &mut out, options.general); out.finish() }