Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify generated enum code #286

Merged
merged 6 commits into from
May 6, 2024
Merged

Simplify generated enum code #286

merged 6 commits into from
May 6, 2024

Conversation

GREsau
Copy link
Owner

@GREsau GREsau commented May 6, 2024

Mitigates #246

Based on #266, but moves new functions into _private module. They are likely to be moved/changed when the structure of Schema changes, so I really don't want them to be part of the public API.

Description from original PR:

Partial solution for #246

  • replace the manual construction of Schema::Object with a helper method
  • replace metadata setting with a function call per-metadata

Taking the enum from https://github.com/adamchalmers/rust-problems/blob/achalmers/schemars-llvm-lines/src/lib.rs and running cargo rustc -- -Z unpretty=mir | wc -l it goes from 165725 lines of MIR to 4871 (removing the serde derives from the linked code)

The per-variant generate code goes from:

{
    let schema = schemars::schema::Schema::Object(schemars::schema::SchemaObject {
        instance_type: Some(
            schemars::schema::InstanceType::String.into(),
        ),
        enum_values: Some(
            <[_]>::into_vec(
                #[rustc_box]
                ::alloc::boxed::Box::new(["Af".into()]),
            ),
        ),
        ..Default::default()
    });
    schemars::_private::apply_metadata(
        schema,
        schemars::schema::Metadata {
            description: Some("Afghanistan".to_owned()),
            ..Default::default()
        },
    )
},

to:

{
    let schema = schemars::schema::Schema::Object(
        schemars::schema::SchemaObject::new_unit_enum("Af"),
    );
    schema.with_description("Afghanistan")
},

I still have a crate that takes forever to generate the schema for so there are still plenty of room for improvement.

@GREsau GREsau merged commit d04c17b into master May 6, 2024
8 checks passed
@GREsau GREsau deleted the simplify-generated-enum-code branch May 6, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants