@@ -160,6 +160,111 @@ impl Refcount for MainContext {
160
160
161
161
g_impl_boxed_type_for_ref ! ( MainContext , gobject:: g_main_context_get_type) ;
162
162
163
+ #[ repr( C ) ]
164
+ pub struct Source < Callback = SourceCallback > {
165
+ raw : ffi:: GSource ,
166
+ phantom_data : marker:: PhantomData < Callback >
167
+ }
168
+
169
+ #[ repr( C ) ]
170
+ pub struct AttachedSource < Callback > {
171
+ raw : ffi:: GSource ,
172
+ phantom_data : marker:: PhantomData < Callback >
173
+ }
174
+
175
+ unsafe impl < C > Send for Source < C > where C : Into < RawCallback > { }
176
+
177
+ unsafe impl < C > Send for AttachedSource < C > where C : Into < RawCallback > { }
178
+ unsafe impl < C > Sync for AttachedSource < C > where C : Into < RawCallback > { }
179
+
180
+ macro_rules! common_source_impls {
181
+ ( $name: ident) => {
182
+ unsafe impl <C > Wrapper for $name<C > {
183
+ type Raw = ffi:: GSource ;
184
+ }
185
+
186
+ impl <C > Refcount for $name<C > {
187
+ unsafe fn inc_ref( & self ) {
188
+ ffi:: g_source_ref( self . as_mut_ptr( ) ) ;
189
+ }
190
+ unsafe fn dec_ref( & self ) {
191
+ ffi:: g_source_unref( self . as_mut_ptr( ) ) ;
192
+ }
193
+ }
194
+ }
195
+ }
196
+
197
+ common_source_impls ! ( Source ) ;
198
+ common_source_impls ! ( AttachedSource ) ;
199
+
200
+ impl < C > Source < C > where C : Into < RawCallback > {
201
+ pub fn set_callback ( & self , callback : C )
202
+ {
203
+ let raw: RawCallback = callback. into ( ) ;
204
+ unsafe {
205
+ ffi:: g_source_set_callback ( self . as_mut_ptr ( ) ,
206
+ raw. func , raw. data , Some ( raw. destroy ) ) ;
207
+ }
208
+ mem:: forget ( raw) ;
209
+ }
210
+
211
+ pub fn set_priority ( & self , priority : gint ) {
212
+ unsafe {
213
+ ffi:: g_source_set_priority ( self . as_mut_ptr ( ) , priority) ;
214
+ }
215
+ }
216
+ }
217
+
218
+ impl < C > Ref < Source < C > > {
219
+ pub fn attach ( self , ctx : & MainContext ) -> Ref < AttachedSource < C > > {
220
+ unsafe {
221
+ let source_ptr = self . as_mut_ptr ( ) ;
222
+ ffi:: g_source_attach ( source_ptr, ctx. as_mut_ptr ( ) ) ;
223
+ mem:: forget ( self ) ;
224
+ Ref :: from_raw ( source_ptr)
225
+ }
226
+ }
227
+ }
228
+
229
+ impl < C > AttachedSource < C > {
230
+ #[ inline]
231
+ pub fn as_source ( & self ) -> & Source < C > {
232
+ unsafe { wrap:: from_raw ( self . as_ptr ( ) ) }
233
+ }
234
+
235
+ pub fn destroy ( & self ) {
236
+ unsafe { ffi:: g_source_destroy ( self . as_mut_ptr ( ) ) }
237
+ }
238
+ }
239
+
240
+ impl < C > convert:: AsRef < Source < C > > for AttachedSource < C > {
241
+ #[ inline]
242
+ fn as_ref ( & self ) -> & Source < C > {
243
+ self . as_source ( )
244
+ }
245
+ }
246
+
247
+ pub fn idle_source_new ( ) -> Ref < Source > {
248
+ unsafe {
249
+ let source = ffi:: g_idle_source_new ( ) ;
250
+ Ref :: from_raw ( source)
251
+ }
252
+ }
253
+
254
+ pub fn timeout_source_new ( interval : guint ) -> Ref < Source > {
255
+ unsafe {
256
+ let source = ffi:: g_timeout_source_new ( interval) ;
257
+ Ref :: from_raw ( source)
258
+ }
259
+ }
260
+
261
+ pub fn timeout_source_new_seconds ( interval : guint ) -> Ref < Source > {
262
+ unsafe {
263
+ let source = ffi:: g_timeout_source_new_seconds ( interval) ;
264
+ Ref :: from_raw ( source)
265
+ }
266
+ }
267
+
163
268
#[ repr( C ) ]
164
269
pub struct MainLoop {
165
270
raw : ffi:: GMainLoop
0 commit comments