https://github.com/hinto-janai/rolock
Read Only Lock
https://github.com/hinto-janai/rolock
lock read-only rust sync
Last synced: 8 months ago
JSON representation
Read Only Lock
- Host: GitHub
- URL: https://github.com/hinto-janai/rolock
- Owner: hinto-janai
- License: mit
- Created: 2023-03-20T01:55:36.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-29T18:42:38.000Z (about 3 years ago)
- Last Synced: 2024-04-23T22:39:30.488Z (about 2 years ago)
- Topics: lock, read-only, rust, sync
- Language: Rust
- Homepage: https://docs.rs/rolock
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RoLock
[](https://github.com/hinto-janai/rolock/actions/workflows/windows.yml) [](https://github.com/hinto-janai/rolock/actions/workflows/macos.yml) [](https://github.com/hinto-janai/rolock/actions/workflows/linux.yml) [](https://crates.io/crates/rolock) [](https://docs.rs/rolock)
Read Only Lock.
This is a wrapper around `Arc>` that only implements `RwLock::read()` operations.
## Usage
Create a normal `Arc>` in `thread_1`, send a `RoLock` to `thread_2`:
```rust
let rw = Arc::new(RwLock::new(0)); // Regular Arc>.
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:
```rust
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:
```rust
let rw = Arc::new(RwLock::new(0));
let ro = RoLock::new(&rw);
ro.0.write(); // Compile error!
```