@@ -106,6 +106,39 @@ pub enum Encoding<'a> {
106
106
// NSLog(@"Encoding: %s", @encode(const int*)); // -> r^i
107
107
}
108
108
109
+ impl Encoding < ' _ > {
110
+ /// Check if one encoding is equivalent to another.
111
+ pub fn equivalent_to ( & self , other : & Self ) -> bool {
112
+ // For now, because we don't allow representing qualifiers
113
+ self == other
114
+ }
115
+
116
+ /// Check if an encoding is equivalent to the given string representation.
117
+ pub fn equivalent_to_str ( & self , s : & str ) -> bool {
118
+ // if the given encoding can be successfully removed from the start
119
+ // and an empty string remains, they were fully equivalent!
120
+ if let Some ( res) = self . equivalent_to_start_of_str ( s) {
121
+ res. is_empty ( )
122
+ } else {
123
+ false
124
+ }
125
+ }
126
+
127
+ /// Check if an encoding is equivalent to the start of the given string
128
+ /// representation.
129
+ ///
130
+ /// If it is equivalent, the remaining part of the string is returned.
131
+ /// Otherwise this returns [`None`].
132
+ pub fn equivalent_to_start_of_str < ' a > ( & self , s : & ' a str ) -> Option < & ' a str > {
133
+ // strip leading qualifiers
134
+ let s = s. trim_start_matches ( parse:: QUALIFIERS ) ;
135
+
136
+ // TODO: Allow missing/"?" names in structs and unions?
137
+
138
+ parse:: rm_enc_prefix ( s, self )
139
+ }
140
+ }
141
+
109
142
impl fmt:: Display for Encoding < ' _ > {
110
143
fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
111
144
use Encoding :: * ;
@@ -162,15 +195,37 @@ impl fmt::Display for Encoding<'_> {
162
195
}
163
196
}
164
197
198
+ // TODO: Deprecate and remove these PartialEq impls
199
+
200
+ /// Partial equality between an [`Encoding`] and a [`str`].
201
+ ///
202
+ /// Using this is heavily discouraged, since it is not transitive; use
203
+ /// [`Encoding::equivalent_to_str`] instead for more correct semantics.
165
204
impl PartialEq < str > for Encoding < ' _ > {
205
+ /// Using this is discouraged.
166
206
fn eq ( & self , other : & str ) -> bool {
167
- parse:: eq_enc ( other, self )
207
+ self . equivalent_to_str ( other)
208
+ }
209
+
210
+ /// Using this is discouraged.
211
+ fn ne ( & self , other : & str ) -> bool {
212
+ !self . eq ( other)
168
213
}
169
214
}
170
215
216
+ /// Partial equality between an [`Encoding`] and a [`str`].
217
+ ///
218
+ /// Using this is heavily discouraged, since it is not transitive; use
219
+ /// [`Encoding::equivalent_to_str`] instead for more correct semantics.
171
220
impl PartialEq < Encoding < ' _ > > for str {
221
+ /// Using this is discouraged.
172
222
fn eq ( & self , other : & Encoding < ' _ > ) -> bool {
173
- parse:: eq_enc ( self , other)
223
+ other. equivalent_to_str ( self )
224
+ }
225
+
226
+ /// Using this is discouraged.
227
+ fn ne ( & self , other : & Encoding < ' _ > ) -> bool {
228
+ !self . eq ( other)
174
229
}
175
230
}
176
231
@@ -183,14 +238,14 @@ mod tests {
183
238
fn test_array_display ( ) {
184
239
let e = Encoding :: Array ( 12 , & Encoding :: Int ) ;
185
240
assert_eq ! ( e. to_string( ) , "[12i]" ) ;
186
- assert_eq ! ( & e , "[12i]" ) ;
241
+ assert ! ( e . equivalent_to_str ( "[12i]" ) ) ;
187
242
}
188
243
189
244
#[ test]
190
245
fn test_pointer_display ( ) {
191
246
let e = Encoding :: Pointer ( & Encoding :: Int ) ;
192
247
assert_eq ! ( e. to_string( ) , "^i" ) ;
193
- assert_eq ! ( & e , "^i" ) ;
248
+ assert ! ( e . equivalent_to_str ( "^i" ) ) ;
194
249
}
195
250
196
251
#[ test]
@@ -205,7 +260,7 @@ mod tests {
205
260
#[ test]
206
261
fn test_int_display ( ) {
207
262
assert_eq ! ( Encoding :: Int . to_string( ) , "i" ) ;
208
- assert_eq ! ( & Encoding :: Int , "i" ) ;
263
+ assert ! ( Encoding :: Int . equivalent_to_str ( "i" ) ) ;
209
264
}
210
265
211
266
#[ test]
@@ -221,7 +276,7 @@ mod tests {
221
276
fn test_struct_display ( ) {
222
277
let s = Encoding :: Struct ( "CGPoint" , & [ Encoding :: Char , Encoding :: Int ] ) ;
223
278
assert_eq ! ( s. to_string( ) , "{CGPoint=ci}" ) ;
224
- assert_eq ! ( & s , "{CGPoint=ci}" ) ;
279
+ assert ! ( s . equivalent_to_str ( "{CGPoint=ci}" ) ) ;
225
280
}
226
281
227
282
#[ test]
@@ -235,7 +290,7 @@ mod tests {
235
290
fn test_union_display ( ) {
236
291
let u = Encoding :: Union ( "Onion" , & [ Encoding :: Char , Encoding :: Int ] ) ;
237
292
assert_eq ! ( u. to_string( ) , "(Onion=ci)" ) ;
238
- assert_eq ! ( & u , "(Onion=ci)" ) ;
293
+ assert ! ( u . equivalent_to_str ( "(Onion=ci)" ) ) ;
239
294
}
240
295
241
296
#[ test]
0 commit comments