Skip to main content

cxx_gen/syntax/
impls.rs

1use crate::syntax::{
2    Array, ExternFn, Include, Lifetimes, Ptr, Receiver, Ref, Signature, SliceRef, Ty1, Type, Var,
3};
4use std::hash::{Hash, Hasher};
5use std::mem;
6use std::ops::{Deref, DerefMut};
7
8impl PartialEq for Include {
9    fn eq(&self, other: &Self) -> bool {
10        let Include {
11            cfg: _,
12            path,
13            kind,
14            begin_span: _,
15            end_span: _,
16        } = self;
17        let Include {
18            cfg: _,
19            path: path2,
20            kind: kind2,
21            begin_span: _,
22            end_span: _,
23        } = other;
24        path == path2 && kind == kind2
25    }
26}
27
28impl Deref for ExternFn {
29    type Target = Signature;
30
31    fn deref(&self) -> &Self::Target {
32        &self.sig
33    }
34}
35
36impl DerefMut for ExternFn {
37    fn deref_mut(&mut self) -> &mut Self::Target {
38        &mut self.sig
39    }
40}
41
42impl Hash for Type {
43    fn hash<H: Hasher>(&self, state: &mut H) {
44        mem::discriminant(self).hash(state);
45        match self {
46            Type::Ident(t) => t.hash(state),
47            Type::RustBox(t) => t.hash(state),
48            Type::UniquePtr(t) => t.hash(state),
49            Type::SharedPtr(t) => t.hash(state),
50            Type::WeakPtr(t) => t.hash(state),
51            Type::Ref(t) => t.hash(state),
52            Type::Ptr(t) => t.hash(state),
53            Type::Str(t) => t.hash(state),
54            Type::RustVec(t) => t.hash(state),
55            Type::CxxVector(t) => t.hash(state),
56            Type::Fn(t) => t.hash(state),
57            Type::SliceRef(t) => t.hash(state),
58            Type::Array(t) => t.hash(state),
59            Type::Void(_) => {}
60        }
61    }
62}
63
64impl Eq for Type {}
65
66impl PartialEq for Type {
67    fn eq(&self, other: &Self) -> bool {
68        match (self, other) {
69            (Type::Ident(lhs), Type::Ident(rhs)) => lhs == rhs,
70            (Type::RustBox(lhs), Type::RustBox(rhs)) => lhs == rhs,
71            (Type::UniquePtr(lhs), Type::UniquePtr(rhs)) => lhs == rhs,
72            (Type::SharedPtr(lhs), Type::SharedPtr(rhs)) => lhs == rhs,
73            (Type::WeakPtr(lhs), Type::WeakPtr(rhs)) => lhs == rhs,
74            (Type::Ref(lhs), Type::Ref(rhs)) => lhs == rhs,
75            (Type::Str(lhs), Type::Str(rhs)) => lhs == rhs,
76            (Type::RustVec(lhs), Type::RustVec(rhs)) => lhs == rhs,
77            (Type::CxxVector(lhs), Type::CxxVector(rhs)) => lhs == rhs,
78            (Type::Fn(lhs), Type::Fn(rhs)) => lhs == rhs,
79            (Type::SliceRef(lhs), Type::SliceRef(rhs)) => lhs == rhs,
80            (Type::Void(_), Type::Void(_)) => true,
81            (_, _) => false,
82        }
83    }
84}
85
86impl Eq for Lifetimes {}
87
88impl PartialEq for Lifetimes {
89    fn eq(&self, other: &Self) -> bool {
90        let Lifetimes {
91            lt_token: _,
92            lifetimes,
93            gt_token: _,
94        } = self;
95        let Lifetimes {
96            lt_token: _,
97            lifetimes: lifetimes2,
98            gt_token: _,
99        } = other;
100        lifetimes.iter().eq(lifetimes2)
101    }
102}
103
104impl Hash for Lifetimes {
105    fn hash<H: Hasher>(&self, state: &mut H) {
106        let Lifetimes {
107            lt_token: _,
108            lifetimes,
109            gt_token: _,
110        } = self;
111        lifetimes.len().hash(state);
112        for lifetime in lifetimes {
113            lifetime.hash(state);
114        }
115    }
116}
117
118impl Eq for Ty1 {}
119
120impl PartialEq for Ty1 {
121    fn eq(&self, other: &Self) -> bool {
122        let Ty1 {
123            name,
124            langle: _,
125            inner,
126            rangle: _,
127        } = self;
128        let Ty1 {
129            name: name2,
130            langle: _,
131            inner: inner2,
132            rangle: _,
133        } = other;
134        name == name2 && inner == inner2
135    }
136}
137
138impl Hash for Ty1 {
139    fn hash<H: Hasher>(&self, state: &mut H) {
140        let Ty1 {
141            name,
142            langle: _,
143            inner,
144            rangle: _,
145        } = self;
146        name.hash(state);
147        inner.hash(state);
148    }
149}
150
151impl Eq for Ref {}
152
153impl PartialEq for Ref {
154    fn eq(&self, other: &Self) -> bool {
155        let Ref {
156            pinned,
157            ampersand: _,
158            lifetime,
159            mutable,
160            inner,
161            pin_tokens: _,
162            mutability: _,
163        } = self;
164        let Ref {
165            pinned: pinned2,
166            ampersand: _,
167            lifetime: lifetime2,
168            mutable: mutable2,
169            inner: inner2,
170            pin_tokens: _,
171            mutability: _,
172        } = other;
173        pinned == pinned2 && lifetime == lifetime2 && mutable == mutable2 && inner == inner2
174    }
175}
176
177impl Hash for Ref {
178    fn hash<H: Hasher>(&self, state: &mut H) {
179        let Ref {
180            pinned,
181            ampersand: _,
182            lifetime,
183            mutable,
184            inner,
185            pin_tokens: _,
186            mutability: _,
187        } = self;
188        pinned.hash(state);
189        lifetime.hash(state);
190        mutable.hash(state);
191        inner.hash(state);
192    }
193}
194
195impl Eq for Ptr {}
196
197impl PartialEq for Ptr {
198    fn eq(&self, other: &Ptr) -> bool {
199        let Ptr {
200            star: _,
201            mutable,
202            inner,
203            mutability: _,
204        } = self;
205        let Ptr {
206            star: _,
207            mutable: mutable2,
208            inner: inner2,
209            mutability: _,
210        } = other;
211        mutable == mutable2 && inner == inner2
212    }
213}
214
215impl Hash for Ptr {
216    fn hash<H: Hasher>(&self, state: &mut H) {
217        let Ptr {
218            star: _,
219            mutable,
220            inner,
221            mutability: _,
222        } = self;
223        mutable.hash(state);
224        inner.hash(state);
225    }
226}
227
228impl Eq for SliceRef {}
229
230impl PartialEq for SliceRef {
231    fn eq(&self, other: &Self) -> bool {
232        let SliceRef {
233            ampersand: _,
234            lifetime,
235            mutable,
236            bracket: _,
237            inner,
238            mutability: _,
239        } = self;
240        let SliceRef {
241            ampersand: _,
242            lifetime: lifetime2,
243            mutable: mutable2,
244            bracket: _,
245            inner: inner2,
246            mutability: _,
247        } = other;
248        lifetime == lifetime2 && mutable == mutable2 && inner == inner2
249    }
250}
251
252impl Hash for SliceRef {
253    fn hash<H: Hasher>(&self, state: &mut H) {
254        let SliceRef {
255            ampersand: _,
256            lifetime,
257            mutable,
258            bracket: _,
259            inner,
260            mutability: _,
261        } = self;
262        lifetime.hash(state);
263        mutable.hash(state);
264        inner.hash(state);
265    }
266}
267
268impl Eq for Array {}
269
270impl PartialEq for Array {
271    fn eq(&self, other: &Self) -> bool {
272        let Array {
273            bracket: _,
274            inner,
275            semi_token: _,
276            len,
277            len_token: _,
278        } = self;
279        let Array {
280            bracket: _,
281            inner: inner2,
282            semi_token: _,
283            len: len2,
284            len_token: _,
285        } = other;
286        inner == inner2 && len == len2
287    }
288}
289
290impl Hash for Array {
291    fn hash<H: Hasher>(&self, state: &mut H) {
292        let Array {
293            bracket: _,
294            inner,
295            semi_token: _,
296            len,
297            len_token: _,
298        } = self;
299        inner.hash(state);
300        len.hash(state);
301    }
302}
303
304impl Eq for Signature {}
305
306impl PartialEq for Signature {
307    fn eq(&self, other: &Self) -> bool {
308        let Signature {
309            asyncness,
310            unsafety,
311            fn_token: _,
312            generics: _,
313            kind,
314            args,
315            ret,
316            throws,
317            paren_token: _,
318            throws_tokens: _,
319        } = self;
320        let Signature {
321            asyncness: asyncness2,
322            unsafety: unsafety2,
323            fn_token: _,
324            generics: _,
325            kind: kind2,
326            args: args2,
327            ret: ret2,
328            throws: throws2,
329            paren_token: _,
330            throws_tokens: _,
331        } = other;
332        asyncness.is_some() == asyncness2.is_some()
333            && unsafety.is_some() == unsafety2.is_some()
334            && kind == kind2
335            && ret == ret2
336            && throws == throws2
337            && args.len() == args2.len()
338            && args.iter().zip(args2).all(|(arg, arg2)| {
339                let Var {
340                    cfg: _,
341                    doc: _,
342                    attrs: _,
343                    visibility: _,
344                    name: _,
345                    colon_token: _,
346                    ty,
347                } = arg;
348                let Var {
349                    cfg: _,
350                    doc: _,
351                    attrs: _,
352                    visibility: _,
353                    name: _,
354                    colon_token: _,
355                    ty: ty2,
356                } = arg2;
357                ty == ty2
358            })
359    }
360}
361
362impl Hash for Signature {
363    fn hash<H: Hasher>(&self, state: &mut H) {
364        let Signature {
365            asyncness,
366            unsafety,
367            fn_token: _,
368            generics: _,
369            kind,
370            args,
371            ret,
372            throws,
373            paren_token: _,
374            throws_tokens: _,
375        } = self;
376        asyncness.is_some().hash(state);
377        unsafety.is_some().hash(state);
378        kind.hash(state);
379        for arg in args {
380            let Var {
381                cfg: _,
382                doc: _,
383                attrs: _,
384                visibility: _,
385                name: _,
386                colon_token: _,
387                ty,
388            } = arg;
389            ty.hash(state);
390        }
391        ret.hash(state);
392        throws.hash(state);
393    }
394}
395
396impl Eq for Receiver {}
397
398impl PartialEq for Receiver {
399    fn eq(&self, other: &Self) -> bool {
400        let Receiver {
401            pinned,
402            ampersand: _,
403            lifetime,
404            mutable,
405            var: _,
406            colon_token: _,
407            ty,
408            shorthand: _,
409            pin_tokens: _,
410            mutability: _,
411        } = self;
412        let Receiver {
413            pinned: pinned2,
414            ampersand: _,
415            lifetime: lifetime2,
416            mutable: mutable2,
417            var: _,
418            colon_token: _,
419            ty: ty2,
420            shorthand: _,
421            pin_tokens: _,
422            mutability: _,
423        } = other;
424        pinned == pinned2 && lifetime == lifetime2 && mutable == mutable2 && ty == ty2
425    }
426}
427
428impl Hash for Receiver {
429    fn hash<H: Hasher>(&self, state: &mut H) {
430        let Receiver {
431            pinned,
432            ampersand: _,
433            lifetime,
434            mutable,
435            var: _,
436            colon_token: _,
437            ty,
438            shorthand: _,
439            pin_tokens: _,
440            mutability: _,
441        } = self;
442        pinned.hash(state);
443        lifetime.hash(state);
444        mutable.hash(state);
445        ty.hash(state);
446    }
447}