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

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.

2 participants