Crate option_lock[−][src]
Expand description
This crate defines a locking structure wrapping an Option
value. The lock
can be acquired using a single atomic operation. The OptionLock
structure
is represented as one atomic u8
variable along with the current value of
the lock (which may be empty). It can be constructed in const
contexts.
The try_lock
and try_take
operations are non-blocking and appropriate
for using within a polled Future
, but the lock cannot register wakers or
automatically park the current thread. A traditional Mutex
or the
async-lock
crate may be used in this case.
This structure allows for multiple usage patterns. A basic example (in this case an AtomicI32 could be substituted):
use option_lock::{OptionLock, OptionGuard}; static SHARED: OptionLock<i32> = OptionLock::new(0); fn try_increase() -> bool { if let Ok(mut guard) = SHARED.try_lock() { let next = guard.take().unwrap() + 1; OptionGuard::replace(&mut guard, next); true } else { false } }
There are additional examples in the code repository.
This crate uses unsafe
code blocks. It is no_std
-compatible when compiled
without the std
feature.
Structs
Lazy | A convenient wrapper around a |
Mutex | An |
MutexGuard | An exclusive guard for a filled |
MutexGuardArc | A write guard for an |
OnceCell | An |
OptionGuard | An exclusive guard for the value of an |
OptionGuardArc | A write guard for the value of an |
OptionLock | A read/write lock around an |
Enums
OptionLockError | Error returned by failing try-lock operations |