Skip to content

Commit 1c7e825

Browse files
committed
glib: Add helper functions to spawn a non-Send future on the (thread-)default MainContext
1 parent e616855 commit 1c7e825

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

glib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub use self::bridged_logging::{rust_log_handler, GlibLogger, GlibLoggerDomain,
199199
pub mod subclass;
200200

201201
mod main_context_futures;
202-
pub use main_context_futures::{JoinError, JoinHandle};
202+
pub use main_context_futures::{spawn, spawn_with_priority, JoinError, JoinHandle};
203203
mod source_futures;
204204
pub use self::source_futures::*;
205205

glib/src/main_context_futures.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,36 @@ impl Spawn for MainContext {
552552
}
553553
}
554554

555+
// rustdoc-stripper-ignore-next
556+
/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`].
557+
///
558+
/// Panics if there is no thread-default main context on this thread and if this thread
559+
/// does not own the global default main context.
560+
pub fn spawn<R: 'static, F: Future<Output = R> + 'static>(f: F) -> JoinHandle<R> {
561+
spawn_with_priority(crate::PRIORITY_DEFAULT, f)
562+
}
563+
564+
// rustdoc-stripper-ignore-next
565+
/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`] with a
566+
/// non-default priority.
567+
///
568+
/// Panics if there is no thread-default main context on this thread and if this thread
569+
/// does not own the global default main context.
570+
pub fn spawn_with_priority<R: 'static, F: Future<Output = R> + 'static>(
571+
priority: Priority,
572+
f: F,
573+
) -> JoinHandle<R> {
574+
let ctx = MainContext::thread_default().unwrap_or_else(|| {
575+
let ctx = MainContext::default();
576+
assert!(
577+
ctx.is_owner(),
578+
"Current thread does not own the default main context and has no thread-default main context",
579+
);
580+
ctx
581+
});
582+
ctx.spawn_local_with_priority(priority, f)
583+
}
584+
555585
impl LocalSpawn for MainContext {
556586
fn spawn_local_obj(&self, f: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
557587
let (tx, _) = oneshot::channel();

0 commit comments

Comments
 (0)