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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::ops::{Deref, DerefMut};

use serde::Serialize;

use crate::{
    untagged::{BoxDataTypeDowncast, DataType, DataTypeWrapper, FromDataType},
    TypeNameLit,
};

/// Box of any type, with no additional trait constraints.
#[derive(Clone, Serialize)]
pub struct BoxDt(pub(crate) Box<dyn DataType>);

#[cfg(not(feature = "debug"))]
impl std::fmt::Debug for BoxDt {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_tuple("BoxDt").field(&"..").finish()
    }
}

#[cfg(feature = "debug")]
impl std::fmt::Debug for BoxDt {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_tuple("BoxDt").field(&self.0).finish()
    }
}

impl BoxDt {
    /// Returns a new `BoxDt` wrapper around the provided type.
    pub fn new<T>(t: T) -> Self
    where
        T: DataType,
    {
        Self(Box::new(t))
    }

    /// Returns the inner `Box<dyn DataType>`.
    pub fn into_inner(self) -> Box<dyn DataType> {
        self.0
    }
}

impl Deref for BoxDt {
    type Target = dyn DataType;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for BoxDt {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T> FromDataType<T> for BoxDt
where
    T: DataType,
{
    fn from(t: T) -> BoxDt {
        BoxDt(Box::new(t))
    }
}

impl<T> BoxDataTypeDowncast<T> for BoxDt
where
    T: DataType,
{
    fn downcast_ref(&self) -> Option<&T> {
        self.0.downcast_ref::<T>()
    }

    fn downcast_mut(&mut self) -> Option<&mut T> {
        self.0.downcast_mut::<T>()
    }
}

impl DataTypeWrapper for BoxDt {
    fn type_name(&self) -> TypeNameLit {
        DataType::type_name(&*self.0)
    }

    fn clone(&self) -> Self {
        Self(self.0.clone())
    }

    #[cfg(feature = "debug")]
    fn debug(&self) -> &dyn std::fmt::Debug {
        &self.0
    }

    fn inner(&self) -> &dyn DataType {
        &self.0
    }
}

#[cfg(test)]
mod tests {
    use std::ops::{Deref, DerefMut};

    use crate::untagged::{BoxDataTypeDowncast, DataTypeWrapper};

    use super::BoxDt;

    #[test]
    fn clone() {
        let box_dt = BoxDt::new(1u32);
        let mut box_dt_clone = Clone::clone(&box_dt);

        *BoxDataTypeDowncast::<u32>::downcast_mut(&mut box_dt_clone).unwrap() = 2;

        assert_eq!(
            Some(1u32),
            BoxDataTypeDowncast::<u32>::downcast_ref(&box_dt).copied()
        );
        assert_eq!(
            Some(2u32),
            BoxDataTypeDowncast::<u32>::downcast_ref(&box_dt_clone).copied()
        );
    }

    #[cfg(not(feature = "debug"))]
    #[test]
    fn debug() {
        let box_dt = BoxDt::new(1u32);

        assert_eq!(r#"BoxDt("..")"#, format!("{box_dt:?}"));
    }

    #[cfg(feature = "debug")]
    #[test]
    fn debug() {
        let box_dt = BoxDt::new(1u32);

        assert_eq!("BoxDt(1)", format!("{box_dt:?}"));
    }

    #[test]
    fn deref() {
        let box_dt = BoxDt::new(1u32);
        let _data_type = Deref::deref(&box_dt);
    }

    #[test]
    fn deref_mut() {
        let mut box_dt = BoxDt::new(1u32);
        let _data_type = DerefMut::deref_mut(&mut box_dt);
    }

    #[test]
    fn serialize() -> Result<(), serde_yaml::Error> {
        let box_dt = BoxDt::new(1u32);
        let data_type_wrapper: &dyn DataTypeWrapper = &box_dt;

        assert_eq!("1\n", serde_yaml::to_string(data_type_wrapper)?);
        Ok(())
    }
}