-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Switch to Axum's
FromRef
for custom state (#250)
Our previous approach to allowing consumers to provide custom state/context was to embed it as a field in Roadster's `AppContext`. This is not how Axum recommends libraries support custom state, which makes it difficult (impossible?) to integrate Roadster with other Axum libraries. The recommended approach is to keep the state generic, and add a type constraint to require Roadster's state to be able to be retrieved from the custom state using the [FromRef](https://docs.rs/axum/latest/axum/extract/derive.FromRef.html) trait. See the following for more details: https://docs.rs/axum/latest/axum/extract/struct.State.html#for-library-authors One example of an integration that was blocked by our non-recommended approach: Leptos requires the app's state provide Leptos's state using `FromRef`. In order to support Leptos with our previous approach, we would have needed to add the Leptos state directly to Roadster's `AppContext`. This adds a dependency on Roadster for consumers who may want to use Leptos, and also adds a direct dependency between Roadster and Leptos (though, it would be optional behind a feature flag). This maybe could work in the short term, but this approach doesn't scale to other libraries -- Roadster would need to follow a similar approach for all other libraries consumers may want to use. Instead, it's better to simply follow Axum's recommendation to provide maximum flexibility to consumers.
- Loading branch information
1 parent
04632d8
commit 65f5db4
Showing
52 changed files
with
937 additions
and
752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use axum::extract::FromRef; | ||
use roadster::app::context::AppContext; | ||
|
||
pub type CustomAppContext = (); | ||
|
||
pub type AppState = AppContext<CustomAppContext>; | ||
#[derive(Clone, FromRef)] | ||
pub struct AppState { | ||
pub app_context: AppContext, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.