{"id":29682383,"url":"https://github.com/honhimw/struct-with-defaults","last_synced_at":"2025-08-30T14:15:24.468Z","repository":{"id":303571783,"uuid":"1015916993","full_name":"honhimW/struct-with-defaults","owner":"honhimW","description":"define struct with default field values like rfc3681 via proc-macro","archived":false,"fork":false,"pushed_at":"2025-08-24T11:34:10.000Z","size":51,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-24T17:12:23.145Z","etag":null,"topics":["default-values","proc-macro"],"latest_commit_sha":null,"homepage":"","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/honhimW.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,"zenodo":null}},"created_at":"2025-07-08T08:16:58.000Z","updated_at":"2025-08-24T11:34:13.000Z","dependencies_parsed_at":"2025-08-24T13:15:38.046Z","dependency_job_id":"b5b6ac76-7476-4638-ac63-33a184547ece","html_url":"https://github.com/honhimW/struct-with-defaults","commit_stats":null,"previous_names":["honhimw/struct-with-defaults"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/honhimW/struct-with-defaults","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honhimW%2Fstruct-with-defaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honhimW%2Fstruct-with-defaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honhimW%2Fstruct-with-defaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honhimW%2Fstruct-with-defaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/honhimW","download_url":"https://codeload.github.com/honhimW/struct-with-defaults/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/honhimW%2Fstruct-with-defaults/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272858568,"owners_count":25005100,"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","status":"online","status_checked_at":"2025-08-30T02:00:09.474Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["default-values","proc-macro"],"created_at":"2025-07-23T02:36:18.179Z","updated_at":"2025-08-30T14:15:24.462Z","avatar_url":"https://github.com/honhimW.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Macro 3681\n\n### FYI\n- [Rust rfcs-3681](https://github.com/rust-lang/rfcs/pull/3681)\n- [Tracking issue for RFC 3681: Default field values](https://github.com/rust-lang/rust/issues/132162)\n\n## About this crate\n\n#### MSRV\nSince crate `syn`'s msrv is `1.61`, that also the msrv for this crate.\n\n- Thinking about default values in structs.  \n- Implement via proc-macro.\n- RFC-3681 default field values must be `const`, which is not enough for most cases e.g. `String`.\n\n## Usage\n\n```shell\ncargo add macro3681\n```\n\n### With Default derive\n\nDefault derive will be automatically removed, and generate an impl with default field values.\nIf there are none-default fields, those fields will be added as constructor parameters. \n```rust\ndefault_field_values! {\n    #[derive(Default, Debug)]\n    struct Example\u003c'a, T, T2: Default\u003e where T: Default {\n        i: u32 = 3,\n        j: i128,\n        i_option: Option\u003cu64\u003e = Some(1000),\n        string: String = {\n            let s = format!(\"{} {}\", \"hello\", \"world\");\n            s\n        },\n        os: Option\u003cString\u003e,\n        foo: Foo = _, // #[derive(Default, Debug)] struct Foo { .. }\n        bytes: \u0026'a[u8] = b\"hello world\",\n        t: T,\n        t2: T2,\n    }\n    \n    #[derive(Default, Debug)]\n    struct Tuple\u003c'a, T: Default\u003e(T, #[allow(unused)] \u0026'a str = \"abc\", Foo = _, Option\u003cString\u003e);\n}\n```\n\n#### Macro Expansion\n\n```rust\n#[derive(Debug)]\nstruct Example\u003c'a, T, T2\u003e\nwhere\n    T: Default,\n{\n    i: u32,\n    j: i128,\n    i_option: Option\u003cu64\u003e,\n    string: String,\n    os: Option\u003cString\u003e,\n    foo: Foo,\n    bytes: \u0026'a [u8],\n    t: T,\n    t2: T2,\n}\nimpl\u003c'a, T, T2: Default\u003e Example\u003c'a, T, T2\u003e\nwhere\n    T: Default,\n{\n    pub fn new(j: i128, os: Option\u003cString\u003e, t: T, t2: T2) -\u003e Self {\n        Self {\n            i: 3,\n            j,\n            i_option: Some(1000),\n            string: {\n                let s = format!(\"{} {}\", \"hello\", \"world\");\n                s\n            },\n            os,\n            foo: Default::default(),\n            bytes: b\"hello world\",\n            t,\n            t2,\n        }\n    }\n}\nimpl\u003c'a, T, T2: Default\u003e Default for Example\u003c'a, T, T2\u003e\nwhere\n    T: Default,\n{\n    fn default() -\u003e Self {\n        Self {\n            i: 3,\n            i_option: Some(1000),\n            string: {\n                let s = format!(\"{} {}\", \"hello\", \"world\");\n                s\n            },\n            foo: Default::default(),\n            bytes: b\"hello world\",\n            j: Default::default(),\n            os: Default::default(),\n            t: Default::default(),\n            t2: Default::default(),\n        }\n    }\n}\n\n#[derive(Debug)]\nstruct Tuple\u003c'a, T\u003e (T, #[allow(unused)]   \u0026'a str, Foo, Option\u003cString\u003e );\nimpl\u003c'a, T: Default\u003e Tuple\u003c'a, T\u003e { pub fn new(_0: T, _3: Option\u003cString\u003e) -\u003e Self { Self(_0, \"abc\", Default::default(), _3 ) } }\nimpl\u003c'a, T: Default\u003e Default for Tuple\u003c'a, T\u003e { fn default() -\u003e Self { Self(Default::default(), \"abc\", Default::default(), Default::default() ) } \n```\n\n### Without Default derive\n\nWill not generate `Self::default()` function, only `Self::new(..)` is available\n```rust\ndefault_field_values! {\n    #[derive(Clone, Debug)]\n    pub(crate) struct Bar {\n        pub i: u32,\n        j: u32 = 100 * 4,\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhonhimw%2Fstruct-with-defaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhonhimw%2Fstruct-with-defaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhonhimw%2Fstruct-with-defaults/lists"}