From cc1fa873dd55f715931293303432dd7e365cd562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 18 Jan 2023 13:03:06 +0200 Subject: [PATCH] glib: Add helper functions to spawn a non-`Send` future on the current thread-default `MainContext` --- glib/src/lib.rs | 2 +- glib/src/main_context_futures.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/glib/src/lib.rs b/glib/src/lib.rs index b9f8e27c0671..9ff6042f44af 100644 --- a/glib/src/lib.rs +++ b/glib/src/lib.rs @@ -199,7 +199,7 @@ pub use self::bridged_logging::{rust_log_handler, GlibLogger, GlibLoggerDomain, pub mod subclass; mod main_context_futures; -pub use main_context_futures::{JoinError, JoinHandle}; +pub use main_context_futures::{spawn, spawn_with_priority, JoinError, JoinHandle}; mod source_futures; pub use self::source_futures::*; diff --git a/glib/src/main_context_futures.rs b/glib/src/main_context_futures.rs index 6d00ac02b9b4..76f1c80826d3 100644 --- a/glib/src/main_context_futures.rs +++ b/glib/src/main_context_futures.rs @@ -552,6 +552,26 @@ impl Spawn for MainContext { } } +/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`]. +/// +/// Panics if there is no thread-default main context on this thread. +pub fn spawn + 'static>(f: F) -> JoinHandle { + spawn_with_priority(crate::PRIORITY_DEFAULT, f) +} + +/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`] with a +/// non-default priority. +/// +/// Panics if there is no thread-default main context on this thread. +pub fn spawn_with_priority + 'static>( + priority: Priority, + f: F, +) -> JoinHandle { + let ctx = + MainContext::thread_default().expect("no thread default `MainContext` on this thread"); + ctx.spawn_local_with_priority(priority, f) +} + impl LocalSpawn for MainContext { fn spawn_local_obj(&self, f: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> { let (tx, _) = oneshot::channel();