colin-kiegel / rust-derive-builder

derive builder implementation for rust structs
https://colin-kiegel.github.io/rust-derive-builder/
Apache License 2.0
1.28k stars 82 forks source link

Add conditional setters for options #322

Closed RustyNova016 closed 1 month ago

RustyNova016 commented 3 months ago

A thing missing from the crate is the ability to conditionally insert a value if the Option containing it if it is Some. This would reduce the amount of boilerplate when mapping a struct to another.

For exemple, here is the current way to do it:

let mut config_builder = RadioConfigBuilder::default();

// self.minimum_count has value Option<u64>
// But we need to conditionally set the value ourselves.
if let Some(val) = self.minimum_count { 
    config_builder.min_count(val); 
}

config_builder.build()

A better way would be:

let mut config_builder = RadioConfigBuilder::default();

// The builder automatically set the value conditionally, allowing us to remove the boilerplate
config_builder.min_count_optional(self.minimum_count);

config_builder.build()
TedDriggs commented 2 months ago

I'm not convinced that this is worth the added complexity; as you demonstrated, there is a syntax for this already in the language, and we'd need to think about how it interacted with all the other kinds of setters before introducing it.