getset
getset copied to clipboard
Fluent Setters
I would like to derive fluent, or 'builder' style API
for my use-case I would prefer settings returning Self, but for other use-cases returning &mut Self is probably more appropriate. ideally both would be supported.
This seems to be a feature request, you're welcome to propose a syntax and work on a feature, but I don't use this crate, so it's unlikely I'm going to implement this.
I'd live to have that feature too. My idea would be this: A normal setter generates this for example:
#[inline(always)]
pub fn set_public(&mut self, val: T) -> &mut Self {
self.public = val;
self
}
where I'd love to use:
#[inline(always)]
pub fn with_public(mut self, val: T) -> Self {
self.public = val;
self
}
so that I can do
let mut thing = Thing::new()
.with_public(value)
.with_another(123);
So the the differences are the mut self instead of &mut self and the return of Self instead of &mut Self.
There is a pull request #84 which implements this, but I'd be unhappy with the naming. I'd prefer with_ prefix instead of set_ or set_fluent_ (as far as i understand the diff) as this will be short and nobody might trip over the copy of self.
This change will add a great value for getset and will ensure users will write shorter, more readable code when creating structs and builders.