Debian Codegraph @

src/common/tools/hash_type.rs

raw symbols SCIP
1 use std::{
2 cmp::Ordering,
3 fmt::{self, Display, Formatter},
4 hash::{Hash, Hasher},
5 str::FromStr,
6 };
7
8 use proc_macro2::Span;
9 use quote::ToTokens;
10 use syn::{spanned::Spanned, Path, Type};
11
12 #[derive(Debug, Clone)]
13 pub(crate) struct HashType(String, Span);
14
15 impl PartialEq for HashType {
16 #[inline]
17 fn eq(&self, other: &Self) -> bool {
18 self.0.eq(&other.0)
19 }
20 }
21
22 impl Eq for HashType {}
23
24 impl PartialOrd for HashType {
25 #[inline]
26 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
27 Some(self.cmp(other))
28 }
29 }
30
31 impl Ord for HashType {
32 #[inline]
33 fn cmp(&self, other: &Self) -> Ordering {
34 self.0.cmp(&other.0)
35 }
36 }
37
38 impl Hash for HashType {
39 #[inline]
40 fn hash<H: Hasher>(&self, state: &mut H) {
41 Hash::hash(&self.0, state);
42 }
43 }
44
45 impl Display for HashType {
46 #[inline]
47 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
48 Display::fmt(&self.0.replace("& '", "&'"), f)
49 }
50 }
51
52 impl From<Type> for HashType {
53 #[inline]
54 fn from(value: Type) -> Self {
55 Self::from(&value)
56 }
57 }
58
59 impl From<&Type> for HashType {
60 #[inline]
61 fn from(value: &Type) -> Self {
62 Self(value.into_token_stream().to_string(), value.span())
63 }
64 }
65
66 impl From<Path> for HashType {
67 #[inline]
68 fn from(value: Path) -> Self {
69 Self::from(&value)
70 }
71 }
72
73 impl From<&Path> for HashType {
74 #[inline]
75 fn from(value: &Path) -> Self {
76 Self(value.into_token_stream().to_string(), value.span())
77 }
78 }
79
80 #[allow(dead_code)]
81 impl HashType {
82 #[inline]
83 pub(crate) fn to_type(&self) -> Type {
84 syn::parse_str(self.0.as_str()).unwrap()
85 }
86
87 #[inline]
88 pub(crate) fn span(&self) -> Span {
89 self.1
90 }
91 }
92
93 impl ToTokens for HashType {
94 #[inline]
95 fn to_tokens(&self, token_stream: &mut proc_macro2::TokenStream) {
96 let ty = proc_macro2::TokenStream::from_str(self.0.as_str()).unwrap();
97
98 token_stream.extend(ty);
99 }
100 }