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
use calyx_utils::GetName;
#[cfg(debug_assertions)]
use calyx_utils::Id;
use std::cell::RefCell;
use std::rc::{Rc, Weak};
#[allow(clippy::upper_case_acronyms)]
pub type RRC<T> = Rc<RefCell<T>>;
pub fn rrc<T>(t: T) -> RRC<T> {
Rc::new(RefCell::new(t))
}
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug)]
pub struct WRC<T>
where
T: GetName,
{
pub(super) internal: Weak<RefCell<T>>,
#[cfg(debug_assertions)]
debug_name: Id,
}
impl<T: GetName> WRC<T> {
pub fn upgrade(&self) -> RRC<T> {
let Some(r) = self.internal.upgrade() else {
#[cfg(debug_assertions)]
unreachable!("weak reference points to a dropped value. Original object's name: `{}'", self.debug_name);
#[cfg(not(debug_assertions))]
unreachable!("weak reference points to a dropped value.");
};
r
}
}
impl<T: GetName> From<&RRC<T>> for WRC<T> {
fn from(internal: &RRC<T>) -> Self {
Self {
internal: Rc::downgrade(internal),
#[cfg(debug_assertions)]
debug_name: internal.borrow().name(),
}
}
}
impl<T: GetName> Clone for WRC<T> {
fn clone(&self) -> Self {
Self {
internal: Weak::clone(&self.internal),
#[cfg(debug_assertions)]
debug_name: self.debug_name,
}
}
}