Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.