abi_stable/
impls.rs

1macro_rules! shared_impls {
2    (pointer
3        mod=$mod_:ident
4        new_type=$tconst:ident[$($lt:lifetime),*][$($ty:ident),*]
5            $(extra[$($ex_ty:ident),* $(,)* ])?
6            $(where [ $($where_:tt)* ])? ,
7        original_type=$original:ident,
8    ) => {
9
10        mod $mod_{
11            use super::*;
12
13            use std::{
14                fmt::{self,Display, Pointer as PointerFmt},
15                ops::Deref,
16            };
17
18            use serde::{Deserialize,Serialize,Deserializer,Serializer};
19
20            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Deref
21                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
22            {
23                type Target=T;
24
25                fn deref(&self)->&Self::Target{
26                    unsafe{
27                        &*self.data()
28                    }
29                }
30            }
31
32            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Display
33                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
34            where
35                $($ty:Display,)*
36            {
37                fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
38                    Display::fmt(&**self,f)
39                }
40            }
41
42            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> PointerFmt
43                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
44            {
45                fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
46                    let ptr: *const _ = &**self;
47                    PointerFmt::fmt(&ptr, f)
48                }
49            }
50
51
52            impl<'de,$($lt,)* $($ty,)* $($($ex_ty,)*)?> Deserialize<'de>
53                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
54            where
55                T:Deserialize<'de>,
56            {
57                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
58                where
59                    D: Deserializer<'de>
60                {
61                    T::deserialize(deserializer)
62                        .map(Self::new)
63                }
64            }
65
66            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Serialize
67                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
68            where
69                T:Serialize
70            {
71                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
72                where
73                    S: Serializer
74                {
75                    (&**self).serialize(serializer)
76                }
77            }
78
79            shared_impls!{
80                mod=$mod_
81                new_type=$tconst[$($lt),*][$($ty),*]
82                    $(extra[$($ex_ty),*])?
83                    $(where [ $($where_)* ])? ,
84                original_type=$original,
85            }
86        }
87
88    };
89    (
90        mod=$mod_:ident
91        new_type=$tconst:ident[$($lt:lifetime),*][$($ty:ident),*]
92            $(extra[$($ex_ty:ident),* $(,)*])?
93            $(constrained[$($c_ty:ty),* $(,)*])?
94            $(where [ $($where_:tt)* ])? ,
95        original_type=$original:ident,
96        $(deref_approach=$deref:tt,)?
97    ) => {
98        mod $mod_{
99            use std::{
100                cmp::{PartialEq,Eq,Ord,PartialOrd,Ordering},
101                fmt::{self,Debug},
102                hash::{Hash,Hasher},
103            };
104
105            use super::*;
106            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Debug
107                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
108            where
109                $($ty:Debug,)*
110                $($($c_ty:Debug,)*)?
111                $($($where_)*)?
112            {
113                fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
114                    Debug::fmt(si_deref!($($deref)? self),f)
115                }
116            }
117
118            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Eq
119                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
120            where
121                $($ty:Eq,)*
122                $($($c_ty:Eq,)*)?
123                $($($where_)*)?
124            {}
125
126
127            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> PartialEq
128                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
129            where
130                $($ty:PartialEq,)*
131                $($($c_ty:PartialEq,)*)?
132                $($($where_)*)?
133            {
134                fn eq(&self, other: &Self) -> bool{
135                    ::std::ptr::eq(si_deref!($($deref)? self),si_deref!($($deref)? other))||
136                    si_deref!($($deref)? self) == si_deref!($($deref)? other)
137                }
138            }
139
140
141            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Ord
142                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
143            where
144                $($ty:Ord,)*
145                $($($c_ty:Ord,)*)?
146                $($($where_)*)?
147            {
148                fn cmp(&self, other: &Self) -> Ordering{
149                    if ::std::ptr::eq(si_deref!($($deref)? self),si_deref!($($deref)? other)) {
150                        return Ordering::Equal;
151                    }
152                    si_deref!($($deref)? self).cmp(si_deref!($($deref)? other))
153                }
154            }
155
156
157            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> PartialOrd
158                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
159            where
160                $($ty:PartialOrd,)*
161                $($($c_ty:PartialOrd,)*)?
162                $($($where_)*)?
163            {
164                fn partial_cmp(&self, other: &Self) -> Option<Ordering>{
165                    if ::std::ptr::eq(si_deref!($($deref)? self),si_deref!($($deref)? other)) {
166                        return Some(Ordering::Equal);
167                    }
168                    si_deref!($($deref)? self).partial_cmp(si_deref!($($deref)? other))
169                }
170            }
171
172
173            impl<$($lt,)* $($ty,)* $($($ex_ty,)*)?> Hash
174                for $tconst<$($lt,)* $($ty,)* $($($ex_ty,)*)?>
175            where
176                $($ty:Hash,)*
177                $($($c_ty:Hash,)*)?
178                $($($where_)*)?
179            {
180                fn hash<H>(&self, state: &mut H)
181                where
182                    H: Hasher
183                {
184                    si_deref!($($deref)? self).hash(state)
185                }
186            }
187        }
188    };
189}
190
191macro_rules! si_deref {
192    ($self:ident) => {
193        &**$self
194    };
195    (double_deref $self:ident) => {
196        &**$self
197    };
198    ((method = $method:ident) $self:ident) => {
199        $self.$method()
200    };
201}