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
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::any::{Any, TypeId};

use downcast_rs::DowncastSync;
use dyn_clone::DynClone;

use crate::TypeNameLit;

/// Trait to represent the stored type.
#[cfg(all(not(feature = "debug"), not(feature = "resman")))]
pub trait DataType: DowncastSync + DynClone + erased_serde::Serialize {
    fn type_name(&self) -> TypeNameLit;
    fn type_id_inner(&self) -> TypeId;
}

#[cfg(all(not(feature = "debug"), not(feature = "resman")))]
impl<T> DataType for T
where
    T: Any + DynClone + erased_serde::Serialize + Send + Sync,
{
    fn type_name(&self) -> TypeNameLit {
        TypeNameLit(std::any::type_name::<T>())
    }

    fn type_id_inner(&self) -> TypeId {
        TypeId::of::<T>()
    }
}

/// Trait to represent the stored type.
#[cfg(all(not(feature = "debug"), feature = "resman"))]
pub trait DataType: resman::Resource + DowncastSync + DynClone + erased_serde::Serialize {
    fn type_name(&self) -> TypeNameLit;
    fn type_id_inner(&self) -> TypeId;
    fn upcast(self: Box<Self>) -> Box<dyn resman::Resource>;
}

#[cfg(all(not(feature = "debug"), feature = "resman"))]
impl<T> DataType for T
where
    T: Any + DynClone + erased_serde::Serialize + Send + Sync,
{
    fn type_name(&self) -> TypeNameLit {
        TypeNameLit(std::any::type_name::<T>())
    }

    fn type_id_inner(&self) -> TypeId {
        TypeId::of::<T>()
    }

    fn upcast(self: Box<Self>) -> Box<dyn resman::Resource> {
        self
    }
}

/// Trait to represent the stored type.
#[cfg(all(feature = "debug", not(feature = "resman")))]
pub trait DataType: DowncastSync + DynClone + std::fmt::Debug + erased_serde::Serialize {
    fn type_name(&self) -> TypeNameLit;
    fn type_id_inner(&self) -> TypeId;
}

#[cfg(all(feature = "debug", not(feature = "resman")))]
impl<T> DataType for T
where
    T: Any + DynClone + std::fmt::Debug + erased_serde::Serialize + Send + Sync,
{
    fn type_name(&self) -> TypeNameLit {
        TypeNameLit(std::any::type_name::<T>())
    }

    fn type_id_inner(&self) -> TypeId {
        TypeId::of::<T>()
    }
}

/// Trait to represent the stored type.
#[cfg(all(feature = "debug", feature = "resman"))]
pub trait DataType:
    resman::Resource + DowncastSync + DynClone + std::fmt::Debug + erased_serde::Serialize
{
    fn type_name(&self) -> TypeNameLit;
    fn type_id_inner(&self) -> TypeId;
    fn upcast(self: Box<Self>) -> Box<dyn resman::Resource>;
}

#[cfg(all(feature = "debug", feature = "resman"))]
impl<T> DataType for T
where
    T: Any + DynClone + std::fmt::Debug + erased_serde::Serialize + Send + Sync,
{
    fn type_name(&self) -> TypeNameLit {
        TypeNameLit(std::any::type_name::<T>())
    }

    fn type_id_inner(&self) -> TypeId {
        TypeId::of::<T>()
    }

    fn upcast(self: Box<Self>) -> Box<dyn resman::Resource> {
        self
    }
}

downcast_rs::impl_downcast!(sync DataType);
dyn_clone::clone_trait_object!(DataType);

impl<'a> serde::Serialize for dyn DataType + 'a {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        erased_serde::serialize(self, serializer)
    }
}

#[cfg(test)]
mod tests {
    use std::any::TypeId;

    use super::DataType;

    #[test]
    fn type_id_inner_matches_inner_type_type_id() {
        let n = 1u32;

        assert_eq!(TypeId::of::<u32>(), DataType::type_id_inner(&n));
    }

    #[test]
    fn type_id_inner_matches_box_inner_type_type_id() {
        let n = Box::new(1u32);

        assert_eq!(TypeId::of::<u32>(), DataType::type_id_inner(n.as_ref()));
    }
}