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
macro_rules! doc_concat {
( $( #[doc = $doc:expr] $( $thing:tt )* )* ) => ( $( #[doc = $doc] $( $thing )* )* );
}
macro_rules! partial_eq_string {
($id:ty) => {
partial_eq_string!(@imp, $id, str);
partial_eq_string!(@imp, $id, &str);
partial_eq_string!(@imp, $id, String);
partial_eq_string!(@imp, str, $id);
partial_eq_string!(@imp, &str, $id);
partial_eq_string!(@imp, String, $id);
};
(@imp, $l:ty, $r:ty) => {
impl ::std::cmp::PartialEq<$r> for $l {
fn eq(&self, other: &$r) -> bool {
::std::convert::AsRef::<str>::as_ref(self)
== ::std::convert::AsRef::<str>::as_ref(other)
}
}
}
}
macro_rules! common_impls {
($id:ty, $try_from:ident, $desc:literal) => {
impl $id {
doc_concat! {
#[doc = concat!("Creates a string slice from this `", stringify!($id), "`")]
pub fn as_str(&self) -> &str {
&self.full_id
}
}
doc_concat! {
#[doc = concat!("Creates a byte slice from this `", stringify!($id), "`")]
pub fn as_bytes(&self) -> &[u8] {
self.full_id.as_bytes()
}
}
}
impl ::std::convert::AsRef<str> for $id {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl ::std::convert::From<$id> for ::std::string::String {
fn from(id: $id) -> Self {
id.full_id.into()
}
}
impl ::std::convert::TryFrom<&str> for $id {
type Error = crate::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
$try_from(s)
}
}
impl ::std::convert::TryFrom<String> for $id {
type Error = crate::Error;
fn try_from(s: String) -> Result<Self, Self::Error> {
$try_from(s)
}
}
impl ::std::fmt::Display for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl ::std::cmp::PartialEq for $id {
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}
impl ::std::cmp::Eq for $id {}
impl ::std::cmp::PartialOrd for $id {
fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
::std::cmp::PartialOrd::partial_cmp(self.as_str(), other.as_str())
}
}
impl ::std::cmp::Ord for $id {
fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
::std::cmp::Ord::cmp(self.as_str(), other.as_str())
}
}
impl ::std::hash::Hash for $id {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
#[cfg(feature = "serde")]
impl ::serde::Serialize for $id {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
#[cfg(feature = "serde")]
impl<'de> ::serde::Deserialize<'de> for $id {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
crate::deserialize_id(deserializer, $desc)
}
}
partial_eq_string!($id);
};
}