Skip to content

Commit ab7525f

Browse files
authored
Merge pull request #876 from pbor/async-init-canc
gio: simplify async initable
2 parents d738b2a + 3a00734 commit ab7525f

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

gio/src/subclass/async_initable.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::{future::Future, pin::Pin, ptr};
44

55
use glib::{prelude::*, subclass::prelude::*, thread_guard::ThreadGuard, translate::*, Error};
66

7-
use crate::{prelude::*, AsyncInitable, AsyncResult, Cancellable, GioFutureResult, LocalTask};
7+
use crate::{
8+
AsyncInitable, AsyncResult, Cancellable, CancellableFuture, GioFutureResult, LocalTask,
9+
};
810

911
pub trait AsyncInitableImpl: ObjectImpl {
1012
fn init_future(
@@ -100,12 +102,12 @@ unsafe extern "C" fn async_initable_init_async<T: AsyncInitableImpl>(
100102
) {
101103
let instance = &*(initable as *mut T::Instance);
102104
let imp = instance.imp();
103-
let cancellable = from_glib_borrow::<_, Option<Cancellable>>(cancellable);
105+
let cancellable = Option::<Cancellable>::from_glib_none(cancellable);
104106

105107
let task = callback.map(|callback| {
106108
let task = LocalTask::new(
107109
Some(imp.obj().unsafe_cast_ref::<glib::Object>()),
108-
cancellable.as_ref().as_ref(),
110+
cancellable.as_ref(),
109111
move |task, obj| {
110112
let result: *mut crate::ffi::GAsyncResult =
111113
task.upcast_ref::<AsyncResult>().to_glib_none().0;
@@ -120,17 +122,11 @@ unsafe extern "C" fn async_initable_init_async<T: AsyncInitableImpl>(
120122

121123
glib::MainContext::ref_thread_default().spawn_local(async move {
122124
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)
134130
} else {
135131
imp.init_future(io_priority).await
136132
};
@@ -174,6 +170,7 @@ unsafe extern "C" fn async_initable_init_finish<T: AsyncInitableImpl>(
174170
#[cfg(test)]
175171
mod tests {
176172
use super::*;
173+
use crate::prelude::*;
177174

178175
pub mod imp {
179176
use std::cell::Cell;

0 commit comments

Comments
 (0)