Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 4, 2019 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/522e76a2dda8fbde2be1da96e35fccd5 to your computer and use it in GitHub Desktop.
Save rust-play/522e76a2dda8fbde2be1da96e35fccd5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::sync;
use std::sync::RwLockWriteGuard;
use std::sync::LockResult;
pub struct RwLock<T: ?Sized> {
pub lock: sync::RwLock<T>,
//id: &'static str,
}
impl<T> RwLock<T> {
pub fn new(t: T) -> RwLock<T> {
RwLock { lock: sync::RwLock::new(t) }
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
println!("write lock");
self.lock.write()
}
}
impl<T: ?Sized> Drop for RwLock<T> {
#[inline]
fn drop(&mut self) {
println!("drop write lock");
}
}
fn main() {
println!("Hello, world!");
let lock = RwLock::new(5);
let mut w = lock.write().unwrap();
*w += 1;
assert_eq!(*w, 6);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment