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

Generic Setter #30

Open
Luro02 opened this issue Sep 8, 2019 · 4 comments
Open

Generic Setter #30

Luro02 opened this issue Sep 8, 2019 · 4 comments
Labels

Comments

@Luro02
Copy link

Luro02 commented Sep 8, 2019

For example

#[set = "pub"]
struct Hello {
    message: String
}

would generate

impl Hello {
    pub fn set_message(&mut self, value: String) -> &mut Self {
        self.value = value;
        self
    }
}

It'd be nice to have something like this generated instead

impl Hello {
    pub fn set_message<T: Into<String>>(&mut self, value: T) -> &mut Self {
        self.value = value.into();
        self
    }
}

this would allow the user to set more, than just String like &str, &String or Cow<'_, str>.

Maybe this could be enable by adding #[setter(into)] to the struct

#[setter(pub, into)]
struct Hello {
    message: String
}

For reference: derive_builder

@Hoverbear
Copy link
Collaborator

Hoverbear commented Sep 19, 2019

This is very interesting!

I do worry this kind of API is outside the scope of this library (this focuses on trivial getters and setters, and these kinds of calls would introduce implicit cloning). These kinds of calls have real performance implications.

That said, if this could be done unobtrusively (without breaking changes) in a way that was easy to understand, I'd be willing to accept a PR. :)

@tekjar
Copy link

tekjar commented Dec 8, 2019

I tend to use this patterns a lot too

@Hoverbear
Copy link
Collaborator

#49 may require us to rethink the proposed syntax here!

@Luro02
Copy link
Author

Luro02 commented Jan 29, 2020

Maybe you can take some inspiration from here https://github.com/Luro02/shorthand

My syntax looks like this

#[shorthand(enable(into))]
// or
#[shorthand(disable(into))]

https://docs.rs/shorthand/0.1.0/shorthand/derive.ShortHand.html#into

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

No branches or pull requests

3 participants