bevyengine / bevyengine/bevy

Resource trait API is dysfunctional when the trait is implemented manually

Open
#24,686 3 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Usability D-Modest S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## Bevy version and features

0.19.0, default features (probably just the ECS crate is enough to reproduce)

## What you did

I have naively misused the API for implementing a `Resource` on accident:

```rust
fn main() {
use bevy::prelude::*;

#[derive(Default, Component)]
struct CoolResource {}

impl Resource for CoolResource {}

let mut world = World::new();

dbg!(world.entities().count_spawned());
for _ in 0..500 {
world.insert_resource(CoolResource::default());
}
dbg!(world.entities().count_spawned());

assert!(world.remove_resource::().is_some());
}
```

## What went wrong

The code above compiles just fine with no errors or warnings, then exhibits astonishing behavior at runtime. The loop spawns 500 separate entities, and the assert at the end fails (i.e. `add_resource` adds something but `remove_resource` doesn't).

I feel this warrants either better documentation, or perhaps a runtime check in `World::insert_resource` that checks whether the resource in question requires `IsResource` to prevent the above from happening.

Contributor guide

Open the contributing guide

Research direction

Start with the Resource and IsResource trait definitions, then trace World::insert_resource and World::remove_resource using the reproduction in the issue. Run the example to confirm the entity count and failed removal, then determine whether the agreed fix is documentation or a runtime check. Done means manually implemented resources no longer fail silently, or the supported API and misuse are clearly documented with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.