bevyengine / bevyengine/bevy

Bad error when AssetServer::load is called on &str from event reader (possibly flawed lifetimes?)

Open
#14,080 6 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Usability S-Blocked S-Needs-Investigation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## Bevy version

Bevy 0.13

## What you did

Used a EventReader which includes a String, and passed a &str based on this to AssetServer::load

This is a minimal reproduction.
```rs
use bevy::prelude::*;
fn main() {}
#[derive(Event)]
struct LoadAsset(String);

fn load_asset(mut events: EventReader, assetserver: Res) {
for i in events.read() {
let _sound: Handle = assetserver.load(i.0.as_str());
}
}
```

## What went wrong

The resulting error message was very unclear, pointing at the events.read() call as "`events` escapes the function body here" which is highly confusing when it happens.

```
error[E0597]: `events` does not live long enough
--> examples\t.rs:7:14
|
6 | fn load_asset(mut events: EventReader, assetserver: Res) {
| ---------- binding `events` declared here
7 | for i in events.read() {
| ^^^^^^-------
| |
| borrowed value does not live long enough
| argument requires that `events` is borrowed for `'static`
...
10 | }
| - `events` dropped here while still borrowed
```

## Additional information

This error was not encountered directly by me, but someone else I was helping with bevy. They're pretty new to Rust which is why cloning the String instead was not immediately obvious, causing some headaches.

I believe the fact that this error message is this wrong, could point towards some badly specified or unclear lifetimes in AssetServer::load or EvenrReader::read. I was unable to reproduce a similar error with only Rust stdlib, but I may be wrong here.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.