What problem does this solve or what need does it fill?
#[derive(FromTemplate)] currently requires all fields to implement FromTemplate. However, many user types do not have a meaningful Default and therefore FromTemplate implementation. To use the type in bsn! macros the user can do the following
- wrap the
Component with template(move |_| Ok(MyComponent {}))
- implement a nonsense or sentinel
Default value (e.g. Entity::PLACEHOLDER) that they need to override
- hand write a companion
Template / FromTemplate type (i.e. MyComponentTemplate)
None of these options are ideal, considering (1) is rather ugly, (2) can result in runtime logic errors, and (3) is boilerplate that could be handled by a macro (this proposal).
What solution would you like?
An opt-in field attribute that re-uses the current #[template(...)] namespace.
#[derive(Component, Clone, FromTemplate)]
struct Weapon {
#[template(required)]
damage: DamageRange,
crit_chance: f32,
}
Which is then used like
bsn! {
Weapon {
damage: DamageRange {low: 10.0, high: 12.0 }
// crit_chance implements `FromTemplate`, so it is optional
}
}
This gives you the regular patching behaviour letting you leave off Default fields.
What alternative(s) have you considered?
It's possible to hand write a companion type with Option wrapping non FromTemplate fields, and manually implement Template.
Additional context
This is the largest issue I'm running into transitioning to Scene from Bundle for my UI work so far. The manual companion type implementations work fine but this would be more convenient, and may help for other things.
I think this should be a pretty small addition to add to the bsn! macro. Happy to work on it myself if this seems like something worth doing.
What problem does this solve or what need does it fill?
#[derive(FromTemplate)]currently requires all fields to implementFromTemplate. However, many user types do not have a meaningfulDefaultand thereforeFromTemplateimplementation. To use the type inbsn!macros the user can do the followingComponentwithtemplate(move |_| Ok(MyComponent {}))Defaultvalue (e.g.Entity::PLACEHOLDER) that they need to overrideTemplate/FromTemplatetype (i.e.MyComponentTemplate)None of these options are ideal, considering (1) is rather ugly, (2) can result in runtime logic errors, and (3) is boilerplate that could be handled by a macro (this proposal).
What solution would you like?
An opt-in field attribute that re-uses the current
#[template(...)]namespace.Which is then used like
This gives you the regular patching behaviour letting you leave off
Defaultfields.What alternative(s) have you considered?
It's possible to hand write a companion type with
Optionwrapping nonFromTemplatefields, and manually implementTemplate.Additional context
This is the largest issue I'm running into transitioning to
ScenefromBundlefor my UI work so far. The manual companion type implementations work fine but this would be more convenient, and may help for other things.I think this should be a pretty small addition to add to the
bsn!macro. Happy to work on it myself if this seems like something worth doing.