1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#[cfg(all(feature = "concurrency", not(debug_assertions)))]
pub use parking_lot::{RwLock as RwLockImpl, RwLockReadGuard, RwLockWriteGuard};
#[cfg(any(not(feature = "concurrency"), debug_assertions))]
pub use std::sync::{RwLock as RwLockImpl, RwLockReadGuard, RwLockWriteGuard};

pub struct RwLock<T>(RwLockImpl<T>);

impl<T: Default> Default for RwLock<T> {
    fn default() -> Self {
        RwLock::new(Default::default())
    }
}

#[cfg(all(feature = "concurrency", not(debug_assertions)))]
impl<T> RwLock<T> {
    pub fn new(v: T) -> Self {
        RwLock(RwLockImpl::new(v))
    }

    pub fn into_inner(self) -> T {
        self.0.into_inner()
    }

    pub fn read(&self) -> RwLockReadGuard<T> {
        self.0.read()
    }

    pub fn write(&self) -> RwLockWriteGuard<T> {
        self.0.write()
    }

    pub fn try_read(&self) -> Option<RwLockReadGuard<T>> {
        self.0.try_read()
    }

    pub fn try_write(&self) -> Option<RwLockWriteGuard<T>> {
        self.0.try_write()
    }
}

#[cfg(any(not(feature = "concurrency"), debug_assertions))]
impl<T> RwLock<T> {
    pub fn new(v: T) -> Self {
        RwLock(RwLockImpl::new(v))
    }

    pub fn into_inner(self) -> T {
        self.0.into_inner().ok().unwrap()
    }

    pub fn read(&self) -> RwLockReadGuard<T> {
        self.0.read().unwrap()
    }

    pub fn write(&self) -> RwLockWriteGuard<T> {
        self.0.write().unwrap()
    }

    pub fn try_read(&self) -> Option<RwLockReadGuard<T>> {
        self.0.try_read().ok()
    }

    pub fn try_write(&self) -> Option<RwLockWriteGuard<T>> {
        self.0.try_write().ok()
    }
}