use crate::read_guard::RwLockReadGuard;
use crate::sys;
use crate::write_guard::RwLockWriteGuard;
use std::io;
#[derive(Debug)]
pub struct RwLock<T: sys::AsRaw> {
lock: sys::RwLock<T>,
}
impl<T: sys::AsRaw> RwLock<T> {
#[inline]
pub fn new(inner: T) -> Self {
Self {
lock: sys::RwLock::new(inner),
}
}
#[inline]
pub fn read(&self) -> io::Result<RwLockReadGuard<'_, T>> {
let guard = self.lock.read()?;
Ok(RwLockReadGuard::new(guard))
}
#[inline]
pub fn try_read(&self) -> io::Result<RwLockReadGuard<'_, T>> {
let guard = self.lock.try_read()?;
Ok(RwLockReadGuard::new(guard))
}
#[inline]
pub fn write(&mut self) -> io::Result<RwLockWriteGuard<'_, T>> {
let guard = self.lock.write()?;
Ok(RwLockWriteGuard::new(guard))
}
#[inline]
pub fn try_write(&mut self) -> io::Result<RwLockWriteGuard<'_, T>> {
let guard = self.lock.try_write()?;
Ok(RwLockWriteGuard::new(guard))
}
#[inline]
pub fn into_inner(self) -> T
where
T: Sized,
{
self.lock.into_inner()
}
}