Crate safe_lock[][src] version license: Apache 2.0 unsafe forbidden pipeline status


A simple SafeLock struct.

Use Cases

  • Run tests sequentially
  • Prevent concurrent operations on atomic values
  • Prevent concurrent operations on data and systems outside the Rust runtime


  • Const constructor
  • Depends only on std
  • forbid(unsafe_code)
  • 100% test coverage


  • Not a Mutex<T>. Does not contain a value.
  • Unoptimized. Uses AtomicBool in a spinlock, not fast OS locks.
  • Not a fair lock. If multiple threads acquire the lock in loops, some may never acquire it.



Make some tests run sequentially so they don’t interfere with each other:

use safe_lock::SafeLock;
static LOCK: SafeLock = SafeLock::new();

fn test1() {
    let _guard = LOCK.lock();
    // ...

fn test2() {
    let _guard = LOCK.lock();
    // ...

Cargo Geiger Safety Report


  • v0.1.3 - Increase test coverage
  • v0.1.2 - Use Acquire and Release ordering
  • v0.1.1 - Update docs
  • v0.1.0 - Initial version

Happy Contributors 🙂

Fixing bugs and adding features is easy and fast. Send us a pull request and we intend to:

  • Always respond within 24 hours
  • Provide clear & concrete feedback
  • Immediately make a new release for your accepted change



A lock.


A handle to the acquired lock. Drop this to release the lock.