colin-kiegel / rust-derive-builder

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

Support for infaliable builders #267

Closed sazzer closed 2 years ago

sazzer commented 2 years ago

In some cases, it's possible that the build() method actually can't fail. For example, if there are no mandatory fields - that is, every field has a default value - and no validation, and maybe some other conditions, then the build method will never fail. However, at present the client code still has to handle this case and check the response for errors.

It would be useful if there was the ability to produce a build() method that didn't return a Result<> in this case, and instead just returns the built object directly.

This also has the subtle advantage that it becomes a compilation error if the struct changes in such a way that the build method can fail - which would indicate to the callers that they now need to pay attention to the errors after all.

TedDriggs commented 2 years ago

This has been discussed at length in #56, and the current solution is provided in #227.

The current solution does use an internal unwrap - it might be possible to use ! as the error type to prove the unwrap is infallible at compile-time, but I haven't tried it personally.