Oxidizing OCaml: Rust-Style Ownership
Jane Street's OCaml extensions add Rust-style ownership through mode axes. A uniqueness axis (unique, exclusive, shared) enables in-place functional updates, type-changing strong updates and safe resource APIs. Borrowing via `&` combines with locality so values can be lent without consuming them. Exclusively mutable fields give mutation without move semantics, and a linearity axis (once vs many) stops closures that capture unique values from being invoked twice, which would otherwise break type safety. Unlike Rust, these modes carry no lifetime variables, so they avoid higher-order polymorphism and don't interfere with type inference.
Related posts
- Oxidizing OCaml: Data Race Freedom
Jane Street Tech Blog hardMasterJane Street's OCaml extensions use a new `sync` mode to make data races statically unrepresentable in multicore OCaml 5. Immutable and exclusively mutable values are `sync` and shareable across domains, while ordinary mutable values are `unsync` and confined to one domain. Mode-annotated mutexes give roughly safe-Rust capabilities. Capsules go further: they encapsulate unrestricted shared mutability, such as cyclic mutable structures, behind existentially typed keys, so code can mutate shared-ownership data in parallel without Rust-style fine-grained ownership tracking or unsafe code.
- Oxidizing OCaml: Locality
Jane Street Tech Blog hardMasterJane Street is extending OCaml with modes, starting with a locality mode that marks values as local (cannot escape their region) or global, so the compiler can stack-allocate short-lived values while keeping garbage collection everywhere else. Locality is inferred automatically and checked by the compiler. An `exclave` keyword lets a function return a local value into its caller's region, and local parameters let APIs like `with_file` guarantee that resources are not used after release. Unlike Rust's lifetime variables, modes are separate from types and do not affect type inference. They are deep and form a sub-mode order (global ≤ local), which forbids heap-to-stack pointers.
- ICFP 2024
Jane Street Tech Blog hardMasterJane Street's OCaml Language team brings nine papers and talks to ICFP 2024 and describes its compiler-branch extensions: modal types (modes like local and unique) for Rust-like control over allocation and data-race-free parallelism, and a kind system for unboxed layouts. Examples include a redesigned Bonsai API that uses a local-mode witness and escape analysis to enforce its two-phase graph-building model, mixed blocks for records combining boxed and unboxed fields, ppx_template for stamping out kind/mode-specialized code until real polymorphism lands, and a zero-overhead or_null option type that uses kinds to forbid nesting. The team also released a bleeding-edge opam repo with the extended compiler and its libraries so outsiders can try the features ahead of any upstreaming.
- Using OxCaml to implement type-safe reference counting between OCaml and Python
Jane Street Tech Blog hardMasterWrapping Python objects as OCaml values in PyOCaml ties each object's release to the OCaml GC finalizer, so large data frames or GPU buffers can stay alive long after Python code drops them. OxCaml's modes fix this: `@ local` arguments let C glue borrow Python references without an incref, `@ unique` returns let it steal the reference back without one, and `borrow_` keeps a value unique across local calls. For manual control, `Py.newref`, a uniqueness-consuming `Py.decref` and `ppx_release` provide type-safe explicit reference counting that frees objects promptly even when exceptions are raised.
- Autogenerating Rust-JS bindings with UniFFI
Mozilla Hacks mediumAdeptMozilla extended UniFFI, its Rust library for auto-generating foreign-language bindings, to emit JavaScript bindings for Firefox Desktop starting in Firefox 105, replacing error-prone hand-written wrappers that undermined Rust's memory safety. Because JavaScript cannot call C ABI functions directly, Firefox's WebIDL code generator produces only the minimal C++ scaffolding layer, with the idiomatic user API written in JavaScript on top. UniFFI calls are blocking while the Firefox main thread must never block, so every generated JS function returns a promise, with per-function sync/async control planned. The approach shipped the Rust remote-tabs sync engine across Desktop, Android and iOS in Firefox 108.