jbaublitz/getset

Generic Setter

Luro02 opened this issue · 4 comments

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

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. :)

I tend to use this patterns a lot too

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

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