Add 'WithSetters' proc_macro_derive, change version to 0.2.0
GenMode::SetWith => {
quote! {
#(#doc)*
#[inline(always)]
#visibility fn #fn_name(mut self, val: #ty) -> Self {
self.#field_name = val;
self
}
}
}
fn_name is with_{field_name}
Demonstration of usage scenarios:
#[derive(Debug, getset::WithSetters)]
#[set_with]
struct Config {
timeout: u32,
retries: u8,
address: String,
}
impl Config {
pub fn new() -> Self {
Config { timeout: 30, retries: 3, address: "127.0.0.1".to_string() }
}
}
// Generate by getset::WithSetters:
impl Config {
pub fn with_timeout(mut self, timeout: u32) -> Self {
self.timeout = timeout;
self
}
pub fn with_retries(mut self, retries: u8) -> Self {
self.retries = retries;
self
}
pub fn with_address(mut self, address: String) -> Self {
self.address = address;
self
}
}
fn main() {
let config = Config::new()
.with_timeout(60)
.with_retries(5)
.with_address("192.168.1.1".to_string());
println!("{:?}", config); // Outupt: Config { timeout: 60, retries: 5, address: "192.168.1.1" }
}
Hi @andeya. As you might know, I took over this crate a few years ago and haven't had much time to work on it. I'm trying to catch up on the PRs and issues at the moment and should have time to review this tomorrow. Just so I understand, is this aiming to do the same thing as #84?
@jbaublitz Yes, it is the same requirement, but the implementation is more in line with Rust specifications and more complete and reliable than that of #84.
@jbaublitz After waiting for a long time without result, I have independently published it to getset2. cc https://github.com/jbaublitz/getset/pull/84
@jbaublitz
Hey, any chance this PR can get some love?
There has been a few attempts at this in the past (https://github.com/jbaublitz/getset/pull/87, https://github.com/jbaublitz/getset/pull/84), but I think this PR has the most idiomatic implementation.
@musjj I have finally been catching up on a backlog of issues over on my other crate I maintain, neli. I just got a release out for that containing some bug fixes that I've been sitting on for a while, so I'm going to shift my focus to getset and try to make my way backwards through the pull requests and then the issues.
@andeya I tried to run cargo semver-checks on this crate, but apparently it doesn't work with proc-macro crates. I believe that this is just a feature, so I'm going to release it as 0.1.4. Please let me know if you have any objections.
Superseded by #102