Skip to content

Instantly share code, notes, and snippets.

@tuzz
Last active August 6, 2020 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuzz/3e7795974757acb9f329ce16f36c4753 to your computer and use it in GitHub Desktop.
Save tuzz/3e7795974757acb9f329ce16f36c4753 to your computer and use it in GitHub Desktop.
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
legion = "0.2.4"
use legion::prelude::*;
use std::iter::once;
struct Foo;
fn main() {
let mut world = Universe::new().create_world();
let entity = world.insert((), once((Foo,)))[0];
if let Some(_foo) = world.get_component_mut::<Foo>(entity) {
} else {
world.add_component(entity, Foo).unwrap();
}
}
@tuzz
Copy link
Author

tuzz commented Aug 6, 2020

Results in the following:

error[E0597]: `world` does not live long enough
  --> src/main.rs:10:25
   |
10 |     if let Some(_foo) = world.get_component_mut::<Foo>(entity) {
   |                         ^^^^^---------------------------------
   |                         |
   |                         borrowed value does not live long enough
   |                         a temporary with access to the borrow is created here ...
...
15 | }
   | -
   | |
   | `world` dropped here while still borrowed
   | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::option::Option<legion::borrow::RefMut<'_, Foo>>`
   |
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
   |
14 |     };
   |      ^

error[E0499]: cannot borrow `world` as mutable more than once at a time
  --> src/main.rs:13:9
   |
10 |     if let Some(_foo) = world.get_component_mut::<Foo>(entity) {
   |                         --------------------------------------
   |                         |
   |                         first mutable borrow occurs here
   |                         a temporary with access to the first borrow is created here ...
...
13 |         world.add_component(entity, Foo).unwrap();
   |         ^^^^^ second mutable borrow occurs here
14 |     }
15 | }
   | - ... and the first borrow might be used here, when that temporary is dropped and runs the destructor for type `std::option::Option<legion::borrow::RefMut<'_, Foo>>`
   |
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
   |
14 |     };
   |      ^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0499, E0597.
For more information about an error, try `rustc --explain E0499`.

@tuzz
Copy link
Author

tuzz commented Aug 6, 2020

When the semi-colon is added, results in the following:

error[E0499]: cannot borrow `world` as mutable more than once at a time
  --> src/main.rs:13:9
   |
10 |     if let Some(_foo) = world.get_component_mut::<Foo>(entity) {
   |                         --------------------------------------
   |                         |
   |                         first mutable borrow occurs here
   |                         a temporary with access to the first borrow is created here ...
...
13 |         world.add_component(entity, Foo).unwrap();
   |         ^^^^^ second mutable borrow occurs here
14 |     };
   |      - ... and the first borrow might be used here, when that temporary is dropped and runs the destructor for type `std::option::Option<legion::borrow::RefMut<'_, Foo>>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0499`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment