Skip to content

Using std::sync::RwLock as state for from_fn_with_state ends up in a "trait not satisfied error" #2458

Answered by davidpdrsn
FelipeLema asked this question in Q&A
Discussion options

You must be logged in to vote

It's a bug in rust. It thinks the lock guard is held longer than it actually is. Adding an extra scope fixes it

async fn my_layer(
    extract::State(s): extract::State<StorageHandle>,
    req: extract::Request,
    next: Next,
) -> Result<response::Response, StatusCode> {
    // ↓ removing this one line cleans up the error
    {
        let _s_readonly = s.read().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    }

    Ok(next.run(req).await)
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by FelipeLema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants