{"id":13648792,"url":"https://github.com/jbaublitz/getset","last_synced_at":"2025-12-12T14:51:54.082Z","repository":{"id":40697833,"uuid":"93063160","full_name":"jbaublitz/getset","owner":"jbaublitz","description":"Getters and Setters for Rust.","archived":false,"fork":false,"pushed_at":"2025-04-18T13:53:30.000Z","size":158,"stargazers_count":355,"open_issues_count":21,"forks_count":28,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-07T10:53:31.345Z","etag":null,"topics":["getters","rust","rust-macro","setters"],"latest_commit_sha":null,"homepage":"https://docs.rs/getset/","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/jbaublitz.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":"2017-06-01T13:58:50.000Z","updated_at":"2025-05-04T02:25:42.000Z","dependencies_parsed_at":"2025-02-27T12:12:36.046Z","dependency_job_id":"757affc1-ada6-47d0-a997-19e5254ce4bb","html_url":"https://github.com/jbaublitz/getset","commit_stats":{"total_commits":120,"total_committers":15,"mean_commits":8.0,"dds":0.5833333333333333,"last_synced_commit":"e540fd438dada61f39f81a5cc87192cce9c9d1e0"},"previous_names":["hoverbear/getset"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbaublitz%2Fgetset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbaublitz%2Fgetset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbaublitz%2Fgetset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbaublitz%2Fgetset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jbaublitz","download_url":"https://codeload.github.com/jbaublitz/getset/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254083626,"owners_count":22011901,"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":["getters","rust","rust-macro","setters"],"created_at":"2024-08-02T01:04:32.745Z","updated_at":"2025-12-12T14:51:54.074Z","avatar_url":"https://github.com/jbaublitz.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# getset\n\n[![Download](https://img.shields.io/crates/d/getset)](https://crates.io/crates/getset)\n[![License](https://img.shields.io/crates/l/getset)](https://github.com/Hoverbear/getset/blob/master/LICENSE)\n[![Docs](https://docs.rs/getset/badge.svg)](https://docs.rs/getset/)\n[![Coverage Status](https://coveralls.io/repos/github/Hoverbear/getset/badge.svg)](https://coveralls.io/github/Hoverbear/getset)\n\nGetset, we're ready to go!\n\nA procedural macro for generating the most basic getters and setters on fields.\n\nGetters are generated as `fn field(\u0026self) -\u003e \u0026type`, while setters are generated as `fn field(\u0026mut self, val: type)`.\n\nThese macros are not intended to be used on fields which require custom logic inside of their setters and getters. Just write your own in that case!\n\n```rust\nuse std::sync::Arc;\n\nuse getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters};\n\n#[derive(Getters, Setters, WithSetters, MutGetters, CopyGetters, CloneGetters, Default)]\npub struct Foo\u003cT\u003e\nwhere\n    T: Copy + Clone + Default,\n{\n    /// Doc comments are supported!\n    /// Multiline, even.\n    #[getset(get, set, get_mut, set_with)]\n    private: T,\n\n    /// Doc comments are supported!\n    /// Multiline, even.\n    #[getset(get_copy = \"pub\", set = \"pub\", get_mut = \"pub\", set_with = \"pub\")]\n    public: T,\n\n    /// Arc supported through CloneGetters\n    #[getset(get_clone = \"pub\", set = \"pub\", get_mut = \"pub\", set_with = \"pub\")]\n    arc: Arc\u003cu16\u003e,\n}\n\nfn main() {\n    let mut foo = Foo::default();\n    foo.set_private(1);\n    (*foo.private_mut()) += 1;\n    assert_eq!(*foo.private(), 2);\n    foo = foo.with_private(3);\n    assert_eq!(*foo.private(), 3);\n    assert_eq!(*foo.arc(), 0);\n}\n```\n\nYou can use `cargo-expand` to generate the output. Here are the functions that the above generates (Replicate with `cargo expand --example simple`):\n\n```rust\nuse std::sync::Arc;                                        \nuse getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters};                                    \npub struct Foo\u003cT\u003e                                          \nwhere                                                      \n    T: Copy + Clone + Default,                             \n{                                                          \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                   \n    #[getset(get, set, get_mut, set_with)]                 \n    private: T,                                            \n    /// Doc comments are supported!                                                                                    \n    /// Multiline, even.                                   \n    #[getset(get_copy = \"pub\", set = \"pub\", get_mut = \"pub\", set_with = \"pub\")]                                        \n    public: T,                                             \n    /// Arc supported through CloneGetters                 \n    #[getset(get_clone = \"pub\", set = \"pub\", get_mut = \"pub\", set_with = \"pub\")]                                       \n    arc: Arc\u003cu16\u003e,                                         \n}                                                          \nimpl\u003cT\u003e Foo\u003cT\u003e                                             \nwhere                                                      \n    T: Copy + Clone + Default,                             \n{                                                          \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                                                                               \n    #[inline(always)]                                      \n    fn private(\u0026self) -\u003e \u0026T {                                                                                          \n        \u0026self.private                                      \n    }                                                      \n}                                                          \nimpl\u003cT\u003e Foo\u003cT\u003e                                             \nwhere                                                      \n    T: Copy + Clone + Default,                             \n{                                                          \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                   \n    #[inline(always)]                                      \n    fn set_private(\u0026mut self, val: T) -\u003e \u0026mut Self {       \n        self.private = val;\n        self                                                                                                           \n    }                                                      \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                   \n    #[inline(always)]                                                                                                  \n    pub fn set_public(\u0026mut self, val: T) -\u003e \u0026mut Self {    \n        self.public = val;                                 \n        self                                                                                                           \n    }                                                      \n    /// Arc supported through CloneGetters                 \n    #[inline(always)]                                      \n    pub fn set_arc(\u0026mut self, val: Arc\u003cu16\u003e) -\u003e \u0026mut Self {\n        self.arc = val;                                                                                                \n        self                                               \n    }                                                      \n}                                                          \nimpl\u003cT\u003e Foo\u003cT\u003e                                             \nwhere                                                      \n    T: Copy + Clone + Default,                             \n{                                                          \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                                                                               \n    #[inline(always)]                                      \n    fn with_private(mut self, val: T) -\u003e Self {                                                                        \n        self.private = val;                                \n        self                                               \n    }                                                                                                                  \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                   \n    #[inline(always)]                                      \n    pub fn with_public(mut self, val: T) -\u003e Self {         \n        self.public = val;                                 \n        self                                               \n    }                                                      \n    /// Arc supported through CloneGetters                                                                             \n    #[inline(always)]                                      \n    pub fn with_arc(mut self, val: Arc\u003cu16\u003e) -\u003e Self {                                                                 \n        self.arc = val;                                    \n        self                                               \n    }                                                      \n}                                                          \nimpl\u003cT\u003e Foo\u003cT\u003e                                             \nwhere                                                      \n    T: Copy + Clone + Default,                             \n{                                                          \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                   \n    #[inline(always)]                                      \n    fn private_mut(\u0026mut self) -\u003e \u0026mut T {\n        \u0026mut self.private                                                                                              \n    }                                                      \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                   \n    #[inline(always)]                                                                                                  \n    pub fn public_mut(\u0026mut self) -\u003e \u0026mut T {               \n        \u0026mut self.public                                   \n    }                                                                                                                  \n    /// Arc supported through CloneGetters                 \n    #[inline(always)]                                      \n    pub fn arc_mut(\u0026mut self) -\u003e \u0026mut Arc\u003cu16\u003e {           \n        \u0026mut self.arc                                      \n    }                                                                                                                  \n}                                                          \nimpl\u003cT\u003e Foo\u003cT\u003e                                             \nwhere                                                      \n    T: Copy + Clone + Default,                             \n{                                                          \n    /// Doc comments are supported!                        \n    /// Multiline, even.                                   \n    #[inline(always)]                                      \n    pub fn public(\u0026self) -\u003e T {                                                                                        \n        self.public                                        \n    }                                                                                                                  \n}                                                          \nimpl\u003cT\u003e Foo\u003cT\u003e                                             \nwhere                                                                                                                  \n    T: Copy + Clone + Default,                             \n{                                                          \n    /// Arc supported through CloneGetters                 \n    #[inline(always)]                                      \n    pub fn arc(\u0026self) -\u003e Arc\u003cu16\u003e {                        \n        self.arc.clone()                                   \n    }                                                      \n}\n```\n\nAttributes can be set on struct level for all fields in struct as well. Field level attributes take\nprecedence.\n\n```rust\n#[macro_use]\nextern crate getset;\n\nmod submodule {\n    #[derive(Getters, CopyGetters, Default)]\n    #[get_copy = \"pub\"] // By default add a pub getting for all fields.\n    pub struct Foo {\n        public: i32,\n        #[get_copy] // Override as private\n        private: i32,\n    }\n    fn demo() {\n        let mut foo = Foo::default();\n        foo.private();\n    }\n}\nfn main() {\n    let mut foo = submodule::Foo::default();\n    foo.public();\n}\n```\n\nFor some purposes, it's useful to have the `get_` prefix on the getters for\neither legacy of compatability reasons. It is done with `with_prefix`.\n\n```rust\n#[macro_use]\nextern crate getset;\n\n#[derive(Getters, Default)]\npub struct Foo {\n    #[get = \"pub with_prefix\"]\n    field: bool,\n}\n\nfn main() {\n    let mut foo = Foo::default();\n    let val = foo.get_field();\n}\n```\n\nSkipping setters and getters generation for a field when struct level attribute is used\nis possible with `#[getset(skip)]`.\n\n```rust\nuse getset::{CopyGetters, Setters};\n\n#[derive(CopyGetters, Setters)]\n#[getset(get_copy, set, set_with)]\npub struct Foo {\n    // If the field was not skipped, the compiler would complain about moving\n    // a non-copyable type in copy getter.\n    #[getset(skip)]\n    skipped: String,\n\n    field1: usize,\n    field2: usize,\n}\n\nimpl Foo {\n    // It is possible to write getters and setters manually,\n    // possibly with a custom logic.\n    fn skipped(\u0026self) -\u003e \u0026str {\n        \u0026self.skipped\n    }\n\n    fn set_skipped(\u0026mut self, val: \u0026str) -\u003e \u0026mut Self {\n        self.skipped = val.to_string();\n        self\n    }\n\n    fn with_skipped(mut self, val: \u0026str) -\u003e Self {\n        self.skipped = val.to_string();\n        self\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbaublitz%2Fgetset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjbaublitz%2Fgetset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbaublitz%2Fgetset/lists"}