macro_rules! setter {
($name:ident, $type:ty) => { ... };
($name:ident, into $type:ty) => { ... };
}
Expand description
A macro for generating builder-style setter methods.
This macro provides two variants:
- Direct assignment:
setter!(field_name, FieldType)
- Automatic Into conversion:
setter!(field_name, into TargetType)
ยงExamples
struct Builder {
name: Option<String>,
count: Option<u32>,
}
impl Builder {
setter!(name, into String); // Accepts any type that implements Into<String>
setter!(count, u32); // Only accepts u32
pub fn build(self) -> Self {
self
}
}
let builder = Builder {
name: None,
count: None,
}
.name("test") // &str automatically converted to String
.count(42); // Direct u32 assignment