bevyengine / bevyengine/bevy

Safe version of `BundleScratch`/`BundleWriter`

Open
#25,316 0 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Feature D-Unsafe S-Ready-For-Implementation
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?

There are times when you want to conditionally/dynamically insert components based on runtime values. Typically this would require you to use `Commands::insert`, which would trigger an archetype move. This is usually bad for performance.

[`BundleScratch`](https://docs.rs/bevy/latest/bevy/ecs/bundle/struct.BundleScratch.html) and [`BundleWriter`](https://docs.rs/bevy/latest/bevy/ecs/bundle/struct.BundleWriter.html) solves this problem by letting you push components into a temporary scratch space, before applying them to an entity [all at once](https://github.com/bevyengine/bevy/blob/7b2b90997ae2e73558ace09cc065d31a2c24b266/crates/bevy_ecs/src/bundle/writer.rs#L186-L204).

But unfortunately, they both currently require `unsafe` to operate. It should be possible to automatically uphold the invariants that these types demand via an abstraction.

## What solution would you like?

Provide a safe abstraction for `BundleScratch` and `BundleWriter`, so that users could benefit from them without using `unsafe`.

## What alternative(s) have you considered?

Continue using `unsafe` in order to use `BundleScratch` and `BundleWriter`.

Contributor guide

Open the contributing guide

Research direction

Start with the BundleScratch and BundleWriter documentation and the linked crates/bevy_ecs/src/bundle/writer.rs implementation. Study the invariants that currently require unsafe and determine the abstraction's safe API boundary. Done means users can benefit from these types without using unsafe.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.