Skip to content

#[template(built_in)]#23699

Open
cart wants to merge 4 commits intobevyengine:mainfrom
cart:built_in_templates
Open

#[template(built_in)]#23699
cart wants to merge 4 commits intobevyengine:mainfrom
cart:built_in_templates

Conversation

@cart
Copy link
Copy Markdown
Member

@cart cart commented Apr 7, 2026

Objective

Built in collection types like Option<T> and Vec<T> use the blanket FromTemplate impl for types that implement Default + Clone. This works in the common case where T is a normal Default + Clone type. This breaks down when T has custom template logic (ex: it is something like Handle<T>, which manually implements Template/FromTemplate, or it is something that derives FromTemplate). These cases currently require a solid chunk of boilerplate and knowledge about what is happening under the hood:

#[derive(Component, FromTemplate)]
struct Widget {
  #[template(OptionTemplate<HandleTemplate<Image>>)]
  image: Option<Handle<Image>>
}

Solution

This adds the BuiltInTemplate trait, which serves essentially the same function as FromTemplate, but does not have a blanket implementation for Default + Clone types. It requires a manual implementation.

This PR adds BuiltInTemplate implementations for Option and Vec.

When deriving FromTemplate, fields can opt in to using BuiltInTemplate instead of FromTemplate like this:

#[derive(Component, FromTemplate)]
struct Widget {
  #[template(built_in)]
  image: Option<Handle<Image>>
}

Of course ideally image: Option<Handle<Image>> would "just work". What we're seeing here is the "price" of transparently supporting any type that implements Default + Clone in BSN. That is a big win, and I consider the papercut in this case to be "worth it".

@cart cart added this to the 0.19 milestone Apr 7, 2026
@cart cart added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Scenes Composing and serializing ECS objects labels Apr 7, 2026
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this solution but it needs more docs. I left suggestions about where to put them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Scenes Composing and serializing ECS objects C-Usability A targeted quality-of-life change that makes Bevy easier to use

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants