A spawn trait for Commands and ChildBuilder
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## What problem does this solve or what need does it fill?
Both `Commands` and `ChildBuilder` share the methods `spawn` and `spawn_bundle`.
In my project I create a `UiExt` that add methods to spawn some ui related structure of entity (e.g. for scrolling list) that look like this :
```rust
pub trait UiExt<'w, 's> {
fn spawn<'a>(&'a mut self) -> EntityCommands<'w, 's, 'a>;
fn spawn_scroll_list<'a>(
&'a mut self,
style: Style,
spawn_items: impl FnOnce(&mut ChildBuilder),
) -> EntityCommands<'w, 's, 'a> {
let mut entity_commands = self.spawn();
scroll_list::build_scroll_list(&mut entity_commands, style, spawn_items);
entity_commands
}
}
impl<'w, 's> UiExt<'w, 's> for Commands<'w, 's> {
fn spawn<'a>(&'a mut self) -> EntityCommands<'w, 's, 'a> {
self.spawn()
}
}
impl<'w, 's, 'a> UiExt<'w, 's> for ChildBuilder<'w, 's, 'a> {
fn spawn<'b>(&'b mut self) -> EntityCommands<'w, 's, 'b> {
self.spawn()
}
}
```
The methods are the same whatever if self is `Commands` or `ChildBuilder` as long as I can get a `EntityCommands`.
But because `spawn` is not implemented via a trait I cannot simply use a blanket implementation.
## What solution would you like?
Expose `spawn` via a trait for `Commands` and `ChildBuilder`.
Allowing blanket implementation:
```rust
impl<'w, 's, T> UiExt<'w, 's> for T
where
T : SpawnEntityCommands<'w, 's>
{
fn spawn_scroll_list<'a>(
&'a mut self,
style: Style,
spawn_items: impl FnOnce(&mut ChildBuilder),
) -> EntityCommands<'w, 's, 'a> {
let mut entity_commands = self.spawn();
scroll_list::build_scroll_list(&mut entity_commands, style, spawn_items);
entity_commands
}
}
```
## What alternative(s) have you considered?
Add a `spawn` method in my `UiExt` trait and manually implement it for `Commands` and `ChildBuilder`.
Contributor guide
Research direction
Start by locating the Commands::spawn and ChildBuilder::spawn entry points and compare their shared return type and lifetimes. Determine where a common spawn trait belongs and how existing API tests cover these methods. Done means both types expose the trait-based spawn behavior and the motivating blanket UiExt implementation can use it without duplicating spawn methods.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- game-dev
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100