externref

Attribute Macro externref 

Source
#[externref]
Expand description

Prepares imported functions or an exported function with Resource args and/or return type.

§Inputs

This attribute must be placed on an extern "C" { ... } block or an extern "C" fn. If placed on block, all enclosed functions with Resource args / return type will be wrapped.

§Processing

The following arg / return types are recognized as resources:

  • Resource<..>, &Resource<..>, &mut Resource<..> with one or two type args
  • ResourceCopy<..> and its references
  • Option<_> of the above six variations

For complex cases, resource detection can be controlled with a #[resource] attribute. This attribute can be placed on a function arg or on the function itself (in which case it corresponds to the return type; attributes cannot be placed on the return type directly).

  • #[resource], #[resource = true] or #[resource(true)] mark an arg / return type as a resource.
  • #[resource = false] or #[resource(false)] mark an arg / return type as a non-resource.

§Attributes

The externref macro supports attributes specified in parentheses after the macro (e.g., #[externref(crate = path::to::externref)]).

§crate

Type: path or string

Allows specifying a path to the externref crate, e.g. to re-export it from a higher-level library. A path can be either a string ("path::to::externref"), or an unquoted path (path::to::externref).

§stubs

Type: compilation condition, e.g. target_family = "wasm".

Generates stub import functions for non-WASM targets with unreachable!() contents. This is useful if the library needs to be compiled (but not run) on non-WASM targets. By default, the compilation condition determining WASM targets is target_family = "wasm"; it can be changed by specifying the condition in parentheses, like stubs(target_arch = "wasm32").

This attribute is only supported on extern modules and will lead to an error if specified on an export.