task::wait_for_local can now return a value

This commit is contained in:
Lucy 2022-02-07 11:13:13 -05:00
parent 8f170b9147
commit 8c376e1df2
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1

View file

@ -14,11 +14,14 @@ pub fn spawn_local<F: Future<Output = ()> + 'static>(future: F) {
gtk4::glib::MainContext::default().spawn_local(future);
}
pub async fn wait_for_local<F: Future<Output = ()> + 'static>(future: F) {
let (tx, rx) = oneshot::channel::<()>();
pub async fn wait_for_local<O, F>(future: F) -> Option<O>
where
O: Send + 'static,
F: Future<Output = O> + Send + 'static,
{
let (tx, rx) = oneshot::channel::<O>();
gtk4::glib::MainContext::default().spawn_local(async move {
future.await;
let _ = tx.send(());
std::mem::drop(tx.send(future.await));
});
std::mem::drop(rx.await);
rx.await.ok()
}