#[wasm_split]Expand description
Indicate a function as a split point.
The macro emits a function with the same signature, except that it is async. Calls to this function will first load
the module into which the annotated function was split into before forwarding the arguments and the result. On non-wasm
targets, the function will be called directly.
The annotated function must fulfill the requirements a typical extern declared function must fufill:
- It can not be
async. If you want to support this, you mustBoxor otherwise wrap theFutureinto adynobject. Also see thereturn_wrapperoption for some further hints. - It can not be
const. - It can not make use of a receiver argument, generics or an
implreturn type. - The only extern linkage on wasm is
#[wasm_import_module]which impliesextern "C", see this blog post. The macro allows you to specify a different linkage but that is only used for the generated wrapper function. The forward call will happen with"C"ABI. This means in particular that panicking is forbidden across the call. As of now, a panic on wasm leads to an abort, and this doc serves only as a warning.
ยงSyntax
wasm_split($module:ident (, $option ),* ) => { ... };All functions with the same specified $module end up in one split off WASM chunk.
The following options are supported:
-
wasm_split_path = $this:pathchanges the path at which the runtime support crate is expected. As a framework, you might want to reexport this from some hidden module path. Default:::wasm_split_helpers. -
return_wrapper( let $bindings:pat = _ ; $compute:block -> $ret:ty ). A rather low-level option to support rewriting the result of the wrapped function. The generated wrapper will, rather than directly return the result from the user-given function, bind this to$bindingsand emit the statements in$computeto generate the return value of the wrapper with the return type indicated by$ret.Example use case:
return_wrapper( let future = _ ; { future.await } -> Output)toawaita future directly in the wrapper. -
preload( $( #[$attr] )* $preload_name:ident )generates an additional preload function$preload_namethat fetches the module in which the wrapped function is contained without calling it. Its signature isasync fn()and a hard load failure panics, matching the wrapper; with thefallibleoption it instead returnsResult<(), $wasm_split_path::SplitLoaderError>. See alsoSplitLoaderError. -
falliblesurfaces a load failure asErrinstead of panicking (a panic aborts the whole wasm module). The annotated function must returnResult<_, E>whereE: From<$wasm_split_path::SplitLoaderError>; the macro leaves that signature untouched and converts a load failure into yourEvia?, so a use-site can fold it straight into a framework-specific error. The generatedpreloadfunction (if any) returnsResult<(), $wasm_split_path::SplitLoaderError>. Without this option both keep their infallible signatures and a hard load failure panics. The failure is not cached, so a later call retries from scratch. See alsoSplitLoaderError.