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
170
171
172
173
174
175
176
177
178
179
macro_rules! shared_impls {
    (pointer
        mod=$mod_:ident
        new_type=$tconst:ident[$($lt:lifetime),*][$($ty:ident),*]
            $(extra[$($ex_ty:ident),* $(,)* ])?
            $(where [ $($where_:tt)* ])? ,
        original_type=$original:ident,
    ) => {

        mod $mod_{
            use super::*;

            use std::{
                fmt::{self,Display},
                ops::Deref,
            };

            use serde::{Deserialize,Serialize,Deserializer,Serializer};

            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Deref
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            {
                type Target=T;

                fn deref(&self)->&Self::Target{
                    unsafe{
                        &*self.data()
                    }
                }
            }

            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Display
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                $($ty:Display,)*
            {
                fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
                    Display::fmt(&**self,f)
                }
            }


            impl<'de,$($lt,)* $($ty,)* $($($ex_ty,)*)?> Deserialize<'de>
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                T:Deserialize<'de>,
            {
                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
                where
                    D: Deserializer<'de>
                {
                    T::deserialize(deserializer)
                        .map(Self::new)
                }
            }

            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Serialize
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                T:Serialize
            {
                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
                where
                    S: Serializer
                {
                    (&**self).serialize(serializer)
                }
            }

            shared_impls!{
                mod=$mod_
                new_type=$tconst[$($lt),*][$($ty),*]
                    $(extra[$($ex_ty),*])?
                    $(where [ $($where_)* ])? ,
                original_type=$original,
            }
        }

    };
    (
        mod=$mod_:ident
        new_type=$tconst:ident[$($lt:lifetime),*][$($ty:ident),*]
            $(extra[$($ex_ty:ident),* $(,)*])?
            $(constrained[$($c_ty:ty),* $(,)*])?
            $(where [ $($where_:tt)* ])? ,
        original_type=$original:ident,
    ) => {
        mod $mod_{
            use std::{
                cmp::{PartialEq,Eq,Ord,PartialOrd,Ordering},
                fmt::{self,Debug},
                hash::{Hash,Hasher},
            };

            use super::*;
            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Debug
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                $($ty:Debug,)*
                $($($c_ty:Debug,)*)?
                $($($where_)*)?
            {
                fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
                    Debug::fmt(&**self,f)
                }
            }

            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Eq
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                $($ty:Eq,)*
                $($($c_ty:Eq,)*)?
                $($($where_)*)?
            {}


            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> PartialEq
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                $($ty:PartialEq,)*
                $($($c_ty:PartialEq,)*)?
                $($($where_)*)?
            {
                fn eq(&self, other: &Self) -> bool{
                    ::std::ptr::eq(&**self,&**other)||
                    (&**self)==(&**other)
                }
            }


            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Ord
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                $($ty:Ord,)*
                $($($c_ty:Ord,)*)?
                $($($where_)*)?
            {
                fn cmp(&self, other: &Self) -> Ordering{
                    if ::std::ptr::eq(&**self,&**other) {
                        return Ordering::Equal;
                    }
                    (&**self).cmp(&**other)
                }
            }


            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> PartialOrd
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                $($ty:PartialOrd,)*
                $($($c_ty:PartialOrd,)*)?
                $($($where_)*)?
            {
                fn partial_cmp(&self, other: &Self) -> Option<Ordering>{
                    if ::std::ptr::eq(&**self,&**other) {
                        return Some(Ordering::Equal);
                    }
                    (&**self).partial_cmp(&**other)
                }
            }


            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Hash
                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
            where
                $($ty:Hash,)*
                $($($c_ty:Hash,)*)?
                $($($where_)*)?
            {
                fn hash<H>(&self, state: &mut H)
                where
                    H: Hasher
                {
                    (&**self).hash(state)
                }
            }
        }
    };
}