cargo - pannous/warp GitHub Wiki

use cargo

Rust crates from crates.io work with one line:

use cargo strsim
strsim.levenshtein("kitten", "sitting")   // 3

use crate "strsim" is the same, and use cargo strsim as s picks the name.

A struct that the crate constructs becomes a handle, and its methods work on it:

use cargo regex
digits = regex.Regex("[0-9]+")
digits.replace_all("call 555 1234", "#")   // "call # #"

Functions of a type are written like in Rust, and a struct's public fields read like fields:

use cargo semver
version = semver.Version.parse("1.4.2")
version.minor                                       // 4
semver.VersionReq.parse(">=1.2").matches(version)   // yes

Errors come back as warp errors: regex.Regex("a(") fails with "unclosed group". A panic inside the crate stops the program with a backtrace that names the Rust functions. With WARP_DEBUG_CRATES=1, warp uses the crate's debug build instead (ten times the size and more), and the backtrace shows every call with its file and line:

<regex::regex::string::Regex>::shortest_match_at   at regex-1.13.1/src/regex/string.rs:1088:49

How it works

A crate has no stable ABI of its own, so warp generates a small wrapper crate for it. The wrapper exports every public function whose types can cross into warp: texts, numbers, bools, bytes, lists, options, results, and structs the crate constructs. It is built once per machine into a WebAssembly component (~/.cache/warp/crates) and loaded like wasm components (use wasm "lib.wasm"). The generated interface lies next to it, as <crate>@<version>.wit.

Common crates come prebuilt: the first use downloads the component (checked against its published SHA-256) and needs no Rust at all. Any other crate is built on the first use, which needs cargo and rustup target add wasm32-wasip2 and takes 10 to 60 seconds. Either way the crate loads at once after that, offline too. WARP_BUILD_CRATES=1 always builds locally.

A crate whose API is only traits or generics (sha2) is refused with a message that says so. Not yet: picking a version in the use line, and the browser playground.

⚠️ **GitHub.com Fallback** ⚠️