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
use owning_ref::OwningRef;
use std::{
any::{type_name, Any, TypeId},
fmt::Debug,
ops::Deref,
panic::Location,
rc::Rc,
};
#[derive(Clone)]
pub(crate) struct AnonRc {
name: &'static str,
id: TypeId,
location: &'static Location<'static>,
depth: u32,
inner: Rc<dyn Any>,
debug: Rc<dyn Debug>,
}
impl AnonRc {
#[track_caller]
pub fn new<T: Debug + 'static>(inner: T, depth: u32) -> Self {
let inner = Rc::new(inner);
Self {
name: type_name::<T>(),
id: TypeId::of::<T>(),
debug: inner.clone(),
location: Location::caller(),
inner,
depth,
}
}
pub(crate) fn downcast_deref<T: Debug + 'static>(
self,
) -> Option<impl Deref<Target = T> + Debug + 'static> {
OwningRef::new(self.inner)
.try_map(|anon| {
let res: Result<&T, &str> = anon.downcast_ref().ok_or("invalid cast");
res
})
.ok()
}
pub fn id(&self) -> TypeId {
self.id
}
pub fn ty(&self) -> &str {
self.name
}
pub fn debug(&self) -> &dyn Debug {
&*self.debug
}
pub fn depth(&self) -> u32 {
self.depth
}
pub fn location(&self) -> &'static Location<'static> {
self.location
}
}