Coverage Report

Created: 2025-12-31 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/mea-0.5.2/src/internal/rwlock.rs
Line
Count
Source
1
// Copyright 2024 tison <wander4096@gmail.com>
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
use std::sync::PoisonError;
16
17
pub(crate) struct RwLock<T: ?Sized>(std::sync::RwLock<T>);
18
19
impl<T> RwLock<T> {
20
0
    pub(crate) const fn new(t: T) -> Self {
21
0
        Self(std::sync::RwLock::new(t))
22
0
    }
23
}
24
25
impl<T: ?Sized> RwLock<T> {
26
0
    pub(crate) fn read(&self) -> std::sync::RwLockReadGuard<'_, T> {
27
0
        self.0.read().unwrap_or_else(PoisonError::into_inner)
28
0
    }
29
30
0
    pub(crate) fn write(&self) -> std::sync::RwLockWriteGuard<'_, T> {
31
0
        self.0.write().unwrap_or_else(PoisonError::into_inner)
32
0
    }
33
}
34
35
#[cfg(test)]
36
mod tests {
37
    use std::sync::Arc;
38
39
    use crate::internal::RwLock;
40
41
    #[test]
42
    fn test_poison_rwlock() {
43
        let rwlock = Arc::new(RwLock::new(42));
44
        let r = rwlock.clone();
45
        let handle = std::thread::spawn(move || {
46
            let _guard = r.write();
47
            panic!("poison");
48
        });
49
        let _ = handle.join();
50
        assert_eq!(*rwlock.read(), 42);
51
        assert_eq!(*rwlock.write(), 42);
52
    }
53
}