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+
122/// TODO
223///
324/// # Safety
930macro_rules! object {
1031 (
1132 $( #[ $m: meta] ) *
12- unsafe $v: vis struct $name: ident: $inherits : ty $( ; ) ?
33+ unsafe $v: vis struct $name: ident: $( $inheritance_chain : ty) ,+ $( ; ) ?
1334 ) => {
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+ }
1540 } ;
1641 (
1742 $( #[ $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) * {
1959 $( $p: ident: $pty: ty, ) *
2060 }
2161 ) => {
@@ -85,7 +125,7 @@ macro_rules! object {
85125 }
86126 }
87127
88- // TODO: AsRef and AsMut
128+ __impl_as_ref_as_mut! ( $name<$ ( $t $ ( : $b ) ? ) , * > , $inherits , $ ( $inheritance_rest , ) * ) ;
89129
90130 // Objective-C equality has approximately the same semantics as Rust
91131 // equality (although less aptly specified).
@@ -100,13 +140,7 @@ macro_rules! object {
100140 impl <$( $t: :: core:: cmp:: PartialEq $( + $b) ?) ,* > :: core:: cmp:: PartialEq for $name<$( $t) ,* > {
101141 #[ inline]
102142 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)
110144 }
111145 }
112146
0 commit comments