Skip to main content

Module lock

Module lock 

Source
Expand description

§Lock Keypath Module

This module provides LockKp for safely navigating through locked/synchronized data structures.

§Naming convention (aligned with crate::Kp and crate::async_lock)

  • then – chain with a plain crate::Kp
  • then_lock – chain with another LockKp for multi-level lock access

§SHALLOW CLONING GUARANTEE & NO UNNECESSARY CLONES

IMPORTANT: All cloning operations in this module are SHALLOW (reference-counted) clones:

  1. LockKp derives Clone: Clones function pointers and PhantomData only

    • prev and next fields contain function pointers (cheap to copy)
    • mid field is typically just PhantomData<T> (zero-sized, zero-cost)
    • No heap allocations or deep data copies
  2. NO Lock: Clone required for most operations:

    • lock_write takes &Lock (not &mut Lock) due to interior mutability
    • For Arc<Mutex<T>>: No cloning needed, we just use &Arc<...>
    • The actual data T inside is NEVER cloned during lock operations
    • This eliminates unnecessary Arc reference count increments
  3. L: Clone bound (e.g., ArcMutexAccess<T>):

    • Only clones PhantomData<T> which is zero-sized
    • Compiled away completely - zero runtime cost

§Performance Characteristics

  • LockKp::clone(): O(1) - copies a few pointers
  • ArcMutexAccess::clone(): O(1) - no-op (zero-sized type)
  • Lock operations: No Arc cloning needed - direct reference use
  • Total: All operations are constant-time with no deep copying

§Memory Safety

The design is safe because:

  • Locks provide interior mutability (no &mut needed for write operations)
  • Arc provides thread-safe reference counting
  • Mutex ensures exclusive access when needed
  • No dangling pointers or use-after-free possible
  • Rust’s ownership system enforces correctness

Structs§

ArcMutexAccess
Lock access implementation for Arc<Mutex>
ArcRwLockAccess
Lock access implementation for Arc<RwLock>
KpThenLockKp
Keypath that chains a crate::Kp with a LockKp. Use crate::Kp::then_lock to create.
LockKp
A keypath that handles locked values (e.g., Arc<Mutex>)
RcRefCellAccess
Lock access implementation for Rc<RefCell>
StdMutexAccess
Lock access implementation for std::sync::Mutex (without Arc wrapper)
StdRwLockAccess
Lock access implementation for std::sync::RwLock (without Arc wrapper)

Traits§

LockAccess
Trait for types that can provide lock/unlock behavior Converts from a Lock type to Inner or InnerMut value

Type Aliases§

LockKpArcMutexFor
Type alias for LockKp over Arc<std::sync::Mutex>. Use with derive macro’s _lock() methods.
LockKpArcRwLockFor
Type alias for LockKp over Arc<std::sync::RwLock>. Use with derive macro’s _lock() methods.
LockKpType
Type alias for common LockKp usage with Arc<Mutex>