Skip to content

Commit

Permalink
glib: Add helper functions to spawn a non-Send future on the (threa…
Browse files Browse the repository at this point in the history
…d-)default `MainContext`
  • Loading branch information
sdroege committed Jan 19, 2023
1 parent e616855 commit 3f5b6cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion glib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
23 changes: 23 additions & 0 deletions glib/src/main_context_futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,29 @@ impl Spawn for MainContext {
}
}

// rustdoc-stripper-ignore-next
/// Spawns an infallible, non-`Send` on the current thread-default [`MainContext`].
///
/// Panics if there is no thread-default main context on this thread and if this thread
/// does not own the global default main context.
pub fn spawn<R: 'static, F: Future<Output = R> + 'static>(f: F) -> JoinHandle<R> {
spawn_with_priority(crate::PRIORITY_DEFAULT, f)
}

// rustdoc-stripper-ignore-next
/// 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 and if this thread
/// does not own the global default main context.
pub fn spawn_with_priority<R: 'static, F: Future<Output = R> + 'static>(
priority: Priority,
f: F,
) -> JoinHandle<R> {
let ctx = MainContext::ref_thread_default();
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();
Expand Down

0 comments on commit 3f5b6cc

Please sign in to comment.