Skip to content

Conversation

BiagioFesta
Copy link
Contributor

Tested code:

#[glib::async_test]
async fn my_test() {
    use std::time::Duration;

    glib::timeout_future(Duration::from_secs(1)).await;
}

Before this PR

fn my_test() {
    let main_ctx = glib::MainContext::new();
    main_ctx
        .with_thread_default(move || {
            main_ctx
                .block_on(async {
                    use std::time::Duration;
                    glib::timeout_future(Duration::from_secs(1)).await;
                })
        })
}
error[E0308]: mismatched types
  --> src/main.rs:12:1
   |
12 | #[glib::async_test]
   | ^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), BoolError>`
   |
   = note: expected unit type `()`
                   found enum `Result<(), BoolError>`

AND

error[E0505]: cannot move out of `main_ctx` because it is borrowed
  --> src/main.rs:23:30
   |
21 |     let main_ctx = glib::MainContext::new();
   |         -------- binding `main_ctx` declared here
22 |     main_ctx
   |     -------- borrow of `main_ctx` occurs here
23 |         .with_thread_default(move || {
   |          ------------------- ^^^^^^^ move out of `main_ctx` occurs here
   |          |
   |          borrow later used by call
24 |             main_ctx
   |             -------- move occurs due to use in closure
   |
help: consider cloning the value if the performance cost is acceptable
   |
22 |     main_ctx.clone()
   |             ++++++++

After this PR

fn my_test() {
    let main_ctx = glib::MainContext::new();
    main_ctx
        .with_thread_default(|| {
            main_ctx
                .block_on(async {
                    use std::time::Duration;
                    glib::timeout_future(Duration::from_secs(1)).await;
                })
        })
        .expect("cannot set thread default main context for test")
}

@BiagioFesta BiagioFesta force-pushed the wip/bfesta/async-test-fix branch from 9912826 to 71f1920 Compare August 12, 2025 23:32
@BiagioFesta BiagioFesta changed the title glib-macros: unwrap result when setting default thread context glib-macros/async_test: Fixes unwrap result and move context Aug 12, 2025
@sdroege sdroege merged commit b7559d3 into gtk-rs:main Aug 13, 2025
47 of 48 checks passed
@sdroege sdroege added the needs-backport PR needs backporting to the current stable branch label Aug 13, 2025
@sdroege sdroege added backported PR was backported to the current stable branch and removed needs-backport PR needs backporting to the current stable branch labels Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backported PR was backported to the current stable branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants