generic_container/impls/arc_rwlock.rs
1use core::convert::Infallible;
2use alloc::sync::Arc;
3use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
4
5use crate::container_traits::{
6 FragileContainer, FragileMutContainer, FragileTryContainer, FragileTryMutContainer,
7};
8use super::HandlePoisonedResult as _;
9
10
11impl<T: ?Sized> FragileTryContainer<T> for Arc<RwLock<T>> {
12 type Ref<'a> = RwLockReadGuard<'a, T> where T: 'a;
13 type RefError = Infallible;
14
15 #[inline]
16 fn new_container(t: T) -> Self where T: Sized {
17 Self::new(RwLock::new(t))
18 }
19
20 /// Attempt to retrieve the inner `T` from the container.
21 /// Behaves identically to [`Arc::into_inner`].
22 ///
23 /// Ignores any poison errors.
24 #[inline]
25 fn into_inner(self) -> Option<T> where T: Sized {
26 Self::into_inner(self)
27 .map(RwLock::into_inner)
28 .map(Result::ignore_poisoned)
29 }
30
31 /// Get immutable access to the inner `T`.
32 ///
33 /// Uses [`RwLock::read`], so this container is
34 /// [fragile](crate#fragility-potential-panics-or-deadlocks).
35 ///
36 /// # Panics and Deadlocks
37 /// Panics if a poison error is encountered, which can only occur if another thread has
38 /// already panicked.
39 ///
40 /// May also panic or deadlock if the contract of a fragile container is broken.
41 #[inline]
42 fn try_get_ref(&self) -> Result<Self::Ref<'_>, Self::RefError> {
43 Ok(self.read().panic_if_poisoned())
44 }
45}
46
47impl<T: ?Sized> FragileContainer<T> for Arc<RwLock<T>> {
48 /// Get immutable access to the inner `T`.
49 ///
50 /// Uses [`RwLock::read`], so this container is
51 /// [fragile](crate#fragility-potential-panics-or-deadlocks).
52 ///
53 /// # Panics and Deadlocks
54 /// Panics if a poison error is encountered, which can only occur if another thread has
55 /// already panicked.
56 ///
57 /// May also panic or deadlock if the contract of a fragile container is broken.
58 #[inline]
59 fn get_ref(&self) -> Self::Ref<'_> {
60 self.read().panic_if_poisoned()
61 }
62}
63
64impl<T: ?Sized> FragileTryMutContainer<T> for Arc<RwLock<T>> {
65 type RefMut<'a> = RwLockWriteGuard<'a, T> where T: 'a;
66 type RefMutError = Infallible;
67
68 /// Get mutable access to the inner `T`.
69 ///
70 /// Uses [`RwLock::write`], so this container is
71 /// [fragile](crate#fragility-potential-panics-or-deadlocks).
72 ///
73 /// # Panics and Deadlocks
74 /// Panics if a poison error is encountered, which can only occur if another thread has
75 /// already panicked.
76 ///
77 /// May also panic or deadlock if the contract of a fragile container is broken.
78 #[inline]
79 fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError> {
80 Ok(self.write().panic_if_poisoned())
81 }
82}
83
84impl<T: ?Sized> FragileMutContainer<T> for Arc<RwLock<T>> {
85 /// Get mutable access to the inner `T`.
86 ///
87 /// Uses [`RwLock::write`], so this container is
88 /// [fragile](crate#fragility-potential-panics-or-deadlocks).
89 ///
90 /// # Panics and Deadlocks
91 /// Panics if a poison error is encountered, which can only occur if another thread has
92 /// already panicked.
93 ///
94 /// May also panic or deadlock if the contract of a fragile container is broken.
95 #[inline]
96 fn get_mut(&mut self) -> Self::RefMut<'_> {
97 self.write().panic_if_poisoned()
98 }
99}