Skip to content

Commit 8f01d4b

Browse files
authored
Clarify the purpose of closures in WASIp1 add_to_linker (#10078)
This is an attempt at resolving #9723 by clarifying in the documentation how the `f` closure is used, notably it's invoked for every single WASI function call. Closes #9723
1 parent 636435f commit 8f01d4b

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

crates/wasi/src/preview1.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,16 @@ enum FdWrite {
673673
/// Adds asynchronous versions of all WASIp1 functions to the
674674
/// [`wasmtime::Linker`] provided.
675675
///
676-
/// This method will add WASIp1 functions to `linker`. The `f` closure provided
677-
/// is used to project from the `T` state that `Linker` is associated with to a
678-
/// [`WasiP1Ctx`]. If `T` is `WasiP1Ctx` itself then this is the identity
679-
/// closure, but otherwise it must project out the field where `WasiP1Ctx` is
680-
/// stored within `T`.
676+
/// This method will add WASIp1 functions to `linker`. Access to [`WasiP1Ctx`]
677+
/// is provided with `f` by projecting from the store-local state of `T` to
678+
/// [`WasiP1Ctx`]. The closure `f` is invoked every time a WASIp1 function is
679+
/// called to get access to [`WASIp1`] from `T`. The returned [`WasiP1Ctx`] is
680+
/// used to implement I/O and controls what each function will return.
681681
///
682-
/// The state provided by `f` is used to implement all WASIp1 functions and
683-
/// provides configuration to know what to return.
682+
/// It's recommended that [`WasiP1Ctx`] is stored as a field in `T` or that `T =
683+
/// WasiP1Ctx` itself. The closure `f` should be a small projection (e.g. `&mut
684+
/// arg.field`) or something otherwise "small" as it will be executed every time
685+
/// a WASI call is made.
684686
///
685687
/// Note that this function is intended for use with
686688
/// [`Config::async_support(true)`]. If you're looking for a synchronous version
@@ -745,14 +747,16 @@ pub fn add_to_linker_async<T: Send>(
745747
/// Adds synchronous versions of all WASIp1 functions to the
746748
/// [`wasmtime::Linker`] provided.
747749
///
748-
/// This method will add WASIp1 functions to `linker`. The `f` closure provided
749-
/// is used to project from the `T` state that `Linker` is associated with to a
750-
/// [`WasiP1Ctx`]. If `T` is `WasiP1Ctx` itself then this is the identity
751-
/// closure, but otherwise it must project out the field where `WasiP1Ctx` is
752-
/// stored within `T`.
750+
/// This method will add WASIp1 functions to `linker`. Access to [`WasiP1Ctx`]
751+
/// is provided with `f` by projecting from the store-local state of `T` to
752+
/// [`WasiP1Ctx`]. The closure `f` is invoked every time a WASIp1 function is
753+
/// called to get access to [`WASIp1`] from `T`. The returned [`WasiP1Ctx`] is
754+
/// used to implement I/O and controls what each function will return.
753755
///
754-
/// The state provided by `f` is used to implement all WASIp1 functions and
755-
/// provides configuration to know what to return.
756+
/// It's recommended that [`WasiP1Ctx`] is stored as a field in `T` or that `T =
757+
/// WasiP1Ctx` itself. The closure `f` should be a small projection (e.g. `&mut
758+
/// arg.field`) or something otherwise "small" as it will be executed every time
759+
/// a WASI call is made.
756760
///
757761
/// Note that this function is intended for use with
758762
/// [`Config::async_support(false)`]. If you're looking for a synchronous version

0 commit comments

Comments
 (0)