๐Ÿฆ€๐Ÿ” Statically prevent deadlocks crates.io/crates/surelock
typesafe utility deadlock-freedom library
0

Configure Feed

Select the types of activity you want to include in your feed.

surelock / HACKING.md
4.9 kB

Contributing to Surelock#

Prerequisites#

This project uses Nix for reproducible development environments.

nix develop
menu          # list available commands

Building#

cargo build
cargo build --all-features
cargo build --no-default-features    # no_std check

Testing#

cargo test                           # all tests (unit, integration, doc, property, compile-fail)
cargo test --doc                     # doc tests only
cargo test --test integration        # integration tests only
cargo test --test property           # property tests (bolero)
cargo test --test compile_fail       # compile-fail tests (trybuild)

Linting#

cargo clippy --all-targets
cargo clippy --all-targets --all-features
cargo fmt --check
cargo doc                            # check for doc warnings

Project Structure#

src/
โ”œโ”€โ”€ acquirable.rs           Acquirable<'a> trait (internal)
โ”œโ”€โ”€ acquirable/tuples.rs    Tuple impls 2-12
โ”œโ”€โ”€ id.rs                   LockId
โ”œโ”€โ”€ key.rs                  MutexKey, lock_scope, try_lock_scope
โ”œโ”€โ”€ key_handle.rs           KeyHandle
โ”œโ”€โ”€ key_voucher.rs          KeyVoucher
โ”œโ”€โ”€ level.rs                Level<N>, IsLevel, LockAfter, etc.
โ”œโ”€โ”€ locksmith.rs            Locksmith
โ”œโ”€โ”€ mutex.rs                Mutex<T, Lvl, R>
โ”œโ”€โ”€ mutex/guard.rs          MutexGuard
โ”œโ”€โ”€ raw_mutex.rs            RawMutex trait
โ”œโ”€โ”€ raw_mutex/std_mutex.rs  StdMutex backend
โ”œโ”€โ”€ raw_mutex/lock_api_adapter.rs  lock_api bridge
โ””โ”€โ”€ set.rs                  LockSet

tests/
โ”œโ”€โ”€ integration.rs          Full lifecycle tests
โ”œโ”€โ”€ lock_order.rs           Level declaration tests
โ”œโ”€โ”€ compile_fail.rs         Compile-fail test runner
โ”œโ”€โ”€ compile_fail/*.rs       Individual compile-fail cases
โ””โ”€โ”€ property.rs             Property tests (bolero)

design/                     Published design docs
โ”œโ”€โ”€ README.md               Architecture overview
โ”œโ”€โ”€ ROADMAP.md              Planned features for v2+
โ””โ”€โ”€ comparison/             Comparisons with prior art

.ignore/                    Internal design notes (gitignored, not published)
โ”œโ”€โ”€ CONTEXT.md              Codebase overview
โ”œโ”€โ”€ DESIGN.md               Architecture reference
โ”œโ”€โ”€ DECISIONS.md            ADRs (Architecture Decision Records)
โ”œโ”€โ”€ ALTERNATIVES.md         Approaches considered
โ”œโ”€โ”€ TODO.md                 Task list
โ””โ”€โ”€ CRITICAL_SECTION_BACKEND.md  no_std backend sketch

Unsafe Policy#

#![deny(unsafe_code)] at the crate level. Only modules that need unsafe opt in via #![allow(unsafe_code)]:

  • raw_mutex.rs -- unsafe trait RawMutex
  • raw_mutex/std_mutex.rs -- unsafe impl for StdMutex
  • raw_mutex/lock_api_adapter.rs -- blanket unsafe impl
  • mutex.rs -- unsafe impl Send/Sync
  • mutex/guard.rs -- Deref/DerefMut on UnsafeCell
  • key_voucher.rs -- unsafe impl Send

All other modules are fully safe. Adding unsafe to a new module requires justification.

Naming Conventions#

Type Name Variable
Factory Locksmith smith
Voucher KeyVoucher voucher
Per-thread KeyHandle handle
Scope key MutexKey key
Guard MutexGuard guard
Lock group Acquirable --
Pre-sorted LockSet set

Adding a New Level Trait#

Level impls are generated by macros in src/level.rs. To change the default count or add a new trait to the level system:

  1. Update the generate_level_impls! macro
  2. Update the #[cfg] blocks for feature-gated counts
  3. Run cargo test -- property tests verify level ordering invariants

Adding a New Tuple Arity#

Tuple Acquirable impls are in src/acquirable/tuples.rs. The 2-tuple supports multi-level (different Lvl per element); arities 3-12 require same level. To add a multi-level impl for higher arities, see the 2-tuple impl as a reference.

Compile-Fail Tests#

Compile-fail tests use trybuild. Each test case is a .rs file in tests/compile_fail/ with a corresponding .stderr file. To update expected output after a change:

TRYBUILD=overwrite cargo test --test compile_fail

Design Documents#

Published design docs live in design/:

  • design/README.md -- architecture overview and design principles
  • design/ROADMAP.md -- planned features for v2+
  • design/comparison/ -- detailed comparisons with happylock and lock_tree

Internal design notes live in .ignore/ (gitignored, not published):

  • DECISIONS.md -- numbered ADRs with rationale
  • DESIGN.md -- architecture reference
  • TODO.md -- task list with checkbox notation

When making design decisions, add an ADR to .ignore/DECISIONS.md.