glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.31k stars 1.07k forks source link

Add useCow option for Rust #2490

Open surma opened 7 months ago

surma commented 7 months ago

(I am not sure if there’s actually interest in this, but I thought I’d share an idea as a PR and we can take it from here.)

I am using QuickType to generate Rust structs so I can parse a LDTK file (a JSON blob) that is bundled with my binary. Since the JSON blob is already embedded in the binary, it seems wasteful to use String. Instead, for most intents and purposes, it’s functionally identical to use Cow<'_, str> instead of String, but without having to copy all the strings to the heap.

This PR adds a new --use-cow option for Rust, which replaces the occurrences of String> with Cow<'a, str>. Because of the introduction of lifetimes, there is also a bit of machinery as a struct containing a string now needs to have a lifetime itself, and this needs to be recursively propagate up the type tree.

Example before/after Before: ```rust #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct World { /// Default new level height pub default_level_height: i64, /// Default new level width pub default_level_width: i64, /// User defined unique identifier pub identifier: String, /// Unique instance identifer pub iid: String, /// All levels from this world. The order of this array is only relevant in /// `LinearHorizontal` and `linearVertical` world layouts (see `worldLayout` value). /// Otherwise, you should refer to the `worldX`,`worldY` coordinates of each Level. pub levels: Vec, /// Height of the world grid in pixels. pub world_grid_height: i64, /// Width of the world grid in pixels. pub world_grid_width: i64, /// An enum that describes how levels are organized in this project (ie. linearly or in a 2D /// space). Possible values: `Free`, `GridVania`, `LinearHorizontal`, `LinearVertical`, `null` pub world_layout: Option, } ``` After: ```rust #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct World<'a> { /// Default new level height pub default_level_height: i64, /// Default new level width pub default_level_width: i64, /// User defined unique identifier pub identifier: Cow<'a, str>, /// Unique instance identifer pub iid: Cow<'a, str>, /// All levels from this world. The order of this array is only relevant in /// `LinearHorizontal` and `linearVertical` world layouts (see `worldLayout` value). /// Otherwise, you should refer to the `worldX`,`worldY` coordinates of each Level. pub levels: Vec>, /// Height of the world grid in pixels. pub world_grid_height: i64, /// Width of the world grid in pixels. pub world_grid_width: i64, /// An enum that describes how levels are organized in this project (ie. linearly or in a 2D /// space). Possible values: `Free`, `GridVania`, `LinearHorizontal`, `LinearVertical`, `null` pub world_layout: Option, } ```
dvdsgl commented 7 months ago

We really appreciate the contribution, but our CI is in a broken state right now, which prevents us from evaluating new contributions.

dvdsgl commented 7 months ago

Our CI is fixed! Please rebase.

surma commented 7 months ago

Done :)