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