Skip to content

Commit f16b13d

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

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,28 @@ 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.
559+
pub fn spawn<R: 'static, F: Future<Output = R> + 'static>(f: F) -> JoinHandle<R> {
560+
spawn_with_priority(crate::PRIORITY_DEFAULT, f)
561+
}
562+
563+
// rustdoc-stripper-ignore-next
564+
/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`] with a
565+
/// non-default priority.
566+
///
567+
/// Panics if there is no thread-default main context on this thread.
568+
pub fn spawn_with_priority<R: 'static, F: Future<Output = R> + 'static>(
569+
priority: Priority,
570+
f: F,
571+
) -> JoinHandle<R> {
572+
let ctx =
573+
MainContext::thread_default().expect("no thread default `MainContext` on this thread");
574+
ctx.spawn_local_with_priority(priority, f)
575+
}
576+
555577
impl LocalSpawn for MainContext {
556578
fn spawn_local_obj(&self, f: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
557579
let (tx, _) = oneshot::channel();

0 commit comments

Comments
 (0)