1#[allow(unused_imports)] #[cfg(feature = "parking_lot")]
12pub(crate) use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
13
14#[cfg(not(feature = "parking_lot"))]
15pub(crate) use self::std_impl::*;
16
17#[cfg(not(feature = "parking_lot"))]
18mod std_impl {
19 use std::sync::{self, PoisonError, TryLockError};
20 pub(crate) use std::sync::{RwLockReadGuard, RwLockWriteGuard};
21
22 #[derive(Debug)]
23 pub(crate) struct RwLock<T> {
24 inner: sync::RwLock<T>,
25 }
26
27 impl<T> RwLock<T> {
28 pub(crate) fn new(val: T) -> Self {
29 Self {
30 inner: sync::RwLock::new(val),
31 }
32 }
33
34 #[inline]
35 pub(crate) fn get_mut(&mut self) -> &mut T {
36 self.inner.get_mut().unwrap_or_else(PoisonError::into_inner)
37 }
38
39 #[inline]
40 pub(crate) fn read(&self) -> RwLockReadGuard<'_, T> {
41 self.inner.read().unwrap_or_else(PoisonError::into_inner)
42 }
43
44 #[inline]
45 #[allow(dead_code)] pub(crate) fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
47 match self.inner.try_read() {
48 Ok(guard) => Some(guard),
49 Err(TryLockError::Poisoned(e)) => Some(e.into_inner()),
50 Err(TryLockError::WouldBlock) => None,
51 }
52 }
53
54 #[inline]
55 pub(crate) fn write(&self) -> RwLockWriteGuard<'_, T> {
56 self.inner.write().unwrap_or_else(PoisonError::into_inner)
57 }
58
59 #[inline]
60 #[allow(dead_code)] pub(crate) fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
62 match self.inner.try_write() {
63 Ok(guard) => Some(guard),
64 Err(TryLockError::Poisoned(e)) => Some(e.into_inner()),
65 Err(TryLockError::WouldBlock) => None,
66 }
67 }
68 }
69}