forked from LukeMathWalker/pavex
-
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: Reshape singleton dependencies (LukeMathWalker#298)
This is a fairly big change to Pavex's dependency injection system. Before this PR, all singleton dependencies were automatically added as inputs to `build_application_state` if they had no registered constructor. There was no way to explicitly tell Pavex, "Please, add this as input to `build_application_state`, I'll build it for you". After this PR, singleton dependencies are no longer special-cased. If there's no constructor for them, you'll get an error. But you now have two options for resolving it: register a constructor or mark them as "prebuilt types." Prebuilt types (if used) will be added as inputs to `build_application_state` and then used as if they had been built by Pavex. The same logic applies to constructed types, e.g. cloning strategy. This brings along a few other changes: - You can no longer register a type with a non-'static lifetime parameter (implicit or explicit) as a singleton. - Singletons are no longer forced to be cloned - We validate upfront that all types with a `CloneIfNecessary` strategy implement `Clone` - This surfaced a bug where `BufferedBody` was marked as `CloneIfNecessary` but didn't implement `Clone`. This is now fixed.
- Loading branch information
1 parent
38eb192
commit 00b1822
Showing
175 changed files
with
4,665 additions
and
1,164 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
doc_examples/guide/cookies/installation/missing_process_config.snap
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[31m[1mERROR[0m[39m: | ||
[31m×[0m I can't find a constructor for `biscotti::ProcessorConfig`. | ||
[31m│[0m I need an instance of `biscotti::ProcessorConfig` to | ||
[31m│[0m invoke your constructor, `<pavex::cookie::Processor as | ||
[31m│[0m std::convert::From::<pavex::cookie::ProcessorConfig>>::from`. | ||
[31m│[0m | ||
[31m│[0m ╭─[[36;1;4m../../../../../libs/pavex/src/cookie/kit.rs[0m:80:1] | ||
[31m│[0m [2m80[0m │ .error_handler(f!(super::errors::InjectResponseCookiesError::into_response)); | ||
[31m│[0m [2m81[0m │ [35;1m╭[0m[35;1m─[0m[35;1m▶[0m let processor = Constructor::singleton(f!(<super::Processor as std::convert::From< | ||
[31m│[0m [2m82[0m │ [35;1m│[0m super::ProcessorConfig, | ||
[31m│[0m [2m83[0m │ [35;1m├[0m[35;1m─[0m[35;1m▶[0m >>::from)) | ||
[31m│[0m · [35;1m╰[0m[35;1m───[0m[35;1m─[0m [35;1mThe constructor was registered here[0m | ||
[31m│[0m [2m84[0m │ .ignore(Lint::Unused); | ||
[31m│[0m ╰──── | ||
[31m│[0m [36mhelp:[0m Register a constructor for `biscotti::ProcessorConfig`. | ||
[31m│[0m [36m help: [0mAlternatively, use `Blueprint::prebuilt` to add a new input | ||
[31m│[0m parameter of type `biscotti::ProcessorConfig` to the (generated) | ||
[31m│[0m `build_application_state`. | ||
|
||
Error: NonZeroExitCode(NonZeroExitCode { code: 1, command: "pavex [...] generate [...]" }) | ||
error: Failed to run `cookie_installation`, the code generator for package `cookie_installation_server_sdk` |
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,9 +1,10 @@ | ||
starter_project_folder: "project" | ||
commands: | ||
- command: "cargo px c" | ||
expected_outcome: "success" | ||
- command: "cargo px c -q" | ||
expected_outcome: "failure" | ||
expected_output_at: "missing_process_config.snap" | ||
snippets: | ||
- name: "kit" | ||
source_path: "src/blueprint.rs" | ||
ranges: [ "0..6", "7..8" ] | ||
hl_lines: [ 6 ] | ||
ranges: ["0..6", "7..8"] | ||
hl_lines: [6] |
6 changes: 6 additions & 0 deletions
6
doc_examples/guide/dependency_injection/prebuilt/01-build_state.snap
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
```rust title="server_sdk/src/lib.rs" hl_lines="2" | ||
// [...] | ||
pub async fn build_application_state(v0: di_prebuilt::base::A) -> crate::ApplicationState { | ||
// [...] | ||
} | ||
``` |
Empty file.
8 changes: 8 additions & 0 deletions
8
doc_examples/guide/dependency_injection/prebuilt/project-registration.snap
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
```rust title="src/base/blueprint.rs" hl_lines="4" | ||
// [...] | ||
pub fn blueprint() -> Blueprint { | ||
let mut bp = Blueprint::new(); | ||
bp.prebuilt(t!(super::A)); | ||
// [...] | ||
} | ||
``` |
1 change: 1 addition & 0 deletions
1
doc_examples/guide/dependency_injection/prebuilt/project/.gitignore
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
12 changes: 12 additions & 0 deletions
12
doc_examples/guide/dependency_injection/prebuilt/project/Cargo.toml
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "di_prebuilt" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
pavex = { path = "../../../../../libs/pavex" } | ||
pavex_cli_client = { path = "../../../../../libs/pavex_cli_client" } | ||
cargo_px_env = "0.1" | ||
|
||
[workspace] | ||
members = [".", "server_sdk"] |
10 changes: 10 additions & 0 deletions
10
doc_examples/guide/dependency_injection/prebuilt/project/server_sdk/Cargo.toml
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "di_prebuilt_server_sdk" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[package.metadata.px.generate] | ||
generator_type = "cargo_workspace_binary" | ||
generator_name = "di_prebuilt" | ||
|
||
[dependencies] |
Empty file.
10 changes: 10 additions & 0 deletions
10
doc_examples/guide/dependency_injection/prebuilt/project/src/base/blueprint.rs
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use pavex::blueprint::router::GET; | ||
use pavex::blueprint::Blueprint; | ||
use pavex::{f, t}; | ||
|
||
pub fn blueprint() -> Blueprint { | ||
let mut bp = Blueprint::new(); | ||
bp.prebuilt(t!(super::A)); | ||
bp.route(GET, "/", f!(super::handler)); | ||
bp | ||
} |
7 changes: 7 additions & 0 deletions
7
doc_examples/guide/dependency_injection/prebuilt/project/src/base/handler.rs
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use super::A; | ||
use pavex::response::Response; | ||
|
||
pub fn handler(a: &A) -> Response { | ||
// Handler logic | ||
todo!() | ||
} |
7 changes: 7 additions & 0 deletions
7
doc_examples/guide/dependency_injection/prebuilt/project/src/base/mod.rs
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub use blueprint::blueprint; | ||
pub use handler::handler; | ||
|
||
mod blueprint; | ||
mod handler; | ||
|
||
pub struct A; |
7 changes: 7 additions & 0 deletions
7
doc_examples/guide/dependency_injection/prebuilt/project/src/blueprint.rs
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use pavex::blueprint::Blueprint; | ||
|
||
pub fn blueprint() -> Blueprint { | ||
let mut bp = Blueprint::new(); | ||
bp.nest(crate::base::blueprint()); | ||
bp | ||
} |
7 changes: 7 additions & 0 deletions
7
doc_examples/guide/dependency_injection/prebuilt/project/src/lib.rs
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
pub use blueprint::blueprint; | ||
|
||
pub mod base; | ||
mod blueprint; |
14 changes: 14 additions & 0 deletions
14
doc_examples/guide/dependency_injection/prebuilt/project/src/main.rs
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use std::error::Error; | ||
|
||
use cargo_px_env::generated_pkg_manifest_path; | ||
use pavex_cli_client::Client; | ||
|
||
use di_prebuilt::blueprint; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let generated_dir = generated_pkg_manifest_path()?.parent().unwrap().into(); | ||
Client::new() | ||
.generate(blueprint(), generated_dir) | ||
.execute()?; | ||
Ok(()) | ||
} |
5 changes: 5 additions & 0 deletions
5
doc_examples/guide/dependency_injection/prebuilt/project/src/routes.rs
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use pavex::http::StatusCode; | ||
|
||
pub fn handler() -> StatusCode { | ||
todo!() | ||
} |
16 changes: 16 additions & 0 deletions
16
doc_examples/guide/dependency_injection/prebuilt/tutorial.yml
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
starter_project_folder: "project" | ||
commands: | ||
- command: "cargo px c" | ||
expected_outcome: "success" | ||
snippets: | ||
- name: "registration" | ||
source_path: "src/base/blueprint.rs" | ||
ranges: ["4..7", "9..10"] | ||
hl_lines: [4] | ||
steps: | ||
- patch: "01.patch" | ||
snippets: | ||
- name: "build_state" | ||
source_path: "server_sdk/src/lib.rs" | ||
ranges: ["11..12", "13..14"] | ||
hl_lines: [2] |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# `ApplicationState` | ||
|
||
When generating the [server SDK crate], Pavex examines all the components you registered to determine which [singletons][Lifecycle::Singleton] will be | ||
used at runtime to process incoming requests. | ||
Pavex then generates a type to group them together, named `ApplicationState`. | ||
|
||
## `build_application_state` | ||
|
||
Inside the [server SDK crate], you'll also find a function named [`build_application_state`][build_application_state]. As the name suggest, it returns an instance of `ApplicationState`. | ||
|
||
`build_application_state` takes as input all the types that you marked | ||
as [prebuilt](prebuilt_types.md). | ||
Inside its body, it'll invoke the constructors for all your [singletons][Lifecycle::Singleton] in order to build an instance of `ApplicationState`. | ||
|
||
[Lifecycle::Singleton]: ../../api_reference/pavex/blueprint/constructor/enum.Lifecycle.html#variant.Singleton | ||
[build_application_state]: ../project_structure.md#applicationstate | ||
[server crate]: ../project_structure.md#the-server-crate | ||
[ApplicationState]: ../project_structure.md#applicationstate | ||
[server SDK crate]: ../project_structure.md#the-server-sdk |
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.