Crate cross_locks

Source
Expand description

§cross‑locks

A single‑source, zero‑dependency¹ implementation of Supabase/GoTrue‑style global exclusive locks that work identically across every runtime.

target / runtimecompile‑time featurebackend used by DefaultLock
Native (Tokio, multi‑thread)nativefair FIFO queue (Notify)
Browser WASM (2022 + navigator.locks)browserNavigator.locks.request
Head‑less WASM (Node / WASI / tests)wasmno‑op passthrough

* Safari shipped the LockManager API in 16.4 (2023‑03).

§Guarantees

  • FIFO fairness for every waiter requesting the same lock name on native.
  • Zero‑timeout try‑lock (Duration::ZERO) mirrors the semantics of the JS SDK.
  • Task‑local re‑entrancy helper demonstrated in the test‑suite.
  • Arc<T> automatically implements GlobalLock, ideal for storing in an Axum State, OnceCell, etc.

¹ Outside the optional features listed below (async‑trait, thiserror, Tokio / wasm‑bindgen).

# Cargo.toml – choose exactly ONE backend
[dependencies]
cross-locks = { version = "*", default-features = false, features = ["native"] }
# or  "browser"  |  "wasm"
# run the exhaustive native test‑suite
cargo test --features native

# browser smoke‑test (headless Firefox)
wasm-pack test --firefox --headless --features browser

Macros§

async_test
Expands to tokio::test on native targets and to a wasm_bindgen_test module on wasm32 (so the name doesn’t clash with the outer function space).
dual_test
Shorthand when you don’t need a dedicated module on wasm (i.e. the test name is unique anyway). Generates either a tokio::test or a wasm_bindgen_test for the same async function.

Structs§

NoopLock
Passthrough implementation – used by the wasm (Node/WASI) feature and in single‑thread CLI tests.

Enums§

LockError
Errors returned by GlobalLock::with_lock.

Traits§

GlobalLock
Back‑end agnostic trait. On wasm32 the Send bounds are automatically relaxed (?Send).