Skip to content

from_fn_with_state does not compile #3353

Answered by jplatte
harshRn asked this question in Q&A
Discussion options

You must be logged in to vote

Aha! I think with that hint (that removing the state parameter fixes the issue) I've found your problem: The error message above refers to the type that isn't a valid middleware as axum::middleware::FromFn<fn(State<AppState>, tower_sessions::Session, axum::http::Request<Body>, Next) -> impl Future<Output = Response<Body>> {middleware::middleware}, Arc<AppState>, Route, _>. The Arc<AppState> in there is a pretty clear sign that the second parameter you're passing to from_fn_with_state is actually an Arc<AppState>, not AppState.

You'll have to either change your function signature to use State<Arc<AppState>> (if AppState itself is not cheap to clone, I would recommend this), or pass a clone…

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@jplatte
Comment options

@harshRn
Comment options

@harshRn
Comment options

@jplatte
Comment options

Answer selected by harshRn
@harshRn
Comment options

@jplatte
Comment options

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
Converted from issue

This discussion was converted from issue #3352 on May 18, 2025 09:41.