ref.rs
Overview
This file defines multiple Rust structs demonstrating the use of the derive_setters crate to automatically generate setter methods with various configurations. The primary focus is on illustrating how setters can be customized with reference borrowing semantics, renaming, and delegation patterns for fields and methods. The code includes unit tests validating the behavior of these setters.
The file showcases:
Basic setter generation with borrowing control.
Field-specific borrow setter behavior.
Delegation of setters to inner structs via fields or methods.
These patterns help simplify mutable builder-style APIs for complex data structures, enabling ergonomic and flexible mutation.
Structs and Their Functionality
BasicRefStruct
Purpose: A struct with four
u32fields demonstrating different setter borrow configurations and renaming.Derives:
Default,Setters,Debug,PartialEq,Eq.Attributes:
#[setters(borrow_self)]: Default setter methods take&mut self.Field-specific overrides:
a: Setter renamed totest.c: Setter takesselfby value (borrow_self = falseas string).d: Setter takesselfby value (borrow_self = falseas bool).
Fields
Field | Type | Setter Behavior | Setter Name |
|---|---|---|---|
a | u32 |
|
|
b | u32 |
|
|
c | u32 | Owned |
|
d | u32 | Owned |
|
Example Usage
let mut s = BasicRefStruct::default().c(34).d(4);
s.test(1);
s.b(3);
assert_eq!(s.a, 1);
assert_eq!(s.b, 3);
assert_eq!(s.c, 34);
assert_eq!(s.d, 4);
FieldRefStruct
Purpose: Demonstrates field-level control of setter borrowing.
Derives:
Default,Setters,Debug,PartialEq,Eq.Attributes: No struct-level borrow_self; field
bhas#[setters(borrow_self)].
Fields
Field | Type | Setter Behavior |
|---|---|---|
a | u32 | Default owned setter (takes |
b | u32 | Borrowed setter (takes |
Example Usage
let mut s = FieldRefStruct::default().a(10);
s.b(20);
assert_eq!(s.a, 10);
assert_eq!(s.b, 20);
InnerRefDelegateStruct
Purpose: Inner struct with setters generated for delegation.
Derives:
Default,Setters,Debug,PartialEq,Eq.Attributes:
#[setters(borrow_self)]: Setter methods take&mut self.Two delegation attributes:
generate_delegates(ty = "BasicRefDelegateField", field = "x"): Generates setters inBasicRefDelegateFielddelegating to fieldx.generate_delegates(ty = "BasicRefDelegateMethod", method = "get_x"): Generates setters inBasicRefDelegateMethoddelegating to methodget_x.
Field
asetter renamed totest.
Fields
Field | Type | Setter Behavior | Setter Name |
|---|---|---|---|
a | u32 |
|
|
b | u32 |
|
|
c | u32 |
|
|
BasicRefDelegateField
Purpose: Struct with a field
xof typeInnerRefDelegateStructto which setter calls are delegated.Derives:
Default,Debug,PartialEq,Eq.Interaction: Uses generated delegate setters from
InnerRefDelegateStruct.
Fields
Field | Type |
|---|---|
x |
|
Example Usage
let mut s = BasicRefDelegateField::default();
s.test(1);
s.b(3);
s.c(34);
assert_eq!(s.x, InnerRefDelegateStruct { a: 1, b: 3, c: 34 });
BasicRefDelegateMethod
Purpose: Struct with optional field
xand a methodget_xto provide mutable access toInnerRefDelegateStruct.Derives:
Default,Debug,PartialEq,Eq.Method:
get_x()initializesxifNoneand returns mutable reference.Interaction: Setters delegate to
get_x()method of this struct.
Methods
fn get_x(&mut self) -> &mut InnerRefDelegateStructInitializes
xwith default if empty.Returns mutable reference to
x.
Fields
Field | Type |
|---|---|
x |
|
Example Usage
let mut s = BasicRefDelegateMethod::default();
s.test(1);
s.b(3);
s.c(34);
assert_eq!(s.x, Some(InnerRefDelegateStruct { a: 1, b: 3, c: 34 }));
Important Implementation Details
The
derive_setterscrate is used to generate setter methods automatically, reducing boilerplate.The
borrow_selfattribute controls whether setters take&mut self(borrowed) orself(owned). This affects method chaining and mutation semantics.Setter renaming allows customizing method names, e.g.,
afield's setter is renamed totest.Delegation attributes enable setters to forward calls to embedded fields or methods, facilitating encapsulation and reuse.
Delegation via field forwards setter calls directly to the field of the specified type.
Delegation via method calls the method to obtain a mutable reference, then performs the setter.
The tests verify that setters mutate the correct fields and respect borrowing semantics.
Interactions with Other Parts of the System
This file primarily operates standalone to demonstrate setter generation capabilities.
It may be part of a larger system where builder-style APIs or mutable configuration structs are used, referencing setter customization techniques.
The use of delegation patterns suggests integration with complex data models needing nested mutability.
The
derive_setterscrate is a dependency that provides the procedural macros for setter generation, integral to this file's functionality.
Visual Diagram
classDiagram
class BasicRefStruct {
+a: u32
+b: u32
+c: u32
+d: u32
+test()
+b()
+c()
+d()
}
class FieldRefStruct {
+a: u32
+b: u32
+a()
+b()
}
class InnerRefDelegateStruct {
+a: u32
+b: u32
+c: u32
+test()
+b()
+c()
}
class BasicRefDelegateField {
+x: InnerRefDelegateStruct
+test()
+b()
+c()
}
class BasicRefDelegateMethod {
+x: Option~InnerRefDelegateStruct~
+get_x()
+test()
+b()
+c()
}
BasicRefDelegateField --> InnerRefDelegateStruct : delegates setters
BasicRefDelegateMethod --> InnerRefDelegateStruct : delegates setters via get_x()
This diagram illustrates struct relationships and setter delegation patterns implemented in this file.