-
Notifications
You must be signed in to change notification settings - Fork 18
/
any.rs
122 lines (102 loc) · 2.72 KB
/
any.rs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/// Mark a type as a [`Resource`]
pub trait ResourceTag: std::fmt::Debug + downcast_rs::DowncastSync + 'static {}
pub trait Resource: ResourceTag {
fn clone_resource(&self) -> Box<dyn Resource>;
}
impl<T: ResourceTag + Clone> Resource for T {
fn clone_resource(&self) -> Box<dyn Resource> {
Box::new(self.clone())
}
}
downcast_rs::impl_downcast!(sync Resource);
impl Clone for Box<dyn Resource> {
fn clone(&self) -> Self {
self.clone_resource()
}
}
#[derive(Clone)]
#[repr(transparent)]
pub(crate) struct BoxedResource(Box<dyn Resource>);
impl BoxedResource {
pub(crate) fn as_ref<R: Resource>(&self) -> &R {
self.0.as_any().downcast_ref::<R>().unwrap()
}
pub(crate) fn as_mut<R: Resource>(&mut self) -> &mut R {
self.0.as_any_mut().downcast_mut::<R>().unwrap()
}
}
impl std::fmt::Debug for BoxedResource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.0.fmt(f)
}
}
#[derive(Clone)]
pub struct BoxedEntry {
pub(crate) id: AnyId,
pub(crate) value: BoxedResource,
}
impl BoxedEntry {
pub(crate) fn new(r: impl Resource) -> Self {
let id = AnyId::from(&r);
let value = BoxedResource(Box::new(r));
BoxedEntry { id, value }
}
}
impl<R: Resource> From<R> for BoxedEntry {
fn from(inner: R) -> Self {
BoxedEntry::new(inner)
}
}
#[derive(Copy, Clone)]
pub(crate) struct AnyId {
type_id: std::any::TypeId,
#[cfg(debug_assertions)]
type_name: &'static str,
}
impl AnyId {
pub(crate) fn of<A: ?Sized + 'static>() -> Self {
Self {
type_id: std::any::TypeId::of::<A>(),
#[cfg(debug_assertions)]
type_name: std::any::type_name::<A>(),
}
}
}
impl PartialEq for AnyId {
fn eq(&self, other: &Self) -> bool {
self.type_id == other.type_id
}
}
impl Eq for AnyId {}
impl PartialOrd for AnyId {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for AnyId {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.type_id.cmp(&other.type_id)
}
}
impl std::hash::Hash for AnyId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.type_id.hash(state);
}
}
impl std::fmt::Debug for AnyId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
#[cfg(not(debug_assertions))]
{
self.type_id.fmt(f)
}
#[cfg(debug_assertions)]
{
f.debug_tuple(self.type_name).field(&self.type_id).finish()
}
}
}
impl<'a, A: ?Sized + 'static> From<&'a A> for AnyId {
fn from(_: &'a A) -> Self {
Self::of::<A>()
}
}