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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use core::convert::Infallible;
use core::cell::{Ref, RefCell, RefMut};
use alloc::rc::Rc;
use crate::container_traits::{
FragileContainer, FragileMutContainer, FragileTryContainer, FragileTryMutContainer,
};
impl<T: ?Sized> FragileTryContainer<T> for Rc<RefCell<T>> {
type Ref<'a> = Ref<'a, T> where T: 'a;
type RefError = Infallible;
#[inline]
fn new_container(t: T) -> Self where T: Sized {
Self::new(RefCell::new(t))
}
/// Attempt to retrieve the inner `T` from the container.
/// Behaves identically to [`Rc::into_inner`].
#[inline]
fn into_inner(self) -> Option<T> where T: Sized {
Self::into_inner(self).map(RefCell::into_inner)
}
/// Get immutable access to the inner `T`.
///
/// Uses [`RefCell::borrow`], so this container is
/// [fragile](crate#fragility-potential-panics-or-deadlocks).
///
/// ## Panics
/// Panics if the contract of a fragile container is broken.
#[inline]
fn try_get_ref(&self) -> Result<Self::Ref<'_>, Self::RefError> {
Ok(self.borrow())
}
}
impl<T: ?Sized> FragileContainer<T> for Rc<RefCell<T>> {
/// Get immutable access to the inner `T`.
///
/// Uses [`RefCell::borrow`], so this container is
/// [fragile](crate#fragility-potential-panics-or-deadlocks).
///
/// ## Panics
/// Panics if the contract of a fragile container is broken.
#[inline]
fn get_ref(&self) -> Self::Ref<'_> {
self.borrow()
}
}
impl<T: ?Sized> FragileTryMutContainer<T> for Rc<RefCell<T>> {
type RefMut<'a> = RefMut<'a, T> where T: 'a;
type RefMutError = Infallible;
/// Get mutable access to the inner `T`.
///
/// Uses [`RefCell::borrow_mut`], so this container is
/// [fragile](crate#fragility-potential-panics-or-deadlocks).
///
/// ## Panics
/// Panics if the contract of a fragile container is broken.
#[inline]
fn try_get_mut(&mut self) -> Result<Self::RefMut<'_>, Self::RefMutError> {
Ok(self.borrow_mut())
}
}
impl<T: ?Sized> FragileMutContainer<T> for Rc<RefCell<T>> {
/// Get mutable access to the inner `T`.
///
/// Uses [`RefCell::borrow_mut`], so this container is
/// [fragile](crate#fragility-potential-panics-or-deadlocks).
///
/// ## Panics
/// Panics if the contract of a fragile container is broken.
#[inline]
fn get_mut(&mut self) -> Self::RefMut<'_> {
self.borrow_mut()
}
}