{"id":13503163,"url":"https://github.com/nrc/derive-new","last_synced_at":"2025-05-14T19:05:25.212Z","repository":{"id":45638755,"uuid":"70296528","full_name":"nrc/derive-new","owner":"nrc","description":"derive simple constructor functions for Rust structs","archived":false,"fork":false,"pushed_at":"2024-08-29T02:17:52.000Z","size":112,"stargazers_count":544,"open_issues_count":14,"forks_count":37,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-03T02:48:06.770Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nrc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-08T02:10:24.000Z","updated_at":"2025-03-16T23:32:00.000Z","dependencies_parsed_at":"2022-09-26T18:11:00.944Z","dependency_job_id":"21b448f5-7b56-4ce1-87be-453d1d83bee0","html_url":"https://github.com/nrc/derive-new","commit_stats":{"total_commits":83,"total_committers":23,"mean_commits":3.608695652173913,"dds":0.6385542168674698,"last_synced_commit":"ef5df7078155ba77ffc584bb696522b8219d4c58"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrc%2Fderive-new","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrc%2Fderive-new/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrc%2Fderive-new/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nrc%2Fderive-new/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nrc","download_url":"https://codeload.github.com/nrc/derive-new/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161264,"owners_count":21057552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T22:02:39.612Z","updated_at":"2025-04-10T04:51:38.033Z","avatar_url":"https://github.com/nrc.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# A custom derive implementation for `#[derive(new)]`\n\nA `derive(new)` attribute creates a `new` constructor function for the annotated\ntype. That function takes an argument for each field in the type giving a\ntrivial constructor. This is useful since as your type evolves you can make the\nconstructor non-trivial (and add or remove fields) without changing client code\n(i.e., without breaking backwards compatibility). It is also the most succinct\nway to initialise a struct or an enum.\n\nImplementation uses macros 1.1 custom derive (which works in stable Rust from\n1.15 onwards).\n\n`#[no_std]` is fully supported if you switch off the default feature `\"std\"`.\n\n## Examples\n\nCargo.toml:\n\n```toml\n[dependencies]\nderive-new = \"0.5\"\n```\n\nInclude the macro:\n\n* Rust Edition 2015\n\n  ```rust\n  #[macro_use]\n  extern crate derive_new;\n  ```\n\n* Rust Edition 2018\n  ```rust\n  use derive_new::new;\n  ```\n\nGenerating constructor for a simple struct:\n\n```rust\n#[derive(new)]\nstruct Bar {\n    a: i32,\n    b: String,\n}\n\nlet _ = Bar::new(42, \"Hello\".to_owned());\n```\n\nDefault values can be specified either via `#[new(default)]` attribute which removes\nthe argument from the constructor and populates the field with `Default::default()`,\nor via `#[new(value = \"..\")]` which initializes the field with a given expression:\n\n```rust\n#[derive(new)]\nstruct Foo {\n    x: bool,\n    #[new(value = \"42\")]\n    y: i32,\n    #[new(default)]\n    z: Vec\u003cString\u003e,\n}\n\nlet _ = Foo::new(true);\n```\n\nTo make type conversion easier, `#[new(into)]` attribute changes the parameter type\nto `impl Into\u003cT\u003e`, and populates the field with `value.into()`:\n\n```rust\n#[derive(new)]\nstruct Foo {\n    #[new(into)]\n    x: String,\n}\n\nlet _ = Foo::new(\"Hello\");\n```\n\nFor iterators/collections, `#[new(into_iter = \"T\")]` attribute changes the parameter type\nto `impl IntoIterator\u003cItem = T\u003e`, and populates the field with `value.into_iter().collect()`:\n\n```rust\n#[derive(new)]\nstruct Foo {\n    #[new(into_iter = \"bool\")]\n    x: Vec\u003cbool\u003e,\n}\n\nlet _ = Foo::new([true, false]);\nlet _ = Foo::new(Some(true));\n```\n\nGeneric types are supported; in particular, `PhantomData\u003cT\u003e` fields will be not\nincluded in the argument list and will be initialized automatically:\n\n```rust\nuse std::marker::PhantomData;\n\n#[derive(new)]\nstruct Generic\u003c'a, T: Default, P\u003e {\n    x: \u0026'a str,\n    y: PhantomData\u003cP\u003e,\n    #[new(default)]\n    z: T,\n}\n\nlet _ = Generic::\u003ci32, u8\u003e::new(\"Hello\");\n```\n\nFor enums, one constructor method is generated for each variant, with the type\nname being converted to snake case; otherwise, all features supported for\nstructs work for enum variants as well:\n\n```rust\n#[derive(new)]\nenum Enum {\n    FirstVariant,\n    SecondVariant(bool, #[new(default)] u8),\n    ThirdVariant { x: i32, #[new(value = \"vec![1]\")] y: Vec\u003cu8\u003e }\n}\n\nlet _ = Enum::new_first_variant();\nlet _ = Enum::new_second_variant(true);\nlet _ = Enum::new_third_variant(42);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnrc%2Fderive-new","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnrc%2Fderive-new","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnrc%2Fderive-new/lists"}