### Complex getset2 Example: Combined Attributes Source: https://context7.com/andeya/getset2/llms.txt Demonstrates a complex scenario combining global struct-level defaults with field-specific overrides for accessor generation. It showcases mixing `get_ref`, `set`, `set_with`, `get_mut`, and visibility controls. ```rust use getset2::Getset2; #[derive(Default, Getset2)] #[getset2(get_ref, set_with)] pub struct ApiClient where T: Copy + Clone + Default, { /// Base URL for API requests #[getset2(set, get_mut, skip(get_ref))] base_url: String, /// API timeout in milliseconds #[getset2( get_copy(pub, const), set(pub = "crate"), get_mut(pub = "super"), set_with(pub = "self", const) )] timeout: T, /// Internal implementation detail #[getset2(skip)] connection_pool: (), } impl ApiClient { fn base_url(&self) -> &String { &self.base_url } fn connection_pool(&self) { self.connection_pool } } fn main() { let mut client = ApiClient::::default(); // Field-specific set method (overrides default set_with) client.set_base_url("https://api.example.com".to_string()); // Mutable access client.base_url_mut().push_str("/v1"); // Verify changes assert_eq!(client.base_url(), "https://api.example.com/v1"); // Builder pattern with const let client = client.with_base_url("https://new.api.com".to_string()); assert_eq!(client.base_url(), "https://new.api.com"); // Public const getter for timeout client.set_timeout(5000); assert_eq!(client.timeout(), 5000); } ``` -------------------------------- ### Rust: Basic Struct Decoration with Getset2 Source: https://context7.com/andeya/getset2/llms.txt Demonstrates the fundamental usage of the `#[derive(Getset2)]` macro to automatically generate getter methods for struct fields. This basic setup applies default getter behaviors, typically immutable references, to all fields in the decorated struct. ```rust use getset2::Getset2; #[derive(Default, Getset2)] #[getset2(get_ref)] pub struct User { name: String, age: u32, } fn main() { let user = User { name: "Alice".to_string(), age: 30, }; // Generated getter: name() returns &String assert_eq!(user.name(), "Alice"); assert_eq!(*user.age(), 30); } ``` -------------------------------- ### Rust: Expanded Code for getset2 Macro Usage Source: https://github.com/andeya/getset2/blob/getset2/README.md This snippet shows the equivalent Rust code generated by the `getset2` derive macro after running `cargo expand`. It illustrates the actual getter, setter, and other accessor methods created by the macro based on the provided attributes in the original struct definition. ```rust #![feature(prelude_import)] #[prelude_import] use std::prelude::rust_2018::*; #[macro_use] extern crate std; use getset2::Getset2; #[getset2(get_ref, set_with)] pub struct Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[getset2(set, get_mut, skip(get_ref))] private: T, /// Doc comments are supported! /// Multiline, even. #[getset2( get_copy(pub, const), set(pub = "crate"), get_mut(pub = "super"), set_with(pub = "self", const) )] public: T, #[getset2(skip)] skip: (), } #[automatically_derived] impl ::core::default::Default for Foo where T: Copy + Clone + Default, { #[inline] fn default() -> Foo { Foo { private: ::core::default::Default::default(), public: ::core::default::Default::default(), skip: ::core::default::Default::default(), } } } 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)] fn private_mut(&mut self) -> &mut T { &mut self.private } /// 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 const fn public(&self) -> T { self.public } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub(crate) fn set_public(&mut self, val: T) -> &mut Self { self.public = val; self } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub(crate) fn public_mut(&mut self) -> &mut T { &mut self.public } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub(self) const fn with_public(mut self, val: T) -> Self { self.public = val; self } } impl Foo { fn private(&self) -> &T { &self.private } fn skip(&self) { self.skip } } 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); foo.set_public(3); assert_eq!(foo.public(), 3); assert_eq!(foo.skip(), ()); } ``` -------------------------------- ### Generate Const Accessor Methods with getset2 Source: https://context7.com/andeya/getset2/llms.txt Shows how to generate accessor methods that are compatible with the `const` keyword using `#[getset2(..., const)]`. This enables compile-time evaluation of getters, useful for constants and configurations. ```rust use getset2::Getset2; #[derive(Getset2)] pub struct Constants { #[getset2(get_copy(pub, const))] max_connections: u32, #[getset2(get_copy(pub, const))] timeout_seconds: u64, } impl Constants { pub const fn new() -> Self { Constants { max_connections: 100, timeout_seconds: 30, } } } const CONFIG: Constants = Constants::new(); fn main() { // Can be used in const contexts const MAX_CONN: u32 = CONFIG.max_connections(); const TIMEOUT: u64 = CONFIG.timeout_seconds(); assert_eq!(MAX_CONN, 100); assert_eq!(TIMEOUT, 30); } ``` -------------------------------- ### Control Visibility of Generated Accessors with getset2 Source: https://context7.com/andeya/getset2/llms.txt Illustrates how to control the visibility of generated accessor methods using attributes like `pub`, `pub(crate)`, `pub(super)`, or `pub(self)`. This allows fine-grained control over which parts of your API are exposed. ```rust use getset2::Getset2; #[derive(Getset2)] pub struct Service { #[getset2(get_ref(pub), set(pub = "crate"))] name: String, #[getset2(get_copy(pub), get_mut(pub = "super"))] port: u16, #[getset2(set_with(pub = "self"))] internal_flag: bool, } fn main() { let mut service = Service { name: "api".to_string(), port: 3000, internal_flag: false, }; // Public getter let name = service.name(); assert_eq!(name, "api"); // Public copy getter let port = service.port(); assert_eq!(port, 3000); // pub(crate) setter (accessible within same crate) service.set_name("web".to_string()); } ``` -------------------------------- ### Rust: Derive Getters and Setters with getset2 Source: https://github.com/andeya/getset2/blob/getset2/README.md This snippet demonstrates how to use the getset2 derive macro in Rust to automatically generate getter and setter methods for struct fields. It shows various attributes like `get_ref`, `set_with`, `set`, `get_mut`, `get_copy`, and `skip` to customize the generated methods' visibility, mutability, and existence. ```rust use getset2::Getset2; #[derive(Default, Getset2)] #[getset2(get_ref, set_with)] pub struct Foo where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[getset2(set, get_mut, skip(get_ref))] private: T, /// Doc comments are supported! /// Multiline, even. #[getset2( get_copy(pub, const), set(pub = "crate"), get_mut(pub = "super"), set_with(pub = "self", const) )] public: T, #[getset2(skip)] skip: (), } impl Foo { fn private(&self) -> &T { &self.private } fn skip(&self) { self.skip } } // cargo expand --example simple 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); foo.set_public(3); assert_eq!(foo.public(), 3); assert_eq!(foo.skip(), ()); } ``` -------------------------------- ### Rust: Builder-Style Setter with #[getset2(set_with)] Source: https://context7.com/andeya/getset2/llms.txt Details the `#[getset2(set_with)]` attribute for creating builder-style setters. These methods consume the struct instance (`self`), modify it, and return the modified instance, facilitating a fluent builder pattern for object construction. ```rust use getset2::Getset2; #[derive(Getset2)] struct Request { #[getset2(set_with(pub), get_ref(pub))] url: String, #[getset2(set_with(pub), get_ref(pub))] method: String, } fn main() { let request = Request { url: String::new(), method: String::new(), }; // Builder-style chaining let request = request .with_url("https://api.example.com".to_string()) .with_method("GET".to_string()); assert_eq!(request.url(), "https://api.example.com"); assert_eq!(request.method(), "GET"); } ``` -------------------------------- ### Rust: Traditional Setter with #[getset2(set)] Source: https://context7.com/andeya/getset2/llms.txt Explains the usage of `#[getset2(set)]` to generate traditional setter methods. These methods typically take a value, assign it to the field, and return a mutable reference to self, enabling method chaining. ```rust use getset2::Getset2; #[derive(Default, Getset2)] struct Settings { #[getset2(set, get_ref)] theme: String, #[getset2(set, get_copy)] volume: u8, } fn main() { let mut settings = Settings::default(); // Setter returns &mut Self for method chaining settings .set_theme("dark".to_string()) .set_volume(75); assert_eq!(settings.theme(), "dark"); assert_eq!(settings.volume(), 75); } ``` -------------------------------- ### Rust: Mutable Reference Getter with #[getset2(get_mut)] Source: https://context7.com/andeya/getset2/llms.txt Demonstrates how to create getter methods that return mutable references (`&mut T`) to struct fields using `#[getset2(get_mut)]`. This allows direct modification of the field's value through the generated accessor. ```rust use getset2::Getset2; #[derive(Getset2)] struct Counter { #[getset2(get_mut)] value: i32, } fn main() { let mut counter = Counter { value: 0 }; // Returns &mut i32 *counter.value_mut() += 5; *counter.value_mut() *= 2; assert_eq!(counter.value, 10); } ``` -------------------------------- ### Rust: Copy-by-Value Getter with #[getset2(get_copy)] Source: https://context7.com/andeya/getset2/llms.txt Illustrates the use of `#[getset2(get_copy)]` to generate getters that return a copy of the field's value. This requires the field type to implement the `Copy` trait and is suitable for primitive types or small, cheap-to-copy data structures. ```rust use getset2::Getset2; #[derive(Getset2)] struct Point { #[getset2(get_copy(pub))] x: f64, #[getset2(get_copy(pub))] y: f64, } fn main() { let point = Point { x: 3.0, y: 4.0 }; // Returns f64 directly (not &f64) let x: f64 = point.x(); let y: f64 = point.y(); assert_eq!(x, 3.0); assert_eq!(y, 4.0); } ``` -------------------------------- ### Skip Fields or Modes with getset2 Source: https://context7.com/andeya/getset2/llms.txt Demonstrates how to use the `#[getset2(skip)]` attribute to prevent the generation of accessor methods for specific fields or modes. This is useful for fields that have custom implementations or should not be directly accessed externally. ```rust use getset2::Getset2; #[derive(Getset2)] #[getset2(get_ref, set)] struct Account { username: String, #[getset2(skip)] // No getters/setters generated internal_id: u64, #[getset2(skip(get_ref))] // Skip only get_ref, keep set password_hash: String, } impl Account { fn internal_id(&self) -> u64 { self.internal_id } } fn main() { let mut account = Account { username: "user123".to_string(), internal_id: 42, password_hash: "hash".to_string(), }; // username has both getter and setter assert_eq!(account.username(), "user123"); account.set_username("newuser".to_string()); // internal_id uses custom implementation assert_eq!(account.internal_id(), 42); // password_hash has setter but no getter account.set_password_hash("newhash".to_string()); } ``` -------------------------------- ### Rust: Immutable Reference Getter with #[getset2(get_ref)] Source: https://context7.com/andeya/getset2/llms.txt Shows how to generate getter methods that return immutable references (`&T`) to struct fields using the `#[getset2(get_ref)]` attribute. This is useful for accessing data without allowing modification through the getter. ```rust use getset2::Getset2; #[derive(Getset2)] #[getset2(get_ref)] struct Config { database_url: String, port: u16, } fn main() { let config = Config { database_url: "localhost:5432".to_string(), port: 8080, }; // Returns &String and &u16 let url: &String = config.database_url(); let port: &u16 = config.port(); assert_eq!(url, "localhost:5432"); assert_eq!(*port, 8080); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.