Skip to content

Extracting generic substate #2814

Answered by mhutter
mhutter asked this question in Q&A
Jul 1, 2024 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

Okay, I figured it out.

While I was on the right track, what was missing was a trait bound X: AsRef<AppState<X>>.

So this works:

// Trait & Implementations
trait Greeter: Clone + Send + Sync + 'static { /* ... */ }
impl Greeter for WorldGreeter { /* ... */ }
impl Greeter for UniverseGreeter { /* ... */ }

// Handler that uses Greeter
async fn homepage<G: Greeter>(State(greeter): State<G>) -> { /* ... */ }

// AppState that contains Greeter
struct AppState<G: Greeter> {
    greeter: G,
}

// FromRef implementations
impl FromRef<AppState<UniverseGreeter>> for UniverseGreeter { /* ... */ }
impl FromRef<AppState<WorldGreeter>> for WorldGreeter { /* ... */ }

// Putting it all together
async fn 

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@mhutter
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by mhutter
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