Documentation
  • Coverage
  • 85.71%
    12 out of 14 items documented7 out of 12 items with examples
  • Size
  • Source code size: 13.93 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.77 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • hinto-janai/rolock
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • hinto-janai

RoLock

Windows macOS Linux crates.io docs.rs

Read Only Lock.

This is a wrapper around Arc<RwLock<T>> that only implements RwLock::read() operations.

Usage

Create a normal Arc<RwLock<T>> in thread_1, send a RoLock to thread_2:

let rw = Arc::new(RwLock::new(0));     // Regular Arc<RwLock<T>>.
let ro = RoLock::new(&rw);             // Read Only Lock.

assert!(*rw.read().unwrap() == 0);     // This can read...
*rw.write().unwrap() = 1;              // and write.

std::thread::spawn(move|| {
	assert!(*ro.read().unwrap() == 1); // This one can only read.
});
  • thread_1 still has full read/write control
  • thread_2 can only RoLock::read()

This type guarantees at compile time that you cannot write because the function doesn't even exist:

let rw = Arc::new(RwLock::new(0));
let ro = RoLock::new(&rw);

ro.write(); // Compile error!

Since the inner field of RoLock (self.0) is private, you can't call RwLock::write directly either:

let rw = Arc::new(RwLock::new(0));
let ro = RoLock::new(&rw);

ro.0.write(); // Compile error!