Created
September 4, 2019 20:27
-
-
Save rust-play/522e76a2dda8fbde2be1da96e35fccd5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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