Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. use std::sync::{Arc, RwLock, TryLockResult, LockResult, RwLockReadGuard};
  2.  
  3. struct CanOnlyReadLock<T> {
  4. inner: Arc<RwLock<T>>,
  5. }
  6. impl<T> CanOnlyReadLock<T> {
  7. pub fn new(t: T) -> (Arc<RwLock<T>>, Self) {
  8. let lock = Arc::new(RwLock::new(t));
  9. let lock2 = lock.clone();
  10. (lock, Self { inner: lock2 })
  11. }
  12. pub fn read(&self) -> LockResult<RwLockReadGuard<T>> {
  13. self.inner.read()
  14. }
  15. pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<T>> {
  16. self.inner.try_read()
  17. }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement