You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to mark all Option fields as default?
I have a struct with about 30 optional fields and just 2 non-optional fields (I'm parsing/creating a specific type of xml file), and I'm not even talking about the nested structs.
I wanted my code to look cleaner, so I thought deriving some sort of builder would help me. But needing to annotate every field with #[builder(default)] seems like a lot of boilerplate.
The text was updated successfully, but these errors were encountered:
I understand your usecase, but I don't even do auto-Option-detection for strip_options. Trying to give special treatment to a generic type using syntax detection alone is a can of worms I with to avoid...
In your case, I'd just do something like this:
use typed_builder::TypedBuilder;#[derive(TypedBuilder,Debug)]#[builder(field_defaults(default))]structFoo{#[builder(!default)]field1:u32,#[builder(!default)]field2:u32,field3:Option<u32>,field4:Option<u32>,field5:Option<u32>,field6:Option<u32>,field7:Option<u32>,field8:Option<u32>,field9:Option<u32>,field10:Option<u32>,}fnmain(){println!("{:#?}", Foo::builder().field1(1).field2(2).build());// These will fail to compile:println!("{:?}", Foo::builder().field1(1).build());println!("{:?}", Foo::builder().field2(2).build());}
Is there a way to mark all Option fields as default?
I have a struct with about 30 optional fields and just 2 non-optional fields (I'm parsing/creating a specific type of xml file), and I'm not even talking about the nested structs.
I wanted my code to look cleaner, so I thought deriving some sort of builder would help me. But needing to annotate every field with
#[builder(default)]
seems like a lot of boilerplate.The text was updated successfully, but these errors were encountered: