Skip to content

About

Compile Haxe to mostly idiomatic Rust (not ready for consumption yet!)

Topics

Resources

Stars

14 stars

Watchers

0 watching

Forks

Latest commit

 

History

1,012 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

reflaxe.rust logo

reflaxe.rust

Latest Release CI

reflaxe.rust compiles Haxe 4.3.7 code to Rust. It is a compiler target built on Reflaxe.

  • What it produces: an ordinary Cargo project with Rust source that you can read. Cargo then builds a native binary from it.
  • Who it is for: Haxe teams that want native Rust binaries and Rust crates, and Rust-aware teams that want to write typed Haxe against Rust-first APIs.
  • What it aims for: production-ready, near-instant compilation along two paths: ordinary Haxe that becomes safe Rust the compiler optimizes automatically, and metal, which lets you write Rust itself through Haxe and runs like handwritten Rust.
  • How mature it is: pre-1.0. It is usable in production only on the paths that your own application tests. See Maturity.
  • Fastest first run: three commands, in Quick start.

This README describes the main branch. A published release can be older; the releases page lists what each one contains.

Maturity

Compile coverage is broad, but runtime proof varies by operation: the semantic confidence summary lists which portable operations executable tests prove. Use reflaxe.rust in production only on paths that your own application tests, and add smoke tests for networking, TLS, databases, processes, and threads. Before relying on it, read the Production Readiness guide, the feature support matrix, and the release policy in SemVer And Release Posture.

Architecture-claim status: 3 closed, 7 qualified, 1 open. The implemented architecture disproves the old claim that a usable Haxe-to-Rust backend is impossible without either a tracing GC or lifetime annotations everywhere, but the evidence still supports bounded, qualified claims rather than universal parity. Review the exact evidence, exclusions, and blocking Beads in the generated architecture capability claims; this summary does not authorize stable 1.0 or an unqualified “all Haxe to handwritten-quality Rust” claim.

Quick start

You need Node.js >=22.14 <23 with npm, Rust 1.96.0 or newer with Cargo, and Git. npm install installs the pinned Haxe toolchain through lix. See the Rust toolchain policy.

From a checkout of this repository:

npm install
cd examples/hello
npx haxe compile.hxml && (cd out && cargo run -q)

The program prints hi. The generated Cargo project is in examples/hello/out.

Next steps:

  • Plain-language tour: Start Here guide.
  • Common first questions (garbage collection, memory, runtime cost, interop): FAQ.
  • Use the compiler in an existing app: Install via lix.
  • Check your checkout: npm test runs the snapshot tests. npm run test:all runs the full local harness, which takes much longer. GitHub Actions runs the same harness split into parallel jobs.

Start a new project

npm run dev:new-project -- ./my_haxe_rust_app
cd my_haxe_rust_app
cargo hx run

The command copies the starter template (templates/basic). The new project contains:

  • cargo hx run, cargo hx test, and cargo hx build --release: one compile and Cargo cycle.
  • cargo hx dev: compile and run again on each change (see Development loop).
  • bash scripts/dev/check-guards.sh: checks for local paths and security wiring, and runs a gitleaks scan when gitleaks is installed.
  • bash scripts/install-git-hooks.sh: installs the pre-commit checks.

Notes:

  • cargo hx calls haxe from your PATH. Set HAXE_BIN or pass --haxe-bin to use a different one.
  • A project created inside this checkout uses the checkout's Haxe setup. For a project anywhere else, first follow the setup section of the generated README.md or Install via lix.
  • cargo hx test finds methods marked @:rustTest below the main class source root. The app does not need a fake call to keep a test class reachable, and ordinary builds leave these test modules out. See Haxe-authored Rust tests.
  • For an application, commit the generated Cargo.lock and pass -D rust_cargo_locked in CI and release builds. Generated manifests use Cargo resolver 3, which prefers dependencies that work with the declared Rust version.

The full generated-app loop is in Workflow.

What you get

  • Two profiles. portable (the default) keeps Haxe behavior. metal adds Rust-first APIs and stricter rules for performance-critical code. See Pick your profile.
  • A runtime only where Haxe needs one. The compiler decides as much as it can at compile time. The runtime crate hxrt covers what Rust has no direct equivalent for: Dynamic, reflection, exceptions, object identity, anonymous records, null, and platform APIs. In metal, -D rust_no_hxrt builds without it and fails if the output still needs it. See the FAQ.
  • Only the code you use. The crate contains the classes your program uses from every class path and pure-Haxe -lib, as on other Haxe targets. Unused parts of the standard library are left out: hello world emits two Rust modules instead of about thirty. See the FAQ.
  • Plain Rust structs for small values. A final class marked @:rustStruct becomes a Rust Copy struct with no allocation and no lock. In metal it may also have var fields and &mut self methods, updated in place. This is experimental and has strict rules. An ordinary class with only final number or Bool fields gets the same struct without the metadata when no code compares, nulls, or reflects its values. See Value structs.
  • Plain Vec for local arrays. A local Array<T> that never gets a second name is stored as a Rust Vec<T>. Every other array stays a shared Haxe array. See Array semantics.
  • Haxe arithmetic in every build. Int wraps on overflow in debug builds too, as on other Haxe targets. Operators and conversions on Dynamic numbers follow Haxe rules. See Operators.
  • Native Rust types where the meaning matches. For example, the admitted Option and Result surfaces of reflaxe.std compile to Rust Option and Result. That shared package is not bundled with this library yet. See Portable near-native guidance.
  • Typed access to Rust. Bind Rust code with extern classes, add crates with @:rustCargo, and use the typed rust.* modules for files, processes, TCP, and UDP in metal. See the Interop guide and the Metal systems facades roadmap.
  • A map from Rust back to Haxe. Every generated crate includes rust-source-map.json, which ties generated Rust bytes to Haxe source positions. Rust compiler errors are not yet rewritten into Haxe errors. See Rust-to-Haxe source maps.

Per-feature behavior (strings, iterators, generics, anonymous records, reflection, threads, async) is in the Documentation Index and the feature support matrix.

Performance

Speed depends on which Haxe features a program uses.

  • Typed arithmetic, @:rustStruct values, and local arrays stored as Vec compile to plain Rust.
  • An ordinary class instance is a shared, locked reference with its own heap allocation, because Haxe classes have reference semantics. A method that never lets this escape works through the reference it was called with, and number or Bool field reads of this in one expression with no call between them share one lock. A shared array takes a lock on each element access. Dynamic, reflection, and exceptions also go through the runtime.
  • A portable String is shared, immutable text: copying one only counts a new reference. Building new text allocates.

Current measurements, as a ratio of generated-program time to handwritten Rust time:

Workload Result Where it was measured
Integer hot loop about 1.0x committed baseline
haxe.io.Bytes reads and writes about 1.0x committed baseline
haxe.Json parse and stringify about 1.5x serde_json committed baseline
JSON parse plus typed validation about 3.1x to 3.3x committed baseline
haxe.Int64 arithmetic about 1.0x native i64 (221x before Int64 became a native i64) one developer machine; the CI lane is warning-only
Particles with a @:rustStruct vector in a local array about 1.0x to 1.6x idiomatic Rust across runs (24x before the Vec change) one developer machine
Particles updated in place through mutable @:rustStruct methods (metal) about 1.1x idiomatic Rust one developer machine
Particles with an ordinary class vector about 2.2x to 2.5x a handwritten Rc<RefCell<..>> model one developer machine
Strings: record copies, map keys, concatenation, StringBuf, split and join, thread readers about 2.8x idiomatic Rust (4.8x before strings shared their text) one developer machine

Limits of these numbers:

  • They are small benchmarks. There is no measurement of a large application.
  • Each timing covers the whole process, including startup and output. In a run of a few milliseconds, startup hides part of the difference in the loop.
  • The particles and strings results are report-only: no CI gate checks them. The latest runs are in HXRT overhead benchmarks and, for mutable structs, in Value structs.
  • Hot-loop ratios from before the benchmark was repaired are not valid. Read the performance evidence limit first.

Compile time: generating Rust for a large program (about 20,000 generated lines) takes about 11 seconds of CPU time on one machine, down from several minutes. Haxe's own type checking is under a second of that. Hello world generates in under two seconds. npm run perf:compile-time measures this. Protocol, cases, and gates: HXRT overhead benchmarks and the Consumer runtime benchmark corpus.

Pick your profile

Select a profile with -D reflaxe_rust_profile=portable|metal.

Profile Best for What you get
portable (default) Haxe-first teams Haxe behavior and code that stays portable to other Haxe targets
metal Rust-aware teams and performance-critical paths Rust-first APIs (rust.*), stricter boundary rules, and the option to build without the runtime

Rules of thumb:

  • Start in portable. Add metal only where the app needs Rust-first APIs, stricter boundaries, or measured hot-path work.
  • In portable, use the shipped haxe.* and sys.* APIs. sys.io.File, sys.io.Process, and sys.net.Socket keep Haxe behavior and may use hxrt.
  • In metal, use rust.* types when the source itself should be Rust-native. For example, rust.Vec.of([a, b]) emits vec![a, b] without a Haxe Array, and Borrow.withRef / Borrow.withMut give scoped borrowed views that the compiler checks before it emits Rust.
  • If you must write awkward Haxe only to get good Rust output, report it. That is a compiler gap.

Read more: Profiles, Metal profile, and Portable vs metal authoring. Lifetime and RAII guides are in the Documentation Index.

Development loop

cargo hx dev watches the project, compiles on each change, and runs the result. It needs watchexec: brew install watchexec or cargo install watchexec-cli.

cd examples/hello    # or the folder of your own project
cargo hx dev
  • cargo hx dev --mode test runs the tests on each change.
  • cargo hx dev --once runs one cycle and does not need watchexec.
  • Watch mode starts its own Haxe compile server and stops it on exit. The server saves Haxe's parsing and typing. Each compile still generates Rust for the whole program. Pass --no-haxe-server to compile without the server.

More options: Dev Watcher guide.

Examples

What each example covers: Examples Matrix.

Commands for work on the compiler

Task Command
Snapshot tests npm test
Full local harness, same as CI npm run test:all
Remove generated files npm run clean:artifacts:all
Compile-time report npm run perf:compile-time
Refresh the generated summary in this README npm run docs:sync:architecture-capability

More commands are in AGENTS.md and Workflow.

Common defines

  • -D rust_output=out: output directory for the generated Cargo project
  • -D reflaxe_rust_profile=portable|metal: select the profile
  • -D rust_no_build (alias -D rust_codegen_only): generate Rust and skip the Cargo build
  • -D rust_build_release (alias -D rust_release): release build
  • -D rust_target=<triple>: pass a target triple to Cargo
  • -D rust_cargo_locked: pass --locked to Cargo
  • -D rust_async: enable the experimental async/await preview; see the Async/Await guide
  • -D rustfmt: run cargo fmt on the output (best effort; it invalidates rust-source-map.json)

Full list: Defines reference.

Rust interop

Preferred order:

  1. Pure Haxe with the standard library and runtime APIs.
  2. Typed externs and metadata (@:native, @:rustCargo, @:rustExtraSrc).
  3. Framework wrappers around hand-written Rust modules.

Application code should not emit raw Rust with __rust__. It is a last resort for framework code, and the examples and snapshot tests fail when they use it. See the Interop guide.

Rules for hand-written Rust helpers and for crates added by metadata are in the Native facade helper policy and the Support-crate facility (Apple Silicon macOS only).

Status and project docs

About

Compile Haxe to mostly idiomatic Rust (not ready for consumption yet!)

Topics

Resources

Stars

14 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages