### Rust: Getters with 'get_' Prefix using with_prefix Source: https://github.com/jbaublitz/getset/blob/main/README.md Illustrates how to generate getters with the 'get_' prefix using the `with_prefix` option in the `#[get]` attribute. This is useful for legacy compatibility or specific naming conventions. ```rust #[macro_use] extern crate getset; #[derive(Getters, Default)] pub struct Foo { #[get = "pub with_prefix"] field: bool, } fn main() { let mut foo = Foo::default(); let val = foo.get_field(); } ``` -------------------------------- ### Unary Tuple Struct Support with getset Source: https://context7.com/jbaublitz/getset/llms.txt Provides support for generating accessors for single-field tuple structs. The `getset` macro simplifies method names for these types, offering methods like `get`, `get_mut`, and `get_copy` for convenient access. This is useful for creating newtype wrappers. ```rust use getset::{Setters, Getters, MutGetters, CopyGetters}; #[derive(Setters, Getters, MutGetters)] struct UnaryTuple(#[getset(set, get, get_mut)] i32); #[derive(CopyGetters)] struct CopyUnaryTuple(#[getset(get_copy)] i32); fn main() { let mut tup = UnaryTuple(42); assert_eq!(tup.get(), &42); assert_eq!(tup.get_mut(), &mut 42); tup.set(43); assert_eq!(tup.get(), &43); let copy_tup = CopyUnaryTuple(42); let val: i32 = copy_tup.get(); } ``` -------------------------------- ### Rust: Derive Getters, Setters, and More with getset Source: https://github.com/jbaublitz/getset/blob/main/README.md Demonstrates the usage of various getset derive macros (Getters, Setters, WithSetters, MutGetters, CopyGetters, CloneGetters) on a generic struct `Foo`. Shows how to apply these macros to different fields with varying visibility and functionality. ```rust use std::sync::Arc; use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters}; #[derive(Getters, Setters, WithSetters, MutGetters, CopyGetters, CloneGetters, Default)] pub struct Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[getset(get, set, get_mut, set_with)] private: T, /// Doc comments are supported! /// Multiline, even. #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")] public: T, /// Arc supported through CloneGetters #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")] arc: Arc, } fn main() { let mut foo = Foo::default(); foo.set_private(1); (*foo.private_mut()) += 1; assert_eq!(*foo.private(), 2); foo = foo.with_private(3); assert_eq!(*foo.private(), 3); assert_eq!(*foo.arc(), 0); } ``` -------------------------------- ### Prefixed Getters for Legacy Compatibility with getset Source: https://context7.com/jbaublitz/getset/llms.txt Shows how to add a 'get_' prefix to getter methods using the `with_prefix` option. This is beneficial for maintaining compatibility with legacy APIs or when a specific naming convention is required. The `getset` macro handles the prefix generation automatically. ```rust use getset::Getters; #[derive(Getters, Default)] pub struct LegacyAPI { #[getset(get = "pub with_prefix")] field: bool, } fn main() { let api = LegacyAPI::default(); let val = api.get_field(); // Note the get_ prefix } ``` -------------------------------- ### Generic Types and Where Clauses with getset Source: https://context7.com/jbaublitz/getset/llms.txt Demonstrates the `getset` library's full support for generic types, including those with trait bounds specified using `where` clauses. This allows for flexible and type-safe accessor generation for generic data structures. ```rust use getset::{Getters, Setters}; #[derive(Getters, Setters, Default)] #[getset(get = "pub", set = "pub")] pub struct Container where T: Clone + Default, { item: T, count: usize, } fn main() { let mut container = Container::::default(); container.set_item("hello".to_string()); let item: &String = container.item(); } ``` -------------------------------- ### Rust: Skipping Getter/Setter Generation with #[getset(skip)] Source: https://github.com/jbaublitz/getset/blob/main/README.md Demonstrates how to skip the automatic generation of getters and setters for specific fields using the `#[getset(skip)]` attribute. This allows for manual implementation of these methods while still leveraging the macro for other fields. ```rust use getset::{CopyGetters, Setters}; #[derive(CopyGetters, Setters)] #[getset(get_copy, set, set_with)] pub struct Foo { // If the field was not skipped, the compiler would complain about moving // a non-copyable type in copy getter. #[getset(skip)] skipped: String, field1: usize, field2: usize, } impl Foo { // It is possible to write getters and setters manually, // possibly with a custom logic. fn skipped(&self) -> &str { &self.skipped } fn set_skipped(&mut self, val: &str) -> &mut Self { self.skipped = val.to_string(); self } fn with_skipped(mut self, val: &str) -> Self { self.skipped = val.to_string(); self } } ``` -------------------------------- ### Rust: Generated Getters and Setters by cargo expand Source: https://github.com/jbaublitz/getset/blob/main/README.md Illustrates the output of the `getset` derive macros when expanded by `cargo-expand`. This shows the actual Rust code generated for getter and setter methods based on the attributes applied in the original struct definition. ```rust use std::sync::Arc; use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters}; pub struct Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[getset(get, set, get_mut, set_with)] private: T, /// Doc comments are supported! /// Multiline, even. #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")] public: T, /// Arc supported through CloneGetters #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")] arc: Arc, } impl Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] fn private(&self) -> &T { &self.private } } impl Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] fn set_private(&mut self, val: T) -> &mut Self { self.private = val; self } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub fn set_public(&mut self, val: T) -> &mut Self { ``` -------------------------------- ### Rust Combined Derives for Multiple Field Accessors Source: https://context7.com/jbaublitz/getset/llms.txt Demonstrates how to use multiple Getset derive macros (`Getters`, `MutGetters`, `Setters`, `WithSetters`, `CopyGetters`, `CloneGetters`) simultaneously on a single struct and its fields. Allows for fine-grained control over visibility and access types for each field, including support for generics and trait bounds. ```rust use getset::{Getters, MutGetters, Setters, WithSetters, CopyGetters, CloneGetters}; use std::sync::Arc; #[derive(Getters, Setters, WithSetters, MutGetters, CopyGetters, CloneGetters, Default)] pub struct Foo where T: Copy + Clone + Default, { /// Private field with all accessors #[getset(get, set, get_mut, set_with)] private: T, /// Public copyable field #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")] public: T, /// Arc field with clone getter #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")] arc: Arc, } fn main() { let mut foo = Foo::default(); foo.set_private(1); (*foo.private_mut()) += 1; assert_eq!(*foo.private(), 2); foo = foo.with_private(3); assert_eq!(*foo.private(), 3); } ``` -------------------------------- ### Rust Builder-Style Setter with Self Consume using WithSetters Macro Source: https://context7.com/jbaublitz/getset/llms.txt Generates builder-style setter methods that consume `self` and return it (e.g., `fn with_field(mut self, val: Type) -> Self`). This pattern is implemented using the `WithSetters` derive macro and is suitable for immutable struct initialization patterns. ```rust use getset::WithSetters; #[derive(WithSetters, Default)] pub struct Config { #[getset(set_with = "pub")] host: String, #[getset(set_with = "pub")] port: u16, } fn main() { let config = Config::default() .with_host("localhost".to_string()) .with_port(8080); } ``` -------------------------------- ### Visibility Modifiers for Generated Methods with getset Source: https://context7.com/jbaublitz/getset/llms.txt Explains how to control the visibility of generated getter methods using standard Rust visibility syntax (e.g., `pub`, `pub(crate)`). This allows fine-grained control over which parts of the API can access struct fields, enhancing encapsulation. ```rust use getset::Getters; #[derive(Getters, Default)] pub struct Example { #[getset(get)] // Private getter private: i32, #[getset(get = "pub")] // Public getter public: i32, #[getset(get = "pub(crate)")] // Crate-visible getter crate_visible: i32, } fn main() { let ex = Example::default(); let val = ex.public(); let crate_val = ex.crate_visible(); } ``` -------------------------------- ### Rust Clone Getter for Clone Types with CloneGetters Macro Source: https://context7.com/jbaublitz/getset/llms.txt Generates getter methods that return cloned values (e.g., `fn field(&self) -> Type`) for types implementing the `Clone` trait, using the `CloneGetters` derive macro. This is useful for types like `String`, `Vec`, or `Arc` where a copy is desired but the type might not be `Copy`. ```rust use getset::CloneGetters; use std::sync::Arc; #[derive(CloneGetters, Default)] pub struct SharedData { #[getset(get_clone = "pub")] arc: Arc, #[getset(get_clone = "pub")] buffer: Vec, } fn main() { let data = SharedData::default(); let arc_copy: Arc = data.arc(); // Clones the Arc let buffer_copy: Vec = data.buffer(); // Clones the Vec } ``` -------------------------------- ### Struct-Level Attributes with Field Overrides using getset Source: https://context7.com/jbaublitz/getset/llms.txt Demonstrates applying attributes at the struct level for default getter generation and overriding specific fields. Supports public copy getters by default, with options to make them private or skip generation entirely. Useful for defining consistent access patterns across a struct. ```rust use getset::{Getters, CopyGetters}; #[derive(Getters, CopyGetters, Default)] #[getset(get_copy = "pub")] // Default: public copy getters for all fields pub struct Point { x: i32, // Gets public copy getter #[getset(get_copy)] // Override: private copy getter y: i32, #[getset(skip)] // Skip getter generation entirely internal_id: String, } fn main() { let point = Point::default(); let x = point.x(); // Public // y() is private // internal_id() doesn't exist } ``` -------------------------------- ### Skipping Fields for Custom Logic with getset Source: https://context7.com/jbaublitz/getset/llms.txt Illustrates how to skip automatic getter/setter generation for specific fields using `#[getset(skip)]`. This allows developers to implement custom logic for accessing or modifying fields, such as validation or transformation, while still leveraging the macro for other fields. ```rust use getset::{CopyGetters, Setters, WithSetters}; #[derive(CopyGetters, Setters, WithSetters)] #[getset(get_copy, set, set_with)] pub struct Foo { #[getset(skip)] skipped: String, field1: usize, field2: usize, } impl Foo { // Custom getter with validation logic pub fn skipped(&self) -> &str { &self.skipped } // Custom setter with transformation pub fn set_skipped(&mut self, val: &str) -> &mut Self { self.skipped = val.to_uppercase(); self } // Custom builder-style setter pub fn with_skipped(mut self, val: &str) -> Self { self.skipped = val.to_uppercase(); self } } ``` -------------------------------- ### Rust Value Getter for Copy Types with CopyGetters Macro Source: https://context7.com/jbaublitz/getset/llms.txt Generates getter methods that return values by copy (e.g., `fn field(&self) -> Type`) specifically for types implementing the `Copy` trait, using the `CopyGetters` derive macro. This avoids borrowing and is efficient for small, primitive types. ```rust use getset::CopyGetters; #[derive(CopyGetters, Default)] pub struct Config { #[getset(get_copy = "pub")] port: u16, #[getset(get_copy = "pub")] timeout: u64, } fn main() { let config = Config::default(); let port: u16 = config.port(); // Returns by value, not reference let timeout: u64 = config.timeout(); } ``` -------------------------------- ### Rust: Implementing Arc Getter with CloneGetters Source: https://github.com/jbaublitz/getset/blob/main/README.md Demonstrates how to implement an Arc getter for a struct field using the CloneGetters trait. This ensures thread-safe access to shared data through cloning. ```rust impl { /// Arc supported through CloneGetters #[inline(always)] pub fn arc(&self) -> Arc { self.arc.clone() } } ``` -------------------------------- ### Rust Setter with Mutable Reference Return using Setters Macro Source: https://context7.com/jbaublitz/getset/llms.txt Generates setter methods that return a mutable reference to `self` (e.g., `fn set_field(&mut self, val: Type) -> &mut Self`), facilitating method chaining. This is achieved using the `Setters` derive macro. ```rust use getset::Setters; #[derive(Setters, Default)] pub struct Builder { #[getset(set = "pub")] name: String, #[getset(set = "pub")] age: u32, } fn main() { let mut builder = Builder::default(); builder.set_name("Alice".to_string()) .set_age(30); } ``` -------------------------------- ### Rust: Default Public Getters with CopyGetters Source: https://github.com/jbaublitz/getset/blob/main/README.md Shows how to use the `#[derive(Getters, CopyGetters, Default)]` macro to automatically generate public copy getters for all fields in a struct. Field-level attributes can override this behavior. ```rust #[macro_use] extern crate getset; mod submodule { #[derive(Getters, CopyGetters, Default)] #[get_copy = "pub"] // By default add a pub getting for all fields. pub struct Foo { public: i32, #[get_copy] // Override as private private: i32, } fn demo() { let mut foo = Foo::default(); foo.private(); } } fn main() { let mut foo = submodule::Foo::default(); foo.public(); } ``` -------------------------------- ### Rust Mutable Reference Getter with MutGetters Macro Source: https://context7.com/jbaublitz/getset/llms.txt Generates mutable reference getter methods (e.g., `fn field_mut(&mut self) -> &mut Type`) for struct fields using the `MutGetters` derive macro. This allows modifying the field's value through the generated accessor. ```rust use getset::MutGetters; #[derive(MutGetters, Default)] pub struct Counter { #[getset(get_mut = "pub")] value: i32, } fn main() { let mut counter = Counter::default(); *counter.value_mut() += 10; assert_eq!(*counter.value_mut(), 10); } ``` -------------------------------- ### Rust Immutable Reference Getter with Getters Macro Source: https://context7.com/jbaublitz/getset/llms.txt Generates immutable reference getter methods (e.g., `fn field(&self) -> &Type`) for struct fields using the `Getters` derive macro. Supports custom visibility modifiers for generated methods. Field-level configuration can override struct-level derive settings. ```rust use getset::Getters; #[derive(Getters, Default)] pub struct Person { #[getset(get = "pub")] name: String, #[getset(get)] age: u32, } fn main() { let person = Person::default(); let name: &String = person.name(); // age() is private to the module } ``` -------------------------------- ### Rust: Implement Setters for Foo Struct Fields Source: https://github.com/jbaublitz/getset/blob/main/README.md Provides setter methods for the `private`, `public`, and `arc` fields of a generic `Foo` struct. These methods allow for modification of the struct's internal state and return a mutable reference to `self` for chaining. ```Rust impl Foo where T: Copy + Clone + Default, { /// Arc supported through CloneGetters #[inline(always)] pub fn set_arc(&mut self, val: Arc) -> &mut Self { self.arc = val; self } } impl Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] fn with_private(mut self, val: T) -> Self { self.private = val; self } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub fn with_public(mut self, val: T) -> Self { self.public = val; self } /// Arc supported through CloneGetters #[inline(always)] pub fn with_arc(mut self, val: Arc) -> Self { self.arc = val; self } } ``` -------------------------------- ### Rust: Implement Mutable Getters for Foo Struct Fields Source: https://github.com/jbaublitz/getset/blob/main/README.md Provides mutable getter methods for the `private`, `public`, and `arc` fields of a generic `Foo` struct. These methods return a mutable reference to the respective fields, allowing for in-place modification. ```Rust impl Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] fn private_mut(&mut self) -> &mut T { &mut self.private } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub fn public_mut(&mut self) -> &mut T { &mut self.public } /// Arc supported through CloneGetters #[inline(always)] pub fn arc_mut(&mut self) -> &mut Arc { &mut self.arc } } ``` -------------------------------- ### Rust: Implement Immutable Getter for Foo Struct Public Field Source: https://github.com/jbaublitz/getset/blob/main/README.md Provides an immutable getter method for the `public` field of a generic `Foo` struct. This method returns a copy of the `public` field, ensuring that the original field remains unmodified. ```Rust impl Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub fn public(&self) -> T { self.public } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.