This repository was archived by the owner on Mar 1, 2019. It is now read-only.
File tree 4 files changed +33
-31
lines changed
4 files changed +33
-31
lines changed Original file line number Diff line number Diff line change 1
1
// adaptation of https://gitlab.gnome.org/GNOME/glib/blob/master/gio/tests/gapplication-example-cmdline2.c
2
2
use std:: mem;
3
- use std:: ops;
4
3
use std:: ptr;
5
4
use std:: sync:: { Once , ONCE_INIT } ;
6
5
@@ -17,6 +16,8 @@ use glib::prelude::*;
17
16
use glib:: translate:: * ;
18
17
19
18
extern crate gio_subclass;
19
+
20
+ #[ macro_use]
20
21
extern crate gobject_subclass;
21
22
22
23
use gio_subclass:: application:: * ;
@@ -131,21 +132,8 @@ glib_wrapper! {
131
132
}
132
133
}
133
134
134
- // TODO: This one should probably get a macro
135
- impl ops:: Deref for SimpleApplication {
136
- type Target = imp:: SimpleApplication ;
135
+ gobject_subclass_deref ! ( SimpleApplication , Application ) ;
137
136
138
- fn deref ( & self ) -> & Self :: Target {
139
- unsafe {
140
- let base: Application = from_glib_borrow ( self . to_glib_none ( ) . 0 ) ;
141
- let imp = base. get_impl ( ) ;
142
- let imp = imp. downcast_ref :: < imp:: SimpleApplication > ( ) . unwrap ( ) ;
143
- // Cast to a raw pointer to get us an appropriate lifetime: the compiler
144
- // can't know that the lifetime of base is the same as the one of self
145
- & * ( imp as * const imp:: SimpleApplication )
146
- }
147
- }
148
- }
149
137
150
138
impl SimpleApplication {
151
139
pub fn new < ' a , I : Into < Option < & ' a str > > > (
Original file line number Diff line number Diff line change
1
+ #[ macro_export]
2
+ macro_rules! gobject_subclass_deref(
3
+ ( $name: ident, $base: ident) => {
4
+ gobject_subclass_deref!( $name, imp:: $name, $base) ;
5
+ } ;
6
+
7
+ ( $name: ident, $target: ty, $base: ident) => {
8
+ use std:: ops:: Deref ;
9
+
10
+ impl Deref for $name {
11
+ type Target = $target;
12
+
13
+ fn deref( & self ) -> & Self :: Target {
14
+ unsafe {
15
+ let base: $base = from_glib_borrow( self . to_glib_none( ) . 0 ) ;
16
+ let imp = base. get_impl( ) ;
17
+ let imp = imp. downcast_ref:: <$target>( ) . unwrap( ) ;
18
+ // Cast to a raw pointer to get us an appropriate lifetime: the compiler
19
+ // can't know that the lifetime of base is the same as the one of self
20
+ & * ( imp as * const $target)
21
+ }
22
+ }
23
+ }
24
+ }
25
+ ) ;
Original file line number Diff line number Diff line change @@ -23,3 +23,6 @@ pub mod guard;
23
23
pub mod properties;
24
24
#[ macro_use]
25
25
pub mod object;
26
+
27
+ #[ macro_use]
28
+ mod deref;
Original file line number Diff line number Diff line change 7
7
// except according to those terms.
8
8
9
9
use std:: mem;
10
- use std:: ops;
11
10
use std:: ptr;
12
11
use std:: sync:: { Once , ONCE_INIT } ;
13
12
@@ -19,6 +18,7 @@ extern crate glib;
19
18
use glib:: prelude:: * ;
20
19
use glib:: translate:: * ;
21
20
21
+ #[ macro_use]
22
22
extern crate gobject_subclass;
23
23
use gobject_subclass:: object:: * ;
24
24
@@ -172,21 +172,7 @@ impl SimpleObject {
172
172
}
173
173
}
174
174
175
- // TODO: This one should probably get a macro
176
- impl ops:: Deref for SimpleObject {
177
- type Target = imp:: SimpleObject ;
178
-
179
- fn deref ( & self ) -> & Self :: Target {
180
- unsafe {
181
- let base: Object = from_glib_borrow ( self . to_glib_none ( ) . 0 ) ;
182
- let imp = base. get_impl ( ) ;
183
- let imp = imp. downcast_ref :: < imp:: SimpleObject > ( ) . unwrap ( ) ;
184
- // Cast to a raw pointer to get us an appropriate lifetime: the compiler
185
- // can't know that the lifetime of base is the same as the one of self
186
- & * ( imp as * const imp:: SimpleObject )
187
- }
188
- }
189
- }
175
+ gobject_subclass_deref ! ( SimpleObject , Object ) ;
190
176
191
177
#[ test]
192
178
fn test_create ( ) {
You can’t perform that action at this time.
0 commit comments