colin-kiegel / rust-derive-builder

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

Feature request: infallible build() #293

Closed kugland closed 1 year ago

kugland commented 1 year ago

When all fields have defaults and there is no validation function, the build() function is effectively infallible. Is it possible for build() to return Result<T, Infallible> in this case, or just T?

kugland commented 1 year ago

This can actually be done by using #[builder(build_fn(error = "Infallible"))], which the compiler will accept as long as there are no fields without a default value.

VladimirMakaev commented 1 year ago

What would be cool is if it somehow possible to have builder constructor function to enforce an invariant on required fields. E.g. instead of having default() and fallable build() to have new(required_field1, ...) and infallible build as a consequence

I find myself wanting to handroll a builder just for this feature as it tend to pollute the code with error handling where it can be avoided with just enforcing proper invariants on the builder itself

TedDriggs commented 1 year ago

Addressed in #227