macro_rules! setters { (@single $(#[$meta:meta])* $name:ident : $typ:ty => $transform:expr) => { ... }; (@recurse) => { ... }; (@recurse $(#[$meta:meta])* $name:ident : $typ:ty, $($tokens:tt)*) => { ... }; (@recurse $(#[$meta:meta])* $name:ident : $typ:ty => $transform:expr, $($tokens:tt)*) => { ... }; ($($tokens:tt)*) => { ... }; }
Expand description
Creates setter methods
The methods created are of the form $name
that takes an argument of type $typ
and sets the field $name to result of calling $transform
with the value of the argument.
In other words. The following macro call:
struct MyStruct<'a> { foo: Option<&'a str> };
impl <'a> MyStruct<'a> {
setters! { foo: &'a str => Some(foo), }
}
Roughly expands to:
struct MyStruct<'a> { foo: Option<&'a str> };
impl <'a> MyStruct<'a> {
fn foo(self, foo: &'a str) -> Self {
Self {
foo: Some(foo),
..self
}
}
}