Stream: wasmtime

Topic: Have a Store borrow it's state.


view this post on Zulip i509VCB (Sep 27 2023 at 01:52):

I'm trying to figure out how to deal with the fact that a Store owns it's data. With how my application works, I need to make a Store only borrow it's state while using it and then return the state. Also the thing that owns the Store must be 'static, so I can't get away with a lifetime here.

Store::into_data probably isn't the right solution since it would destroy any vm internal state.

I can't move the application state into the Store because I may destroy the current Store and move app state to a new store at runtime.

view this post on Zulip i509VCB (Sep 27 2023 at 01:58):

The Store documentation also seems to mention it should be a short lived object, but in my application the Store may be around for a very long time

view this post on Zulip Lann Martin (Sep 27 2023 at 02:42):

https://doc.rust-lang.org/std/mem/fn.replace.html perhaps?

view this post on Zulip i509VCB (Sep 27 2023 at 02:58):

Lann Martin said:

https://doc.rust-lang.org/std/mem/fn.replace.html perhaps?

I'm not sure mem::replace is going to solve my issue, let me type up something quickly

view this post on Zulip i509VCB (Sep 27 2023 at 03:04):

struct App {
    state: State,
    // most of the time a runtime will exist, but it can be replaced and exist for a short time with no runtime.
    //
    // However T needs to reference stuff in State. This would result in a self referential type.
    //
    //
    runtime: Option<Store<T>>,
}

Ideally I'd prefer to avoid using ouroboros which would make a self referential type and instead or resorting to unsafe

view this post on Zulip i509VCB (Sep 27 2023 at 03:05):

Although I'm not sure ouroboros is even capable of describing a self referential type which may cease being self referential in the future

view this post on Zulip i509VCB (Sep 27 2023 at 04:15):

Actually scratch the code example I wrote, I need something more like:

struct App {
    some_data: i32,
    runtime: Option<Store<App>>, // where App is &mut self pretty much
}

Last updated: Nov 22 2024 at 16:03 UTC