Skip to content

Commit

Permalink
Add spawn_future and spawn_future_local convenience functions
Browse files Browse the repository at this point in the history
A couple of gtk-rs apps currently use utility functions to make spawning futures less verbose.
See:
- https://docs.rs/gtk-macros/0.3.0/gtk_macros/macro.spawn.html
- https://gitlab.gnome.org/GNOME/loupe/-/blob/main/src/util/mod.rs#L251

Exposing these functions via glib should fulfill that need.
  • Loading branch information
Hofer-Julian committed Oct 9, 2023
1 parent 70f9d7d commit 3bc0c85
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions glib/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,30 @@ pub fn file_open_tmp(
}
}
}

// rustdoc-stripper-ignore-next
/// Spawn a new infallible `Future` on the thread-default main context.
///
/// This can be called from any thread and will execute the future from the thread
/// where main context is running, e.g. via a `MainLoop`.
pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send + 'static>(
f: F,
) -> crate::JoinHandle<R> {
let ctx = crate::MainContext::ref_thread_default();
ctx.spawn(f)
}

// rustdoc-stripper-ignore-next
/// Spawn a new infallible `Future` on the thread-default main context.
///
/// The given `Future` does not have to be `Send`.
///
/// This can be called only from the thread where the main context is running, e.g.
/// from any other `Future` that is executed on this main context, or after calling
/// `with_thread_default` or `acquire` on the main context.
pub fn spawn_future_local<R: 'static, F: std::future::Future<Output = R> + 'static>(
f: F,
) -> crate::JoinHandle<R> {
let ctx = crate::MainContext::ref_thread_default();
ctx.spawn_local(f)
}

0 comments on commit 3bc0c85

Please sign in to comment.