#lazy-evaluation #lazy-mut #lazy-lock-mutex-t

no-std lazy_mut

Alternative to LazyLock<Mutex<T>>

2 unstable releases

0.2.1 Jan 20, 2025
0.1.0 Mar 11, 2018

#699 in Algorithms

Download history 308/week @ 2024-12-04 111/week @ 2024-12-11 182/week @ 2024-12-18 59/week @ 2024-12-25 143/week @ 2025-01-01 242/week @ 2025-01-08 591/week @ 2025-01-15 355/week @ 2025-01-22 513/week @ 2025-01-29 625/week @ 2025-02-05 509/week @ 2025-02-12 412/week @ 2025-02-19 539/week @ 2025-02-26 1087/week @ 2025-03-05 726/week @ 2025-03-12 576/week @ 2025-03-19

3,026 downloads per month
Used in librstb

MIT/Apache

19KB
329 lines

lazy-mut

Overview

The LazyMut library provides a synchronization primitive for deferred initialization with a single synchronization step. It is especially useful for scenarios where initialization logic is expensive or should be deferred until first use. The library supports multiple locking backends, including std, parking_lot, and spin (depending on feature flags). This library is #![no_std] compatible, making it suitable for embedded systems and environments where the standard library is unavailable.

Features

  • thread-safe lazy-initialization structure that wraps an initialization function and synchronizes access to the inner data.
  • RAII Guards: Provides scoped locks via LazyMutGuard for safe and automatic unlocking.
  • Poisoning Support: Detects panics during initialization and ensures subsequent accesses are safe, marking the instance as poisoned.
  • Configurable Locking: Uses RawMutex from different synchronization backends (e.g., std, parking_lot, or spin) depending on enabled features.
use lazy_mut::LazyMut;

static VICTOR: LazyMut<Vec<u8>> = LazyMut::new(|| vec![1, 2, 3]);

fn main() {
    VICTOR.get_mut().push(10);
    VICTOR.try_get_mut().unwrap().push(10);
    
    assert_eq!(*VICTOR.get_mut(), [1, 2, 3, 10 ,10])
}

Dependencies

~0.4–5MB
~11K SLoC