Skip to content

Commit 2ae030c

Browse files
committed
Add spawn_future and spawn_future_local convenience functions
A couple of gtk-rs apps currently use utility functions to make spawning futures less verbose. See: - https://docs.rs/gtk-macros/0.3.0/gtk_macros/macro.spawn.html - https://gitlab.gnome.org/GNOME/loupe/-/blob/main/src/util/mod.rs#L251 Exposing these functions via glib should fulfill that need.
1 parent 70f9d7d commit 2ae030c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

glib/src/functions.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,30 @@ pub fn file_open_tmp(
260260
}
261261
}
262262
}
263+
264+
// rustdoc-stripper-ignore-next
265+
/// Spawn a new infallible `Future` on the thread-default main context.
266+
///
267+
/// This can be called from any thread and will execute the future from the thread
268+
/// where main context is running, e.g. via a `MainLoop`.
269+
pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send + 'static>(
270+
f: F,
271+
) -> crate::JoinHandle<R> {
272+
let ctx = crate::MainContext::ref_thread_default();
273+
ctx.spawn(f)
274+
}
275+
276+
// rustdoc-stripper-ignore-next
277+
/// Spawn a new infallible `Future` on the thread-default main context.
278+
///
279+
/// The given `Future` does not have to be `Send`.
280+
///
281+
/// This can be called only from the thread where the main context is running, e.g.
282+
/// from any other `Future` that is executed on this main context, or after calling
283+
/// `with_thread_default` or `acquire` on the main context.
284+
pub fn spawn_future_local<R: 'static, F: std::future::Future<Output = R> + 'static>(
285+
f: F,
286+
) -> crate::JoinHandle<R> {
287+
let ctx = crate::MainContext::ref_thread_default();
288+
ctx.spawn_local(f)
289+
}

0 commit comments

Comments
 (0)