No more worrying whether the build
call on your builder will return Ok
or not. Maybe you forgot to set a field? typesafe-builders
solves this by using the Rust type-system to ensure correct usage.
Example
use *;
Known Downside
I can recommend this only for internal use. It is best to not expose these builder types as an API of your crate, since they look extremely ugly and verbose. For example:
use *;
// Ugly type name here... and it only gets worse for const-generics etc.
Please open an MR/Issue if you know how to improve this.
Field Attributes
Attributes can be combined. Ones that do not work together will throw an explicit error at compile time. Duplicates always error.
Optional
A field can be set, but does not have to be. Requires the field type to be Default
.
use *;
Constructor
Require a field to be set upon builder construction.
use *;
Decay
Decay the type to its first generic. Eases use for Option
, Box
etc. Requires that the decayed type can be into
ed its original. Works on all types with one generic arg.
use *;
How does it work?
Const generic one-hot bitfields. What you get is similar to this:
// The build function is only available once all fields are set:
More Examples
Lifetimes
They work as expected
use *;
Generics
Works as expected, but does not yet support defaults.
Const Generics
Works as expected, but does not yet support defaults.
TODOs
- Lifetimes
- Generics
- Bounds
- With default
- Const generics
- With default
- Add
optional
fields. - Add
rename
field attribute. - Add
constructor
or something like this to have mandatory args directly in thebuilder
function. - Add
Into
or whatever to cast types. - Add way to pass options as
Some
automatically. - Cleanup