Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'WithSetters' proc_macro_derive, change version to 0.2.0 #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andeya
Copy link

@andeya andeya commented Sep 14, 2024

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" }
}

@jbaublitz
Copy link
Owner

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?

@andeya
Copy link
Author

andeya commented Sep 15, 2024

@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.

@andeya
Copy link
Author

andeya commented Sep 20, 2024

@jbaublitz After waiting for a long time without result, I have independently published it to getset2.
cc #84

@musjj
Copy link

musjj commented Jan 15, 2025

@jbaublitz

Hey, any chance this PR can get some love?

There has been a few attempts at this in the past (#87, #84), but I think this PR has the most idiomatic implementation.

@jbaublitz
Copy link
Owner

@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.

Copy link
Owner

@jbaublitz jbaublitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed the code and just have one comment. Other than that, this looks good and I'd be willing to merge and put out a new release.

@@ -5,8 +5,11 @@ Getset, we're ready to go!
A procedural macro for generating the most basic getters and setters on fields.
"""
version = "0.1.3"
authors = ["Ana Hobden <ana@hoverbear.org>", "John Baublitz <john.m.baublitz@gmail.com"]
version = "0.2.0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a breaking change that I'm missing? I believe this is an added feature which, pre-1.0.0, does not need a minor version bump.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants