Skip to content

Commit 9125737

Browse files
authored
Merge pull request #1201 from Hofer-Julian/add-spawn-functions
Add `spawn_future` and `spawn_future_local` convenience functions
2 parents 70f9d7d + 2ae030c commit 9125737

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)