@@ -39,22 +39,45 @@ macro_rules! object {
39
39
}
40
40
}
41
41
42
- impl <$( $t $( : $b) ?) ,* > :: core:: cmp:: PartialEq for $name<$( $t) ,* > {
42
+ // Objective-C equality has approximately the same semantics as Rust
43
+ // equality (although less aptly specified).
44
+ //
45
+ // At the very least, equality is _expected_ to be symmetric and
46
+ // transitive, and that's about the best we can do.
47
+ //
48
+ // `T: PartialEq` bound added because e.g. `NSArray` does deep
49
+ // (instead of shallow) equality comparisons.
50
+ //
51
+ // See also https://nshipster.com/equality/
52
+ impl <$( $t: :: core:: cmp:: PartialEq $( + $b) ?) ,* > :: core:: cmp:: PartialEq for $name<$( $t) ,* > {
53
+ #[ inline]
43
54
fn eq( & self , other: & Self ) -> bool {
44
55
use $crate:: INSObject ;
45
56
self . is_equal( other)
46
57
}
47
58
}
48
59
49
- impl <$( $t $( : $b) ?) ,* > :: core:: cmp:: Eq for $name<$( $t) ,* > { }
60
+ // Most types' equality is reflexive.
61
+ //
62
+ // `T: Eq` bound added to prevent e.g. `NSValue<f32>` from being `Eq`
63
+ // (even though `[NAN isEqual: NAN]` is true in Objective-C).
64
+ impl <$( $t: :: core:: cmp:: Eq $( + $b) ?) ,* > :: core:: cmp:: Eq for $name<$( $t) ,* > { }
50
65
51
- impl <$( $t $( : $b) ?) ,* > :: core:: hash:: Hash for $name<$( $t) ,* > {
66
+ // Hashing in Objective-C has the exact same requirement as in Rust:
67
+ //
68
+ // > If two objects are equal (as determined by the isEqual: method),
69
+ // > they must have the same hash value.
70
+ //
71
+ // See https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418859-hash
72
+ impl <$( $t: :: core:: hash:: Hash $( + $b) ?) ,* > :: core:: hash:: Hash for $name<$( $t) ,* > {
73
+ #[ inline]
52
74
fn hash<H : :: core:: hash:: Hasher >( & self , state: & mut H ) {
53
75
use $crate:: INSObject ;
54
76
self . hash_code( ) . hash( state) ;
55
77
}
56
78
}
57
79
80
+ // TODO: Consider T: Debug bound
58
81
impl <$( $t $( : $b) ?) ,* > :: core:: fmt:: Debug for $name<$( $t) ,* > {
59
82
fn fmt( & self , f: & mut :: core:: fmt:: Formatter <' _>) -> :: core:: fmt:: Result {
60
83
use $crate:: { INSObject , INSString } ;
0 commit comments