Skip to content

Bridge

sh
rust-anything bridge <repo> <unit-id>          # crate in <repo>/rust/<name>; never overwrites
rust-anything refresh <crate>                  # after changing a signature in port.rs

bridge creates a crate with the bindings (lib.rs, plus the wrapper the source language loads) and stubs to port (port.rs). You write the port in src/port.rs only; src/lib.rs and the wrappers are generated, and refresh regenerates them from the signatures in port.rs, adding any new batch classes. It never touches port.rs.

Options:

  • --batch fn:param adds a class that keeps many param values in Rust. See Batches.
  • --as-json T bridges a type parameter the app only fills with data (T in paginate<T>(items: T[])) as JSON values.
  • --keep "name: reason" keeps more exports in the source language, e.g. code that depends on Intl data.
  • --kind overrides the bridge the plan chose (napi-rs or wasm for TypeScript).

The bridge per language

Language Bridge
TypeScript/JavaScript napi-rs or wasm-bindgen with a generated binary wire; classes and async included
Python PyO3 (abi3), direct conversion, typed exceptions
C extern "C" functions with the original names and a header
Go typed cgo calls and a generated binary wire, in a package with the original API
Ruby a magnus extension under the original modules
Java typed JNI and a generated binary wire, in wrapper classes with the original methods

PHP and C# units cannot be bridged yet; see PHP and C#.

What bridge decides

  • Exports that cannot cross a bridge stay in the source language, with the reason, and the shadow module re-exports the original. For TS: functions that take or return functions, generators and iterators, RegExp, instanceof checks, generics (--as-json T bridges one the app only fills with data), and values whose types (followed through imports, re-exports and extends) hold functions. --keep "name: reason" keeps more, e.g. code that depends on Intl data.
  • TS types map to Rust types, with type aliases resolved through imports and parameter types inferred from default values; structured values cross as a generated binary wire (JSON only as a fallback for shapes the wire does not model).
  • JSON cannot express undefined, NaN, Infinity, -0 or lone surrogates: structured values carry markers ({"$undefined": true}, {"$num": "NaN"}, {"$utf16": [...]}) that the generated index.cjs translates, with helpers in the crate's js.rs.

record runs TS sources as they are: a Node resolve hook follows bundler-style imports (./errors to ./errors.ts), --resolve-from supplies npm packages when the repo has none installed, and TS that needs transforming (enums, parameter properties) is retried with --experimental-transform-types.

Edit this page on GitHub →