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
160
161
162
163
164
165
166
167
168
169
use crate::typ::{Type, RawType};
use crate::ValueBox;

use core::marker::PhantomData;

#[repr(transparent)]
///Value type
pub struct Value<T> {
    inner: ValueBox,
    _typ: PhantomData<T>
}

impl Value<RawType> {
    #[inline]
    ///Attempts downcast self into specified type
    pub fn try_downcast<O: Type>(self) -> Result<Box<O>, Self> {
        match self.inner.downcast() {
            Ok(res) => Ok(res),
            Err(inner) => Err(Self::new_inner(inner)),
        }
    }

    #[inline]
    ///Attempts to downcast self into concrete type
    pub fn try_downcast_ref<O: Type>(&self) -> Option<&O> {
        self.inner.downcast_ref()
    }

    #[inline]
    ///Attempts to downcast self into concrete type
    pub fn try_downcast_mut<O: Type>(&mut self) -> Option<&mut O> {
        self.inner.downcast_mut()
    }
}

impl<T: Type> Value<T> {
    #[inline(always)]
    ///Creates new raw Value trusting user to specify correct type
    pub unsafe fn new(inner: ValueBox) -> Self {
        Self::new_inner(inner)
    }

    #[inline(always)]
    pub(crate) fn new_inner(inner: ValueBox) -> Self {
        Self {
            inner,
            _typ: PhantomData,
        }
    }

    #[inline(always)]
    pub(crate) fn new_inner_ref(inner: &ValueBox) -> &Self {
        unsafe {
            core::mem::transmute(inner)
        }
    }

    #[inline(always)]
    pub(crate) fn new_inner_mut(inner: &mut ValueBox) -> &mut Self {
        unsafe {
            core::mem::transmute(inner)
        }
    }

    #[inline(always)]
    ///Creates instance from concrete type
    pub fn from_boxed(inner: Box<T>) -> Self {
        Self::new_inner(inner)
    }

    #[inline]
    ///Downcasts self into concrete type
    pub fn downcast(self) -> Box<T> {
        //dum dum no specialization
        if T::id() == RawType::id() {
            panic!("Raw box cannot use this method")
        }

        match self.inner.downcast() {
            Ok(res) => res,
            Err(_) => unreach!(),
        }
    }


    #[inline]
    ///Downcasts self into concrete type
    pub fn downcast_ref(&self) -> &T {
        //dum dum no specialization
        if T::id() == RawType::id() {
            panic!("Raw box cannot use this method")
        }

        match self.inner.downcast_ref() {
            Some(res) => res,
            None => unreach!(),
        }
    }

    #[inline]
    ///Downcasts self into concrete type
    pub fn downcast_mut(&mut self) -> &mut T {
        //dum dum no specialization
        if T::id() == RawType::id() {
            panic!("Raw box cannot use this method")
        }

        match self.inner.downcast_mut() {
            Some(res) => res,
            None => unreach!(),
        }
    }

    #[inline(always)]
    ///Access underlying untyped pointer
    pub fn as_raw(&self) -> &ValueBox {
        &self.inner
    }

    #[inline(always)]
    ///Access underlying untyped pointer
    pub fn as_raw_mut(&mut self) -> &mut ValueBox {
        &mut self.inner
    }


    #[inline(always)]
    ///Access underlying untyped pointer
    pub fn into_raw(self) -> ValueBox {
        self.inner
    }
}

impl<T: Type> AsRef<T> for Value<T> {
    #[inline(always)]
    fn as_ref(&self) -> &T {
        self.downcast_ref()
    }
}

impl<T: Type> AsMut<T> for Value<T> {
    #[inline(always)]
    fn as_mut(&mut self) -> &mut T {
        self.downcast_mut()
    }
}

impl<T: Type> core::ops::Deref for Value<T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        self.downcast_ref()
    }
}

impl<T: Type> core::ops::DerefMut for Value<T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.downcast_mut()
    }
}

impl<T: Type> Into<ValueBox> for Value<T> {
    #[inline(always)]
    fn into(self) -> ValueBox {
        self.into_raw()
    }
}