Bad error when AssetServer::load is called on &str from event reader (possibly flawed lifetimes?)
- 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
Assessment
This issue has not been assessed yet.