#Storing the World reference from an exclusive sytem temporarily in another struct

8 messages · Page 1 of 1 (latest)

untold spade
#

I'd like to do something like the following, but the compiler keeps complaining about the lifetimes, as if the World is kept in the Container after the function completes. Does anyone know how you could do it? Might be a generic rust question, but I've tried to replicate it without bevy and that seemed to work fine.

use bevy::prelude::*;

fn main() {
    App::new()
        .insert_resource(Container::default())
        .add_systems(Update, exclusive)
        .run();
}

fn exclusive(world: &mut World) {
    let mut container = world.remove_resource::<Container>().unwrap();
    container.world = Some(world);
    let world = container.world.take().unwrap();
    world.insert_resource(container);
}

#[derive(Resource, Default)]
struct Container<'w> {
    world: Option<&'w mut World>,
}
earnest lintel
untold spade
#

It's for running plugins in Wasmtime that need access to the world for imported functions. Specifc implementation here https://dpaste.org/qRgWR

earnest lintel
#

I don't know or understand much about that sadly, I hope someone else can help

untold spade
#

Storing the World reference from an exclusive sytem temporarily in another struct

halcyon halo
#

Hmmmmm that's a weird one. I'm tempted to say it's not currently possible without unsafe, but might be in the future with the new borrow checker.

#

In the meantime, you might just have to stick a *mut World in there 😦

untold spade