Skip to content

Commit cc1fa87

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

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,26 @@ impl Spawn for MainContext {
552552
}
553553
}
554554

555+
/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`].
556+
///
557+
/// Panics if there is no thread-default main context on this thread.
558+
pub fn spawn<R: 'static, F: Future<Output = R> + 'static>(f: F) -> JoinHandle<R> {
559+
spawn_with_priority(crate::PRIORITY_DEFAULT, f)
560+
}
561+
562+
/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`] with a
563+
/// non-default priority.
564+
///
565+
/// Panics if there is no thread-default main context on this thread.
566+
pub fn spawn_with_priority<R: 'static, F: Future<Output = R> + 'static>(
567+
priority: Priority,
568+
f: F,
569+
) -> JoinHandle<R> {
570+
let ctx =
571+
MainContext::thread_default().expect("no thread default `MainContext` on this thread");
572+
ctx.spawn_local_with_priority(priority, f)
573+
}
574+
555575
impl LocalSpawn for MainContext {
556576
fn spawn_local_obj(&self, f: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
557577
let (tx, _) = oneshot::channel();

0 commit comments

Comments
 (0)