1
+ macro_rules! __impl_as_ref_as_mut {
2
+ ( $name: ident<$( $t: ident $( : $b: ident) ?) ,* >, ) => { } ;
3
+ ( $name: ident<$( $t: ident $( : $b: ident) ?) ,* >, $item: ty, $( $tail: ty, ) * ) => {
4
+ impl <$( $t $( : $b) ?) ,* > AsRef <$item> for $name<$( $t) ,* > {
5
+ #[ inline]
6
+ fn as_ref( & self ) -> & $item {
7
+ & * * self
8
+ }
9
+ }
10
+
11
+ impl <$( $t $( : $b) ?) ,* > AsMut <$item> for $name<$( $t) ,* > {
12
+ #[ inline]
13
+ fn as_mut( & mut self ) -> & mut $item {
14
+ & mut * * self
15
+ }
16
+ }
17
+
18
+ __impl_as_ref_as_mut!( $name<$( $t $( : $b) ?) ,* >, $( $tail, ) * ) ;
19
+ } ;
20
+ }
21
+
1
22
/// TODO
2
23
///
3
24
/// # Safety
9
30
macro_rules! object {
10
31
(
11
32
$( #[ $m: meta] ) *
12
- unsafe $v: vis struct $name: ident: $inherits : ty $( ; ) ?
33
+ unsafe $v: vis struct $name: ident: $( $inheritance_chain : ty) ,+ $( ; ) ?
13
34
) => {
14
- object!( $( #[ $m] ) * unsafe $v struct $name<>: $inherits { } ) ;
35
+ object! {
36
+ @__inner
37
+ $( #[ $m] ) *
38
+ unsafe $v struct $name<>: $( $inheritance_chain, ) + :: objc2:: runtime:: Object { }
39
+ }
15
40
} ;
16
41
(
17
42
$( #[ $m: meta] ) *
18
- unsafe $v: vis struct $name: ident<$( $t: ident $( : $b: ident) ?) ,* >: $inherits: ty {
43
+ unsafe $v: vis struct $name: ident<$( $t: ident $( : $b: ident) ?) ,* >: $( $inheritance_chain: ty) ,+ {
44
+ $( $p: ident: $pty: ty, ) *
45
+ }
46
+ ) => {
47
+ object! {
48
+ @__inner
49
+ $( #[ $m] ) *
50
+ unsafe $v struct $name<$( $t $( : $b) ?) ,* >: $( $inheritance_chain, ) + :: objc2:: runtime:: Object {
51
+ $( $p: $pty, ) *
52
+ }
53
+ }
54
+ } ;
55
+ (
56
+ @__inner
57
+ $( #[ $m: meta] ) *
58
+ unsafe $v: vis struct $name: ident<$( $t: ident $( : $b: ident) ?) ,* >: $inherits: ty $( , $inheritance_rest: ty) * {
19
59
$( $p: ident: $pty: ty, ) *
20
60
}
21
61
) => {
@@ -85,7 +125,7 @@ macro_rules! object {
85
125
}
86
126
}
87
127
88
- // TODO: AsRef and AsMut
128
+ __impl_as_ref_as_mut! ( $name<$ ( $t $ ( : $b ) ? ) , * > , $inherits , $ ( $inheritance_rest , ) * ) ;
89
129
90
130
// Objective-C equality has approximately the same semantics as Rust
91
131
// equality (although less aptly specified).
@@ -100,13 +140,7 @@ macro_rules! object {
100
140
impl <$( $t: :: core:: cmp:: PartialEq $( + $b) ?) ,* > :: core:: cmp:: PartialEq for $name<$( $t) ,* > {
101
141
#[ inline]
102
142
fn eq( & self , other: & Self ) -> bool {
103
- use :: objc2:: MessageReceiver ;
104
- use $crate:: NSObject ;
105
- // "downgrading" to NSObject to work around generic
106
- // downgrading not having been set up yet.
107
- // TODO: Fix this.
108
- let other: & NSObject = unsafe { & * other. as_raw_receiver( ) . cast( ) } ;
109
- self . is_equal( other)
143
+ self . is_equal( & * other)
110
144
}
111
145
}
112
146
0 commit comments