Skip to content

How to use FnMut with Func::wrap? #932

Answered by Robbepop
orsinium asked this question in Q&A
Discussion options

You must be logged in to vote

Use cases like these are the reason why the Store is generic over a so-called HostState type and while host functions as created with Func::wrap are not FnMut and thus cannot change their own state, they can change the host state if they accept a first parameter as Caller which works very similar to Wasmtime's API: https://docs.rs/wasmtime/17.0.1/wasmtime/struct.Func.html#method.wrap

So you need to define your own host state type that includes whatever you want to mutate in host functions:

pub struct HostState {
    pub pybadge: PyBadge,
}

and

#[entry]
fn main() -> ! {
    let mut pybadge = PyBadge::take().unwrap();
    pybadge.display.clear(Color::RED).unwrap();
    let style = MonoTextS…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@orsinium
Comment options

Answer selected by orsinium
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants