acfoltzer commented on Issue #11:
I'm so excited about this, thank you for making it happen!
I'm a little unclear about the role of the store's type variable
T
from a library user perspective. I think it would help to have some examples of user code showing how theT
impacts the developer experience. Is it usually going to be inferred or fixed by library code, or will the user have to reason about what this variable should be?
alexcrichton commented on Issue #11:
A good question! It's sort of tangentially touched on here but the general idea is that if you are a library crate then to work with a
Store<T>
you'll probably place some sort of trait bound onT
to get access to the data you'd want. For example with WASI:// in the wasmtime-wasi crate fn add_to_linker<T>(funcs: &mut Linker<'_, T>) where T: AsMut<WasiCtx>;
or alternatively:
// in your crate fn add_to_linker<T>( funcs: &mut Linker<'_, T>, get: impl Fn(&mut T) -> &mut MyLibraryState + Copy + Send + Sync + 'static, );
The idea being that the owner of the store will still determine the
T
for you. Libraries will work generically with someT
with trait bounds or accessors or similar.In terms of application code and such I'd imagine that the
T
is generally inferred from a call toStore::new
for you, given that you're selecting some type to be owned by the store there.
acfoltzer commented on Issue #11:
Hmm, I think I get it. To check my understanding, if I want to have my own application-specific context type but also a WASI context, how might that look? Create my own combined context type that impls both
AsMut<WasiCtx>
andAsMut<MyAppCtx>
?
alexcrichton commented on Issue #11:
@acfoltzer indeed! While it's not the prettiest this is an example in the branch I'm working on. The
wasmtime
CLI optionally supports some WASI proposals both at runtime and compile time, and that struct is used to manage the state where it's dynamically initialized and then used by linker-added functions through theBorrowMut
traits
acfoltzer commented on Issue #11:
Perfect, that makes tons of sense. Thank you!
Last updated: Nov 22 2024 at 16:03 UTC