#[derive(Data)]
{
// Attributes available to this derive:
#[get]
#[get_mut]
#[set]
}
Expand description
A procedural macro that combines getter, mutable getter, and setter functionality in a single derive.
This macro derives all three types of accessor methods (getters, mutable getters, and setters) for struct and enum fields, providing comprehensive data manipulation capabilities with configurable visibility and behavior options.
§Supported Attributes
#[get(...)]- Controls getter generation (supportsreference,clone,copy,derefoptions)#[get_mut(...)]- Controls mutable getter generation#[set(...)]- Controls setter generation (supports parameter type conversion withtype(AsRef<T>),Into, etc.)
§Visibility Control
Each attribute supports the same visibility options:
pub- Public accesspub(crate)- Crate-level accesspub(super)- Parent module accessprivate- Private access
§Examples
§Basic Combination
use lombok_macros::*;
#[derive(Data, Debug, Clone)]
struct User {
#[get(pub)]
#[set(pub)]
name: String,
#[get(pub, type(clone))]
#[set(pub)]
email: String,
#[get(pub, type(copy))]
age: u32,
#[get_mut(pub)]
mutable_age: u32,
}
let mut user = User {
name: "Alice".to_string(),
email: "alice@ltpp.vip".to_string(),
age: 30,
mutable_age: 25,
};
let name_reference: &String = user.get_name();
let email_clone: String = user.get_email();
let age_copy: u32 = user.get_age();
assert_eq!(*name_reference, "Alice");
assert_eq!(email_clone, "alice@ltpp.vip");
assert_eq!(age_copy, 30);
user.set_name("Bob".to_string());
user.set_email("bob@ltpp.vip".to_string());
let updated_email: String = user.get_email();
assert_eq!(updated_email, "bob@ltpp.vip");
(*user.get_mut_mutable_age() = 31);
assert_eq!(*user.get_mutable_age(), 31);§Multiple Field Types
use lombok_macros::*;
#[derive(Data, Debug, Clone)]
struct ComplexStruct {
#[get(pub)]
id: i32,
#[get(pub)]
#[set(pub)]
optional: Option<String>,
#[get(pub, type(reference))]
result: Result<i32, String>,
#[get(pub(crate))]
#[set(private)]
internal_data: Vec<u8>,
}
let mut complex = ComplexStruct {
id: 1,
optional: Some("value".to_string()),
result: Ok(42),
internal_data: vec![1, 2, 3],
};
let id_reference: &i32 = complex.get_id();
let optional_clone: String = complex.get_optional();
let result_reference: &Result<i32, String> = complex.get_result();
assert_eq!(*id_reference, 1);
assert_eq!(optional_clone, "value");
assert_eq!(*result_reference, Ok(42));§Tuple Struct with Combined Accessors
use lombok_macros::*;
#[derive(Data, Debug, Clone)]
struct Point(
#[get(pub)] f64,
#[get(pub, type(clone))]
#[set(pub)] f64,
);
let mut point = Point(1.0, 2.0);
let x_coordinate: &f64 = point.get_0();
let y_coordinate: f64 = point.get_1();
assert_eq!(*x_coordinate, 1.0);
assert_eq!(y_coordinate, 2.0);
point.set_1(3.0);
let updated_y_coordinate: f64 = point.get_1();
assert_eq!(updated_y_coordinate, 3.0);