How Axum found the bug in my code?

⚓ Rust    📅 2025-10-30    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 33 days ago. The information described in this article may have changed.
fn api_router() -> Router {
    let mut r0 = Router::new();
    r0 = r0.route("/ips", get(get_managed_list));
    r0
}


pub async fn x(reload: bool) -> std::sync::MutexGuard<'static, Vec<i32>> {
    static WTF: Mutex<Vec<i32>> = Mutex::new(vec![]);

    let mut g = WTF.lock().unwrap();
    if reload || g.len() == 0 {
        // if let Ok(v) = pg::load_tracked().await { do_something_with(&mut *g, v); }
    }
    g
}

async fn get_managed_list() -> Result<axum::Json<Value>, StatusCode> {
    let g = x(true).await;
    todo!()
}

As mentioned above: when I comment out if let Ok(v) = pg::load_tracked().await {}
the program compiles; but once I uncomment it, compilation fails with the error:

the trait `Handler<_, _>` is not implemented for fn item 
`fn() -> impl Future<Output = std::result::Result<axum::Json<JsonValue>, axum::http::StatusCode>> {get_managed_list}`

After carefully examining the code (really painfully line by line, since I had no idea what was actually wrong), I found that there was a bug in my code:

pub async fn x(reload: bool) -> std::sync::MutexGuard<'static, Vec<i32>> {
    static WTF: Mutex<Vec<i32>> = Mutex::new(vec![]);


    if reload || WTF.lock().unwrap().len() == 0 {
        if let Ok(v) = pg::load_tracked().await { 
            v 
        } else { 
            vec![] 
        }
    } else { 
        vec![]
    }
    
    let mut g = WTF.lock().unwrap();
    if v.len() > 0 { 
        do_something_with(&mut *g, v);
    }
    g
}

And after that, the code compiles.

My question is: how did Axum manage to detect this problem?
From a type system perspective, it seems like the compiler shouldn’t have been able to catch it that way.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed