crates.io
Declare a crate in crossbind.config.js and import it with the cargo: prefix. crossbind reads the crate's sources and generates the bridge: you write no Rust and no glue.
What you need
The same as for Rust: with the default Docker runner, you need Rust on your machine only for iOS and macOS builds. The first build of a crate needs the network to reach crates.io.
Declare the crate
List crates under packages.cargo in crossbind.config.js (packages is top level, next to dependencies). The key is the crate name; the value is what would follow <crate> = in a Cargo.toml, as a string: a version requirement or an inline table.
Import it
The prefix keeps a crate apart from an npm package of the same name, as node: does. One initNative() binds every module. Importing a crate that packages.cargo does not declare fails the build.
In a browser the module runs in a worker by default, so each call returns a promise (Browser). Node.js without a bundler resolves cargo: imports through the hooks crossbind build -e node writes (Node.js).
Two crates may export the same name, as semver::Version and uuid::Version do. Import each from its cargo: module and rename with as; on the module object initNative() resolves to, they have prefixed names.
One module at a time
A crate that keeps its API in modules is imported one public module at a time, with / between the crate and each module level. xxhash-rust exports nothing from its root:
The module must be public all the way from the crate root and enabled by the features you declare; an inline mod x { ... } block cannot be imported. All imports of one crate share one bridge, so a type is the same JavaScript class whichever module you reach it through.
Through your own .rs file
A declared crate is also a dependency of your .rs files in src/native. Write a small Rust surface over it to reach an API that does not bind as it is, such as a generic one. See Rust.
What binds
crossbind reads the crate's own sources with the features you enabled. An item behind a disabled feature does not bind, and one that only some targets compile (target_arch, target_os) is left out everywhere, so one bridge builds on every platform.
Types map as on the Rust page. A crate whose API is generic, or that only re-exports other crates, may have nothing to bind; the build then warns.
Where it builds
Crates download into ~/.crossbind/cache/cargo, which every project shares, and build for:
- WebAssembly, wasm32,
standmt. - React Native on Android and iOS.
- Node.js addons for macOS, Linux (glibc and musl) and Windows.
Versions and the lockfile
The value becomes the crate's line in a generated Cargo.toml, so Cargo's rules apply: '1' accepts any 1.x release, and features and default-features work in the inline table.
The first build writes crossbind.cargo.lock next to crossbind.config.js. It pins every crate the build links, dependencies included: commit it. Later builds follow it; delete it to resolve every version again.
TypeScript
Each cargo: import is typed by an ambient module, declare module 'cargo:uuid', under .crossbind/rust-crates/types/, which @crossbind/typescript-config includes. A tsconfig that defines its own include replaces that list rather than adding to it, so keep the folder in yours:
For worker mode, set dts: 'promise'; see Runtime API.
Limits
- No WASI builds: there is no
wasm32-wasip3Rust target yet. crossbind licensesdoes not list the crates inpackages.cargo.- In React Native, Metro learns
cargo:<crate>/<module>imports by scanning your sources when it starts, so a specifier assembled at run time is not found.