MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / declare_import

Function declare_import

crates/rust/src/lib.rs:1781–1813  ·  view source on GitHub ↗
(
    wasm_import_module: &str,
    wasm_import_name: &str,
    rust_name: &str,
    params: &[WasmType],
    results: &[WasmType],
)

Source from the content-addressed store, hash-verified

1779}
1780
1781fn declare_import(
1782 wasm_import_module: &str,
1783 wasm_import_name: &str,
1784 rust_name: &str,
1785 params: &[WasmType],
1786 results: &[WasmType],
1787) -> String {
1788 let mut sig = "(".to_owned();
1789 for param in params.iter() {
1790 sig.push_str("_: ");
1791 sig.push_str(wasm_type(*param));
1792 sig.push_str(", ");
1793 }
1794 sig.push(')');
1795 assert!(results.len() < 2);
1796 for result in results.iter() {
1797 sig.push_str(" -> ");
1798 sig.push_str(wasm_type(*result));
1799 }
1800 format!(
1801 "
1802 #[cfg(target_arch = \"wasm32\")]
1803 #[link(wasm_import_module = \"{wasm_import_module}\")]
1804 unsafe extern \"C\" {{
1805 #[link_name = \"{wasm_import_name}\"]
1806 fn {rust_name}{sig};
1807 }}
1808
1809 #[cfg(not(target_arch = \"wasm32\"))]
1810 unsafe extern \"C\" fn {rust_name}{sig} {{ unreachable!() }}
1811 "
1812 )
1813}
1814
1815fn int_repr(repr: Int) -> &'static str {
1816 match repr {

Callers 4

generate_exportsMethod · 0.85
type_resourceMethod · 0.85
declare_importMethod · 0.85

Calls 4

wasm_typeFunction · 0.70
iterMethod · 0.45
push_strMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected