@@ -4,7 +4,9 @@ use std::{future::Future, pin::Pin, ptr};
4
4
5
5
use glib:: { prelude:: * , subclass:: prelude:: * , thread_guard:: ThreadGuard , translate:: * , Error } ;
6
6
7
- use crate :: { prelude:: * , AsyncInitable , AsyncResult , Cancellable , GioFutureResult , LocalTask } ;
7
+ use crate :: {
8
+ AsyncInitable , AsyncResult , Cancellable , CancellableFuture , GioFutureResult , LocalTask ,
9
+ } ;
8
10
9
11
pub trait AsyncInitableImpl : ObjectImpl {
10
12
fn init_future (
@@ -100,12 +102,12 @@ unsafe extern "C" fn async_initable_init_async<T: AsyncInitableImpl>(
100
102
) {
101
103
let instance = & * ( initable as * mut T :: Instance ) ;
102
104
let imp = instance. imp ( ) ;
103
- let cancellable = from_glib_borrow :: < _ , Option < Cancellable > > ( cancellable) ;
105
+ let cancellable = Option :: < Cancellable > :: from_glib_none ( cancellable) ;
104
106
105
107
let task = callback. map ( |callback| {
106
108
let task = LocalTask :: new (
107
109
Some ( imp. obj ( ) . unsafe_cast_ref :: < glib:: Object > ( ) ) ,
108
- cancellable. as_ref ( ) . as_ref ( ) ,
110
+ cancellable. as_ref ( ) ,
109
111
move |task, obj| {
110
112
let result: * mut crate :: ffi:: GAsyncResult =
111
113
task. upcast_ref :: < AsyncResult > ( ) . to_glib_none ( ) . 0 ;
@@ -120,17 +122,11 @@ unsafe extern "C" fn async_initable_init_async<T: AsyncInitableImpl>(
120
122
121
123
glib:: MainContext :: ref_thread_default ( ) . spawn_local ( async move {
122
124
let io_priority = from_glib ( io_priority) ;
123
- let res = if let Some ( cancellable) = cancellable. as_ref ( ) {
124
- futures_util:: future:: select (
125
- imp. init_future ( io_priority) ,
126
- Box :: pin ( async {
127
- cancellable. future ( ) . await ;
128
- cancellable. set_error_if_cancelled ( )
129
- } ) ,
130
- )
131
- . await
132
- . factor_first ( )
133
- . 0
125
+ let res = if let Some ( cancellable) = cancellable {
126
+ CancellableFuture :: new ( imp. init_future ( io_priority) , cancellable)
127
+ . await
128
+ . map_err ( |cancelled| cancelled. into ( ) )
129
+ . and_then ( |res| res)
134
130
} else {
135
131
imp. init_future ( io_priority) . await
136
132
} ;
@@ -174,6 +170,7 @@ unsafe extern "C" fn async_initable_init_finish<T: AsyncInitableImpl>(
174
170
#[ cfg( test) ]
175
171
mod tests {
176
172
use super :: * ;
173
+ use crate :: prelude:: * ;
177
174
178
175
pub mod imp {
179
176
use std:: cell:: Cell ;
0 commit comments