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.
This commit is contained in:
Lukas Kalbertodt
2023-03-10 12:58:07 +01:00
parent 2c19c0eed9
commit 20f0379706
4 changed files with 5 additions and 7 deletions

View File

@@ -95,7 +95,7 @@ impl Default for FormatOptions {
/// ```
pub fn template<C: Config>(options: FormatOptions) -> String {
let mut out = Json5Formatter::new(&options);
template::format::<C>(&mut out, options.general);
template::format(&C::META, &mut out, options.general);
out.finish()
}

View File

@@ -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<C: Config>(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));

View File

@@ -93,7 +93,7 @@ impl Default for FormatOptions {
/// ```
pub fn template<C: Config>(options: FormatOptions) -> String {
let mut out = TomlFormatter::new(&options);
template::format::<C>(&mut out, options.general);
template::format(&C::META, &mut out, options.general);
out.finish()
}

View File

@@ -95,7 +95,7 @@ impl Default for FormatOptions {
/// ```
pub fn template<C: Config>(options: FormatOptions) -> String {
let mut out = YamlFormatter::new(&options);
template::format::<C>(&mut out, options.general);
template::format(&C::META, &mut out, options.general);
out.finish()
}