-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reject singletons with non-'static lifetime parameters
- Loading branch information
1 parent
57ac60a
commit 6af94d6
Showing
7 changed files
with
97 additions
and
4 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...y_injection/lifecycles/non_static_references_cannot_be_singletons/expectations/stderr.txt
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
3 changes: 1 addition & 2 deletions
3
...pendency_injection/lifecycles/non_static_references_cannot_be_singletons/test_config.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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
description = """Singletons can only depend on other singletons, they can't depend on | ||
request-scoped or transient components""" | ||
description = """Singletons cannot be non-'static references""" | ||
|
||
[expectations] | ||
codegen = "fail" |
16 changes: 16 additions & 0 deletions
16
.../lifecycles/singletons_cannot_have_non_static_lifetime_parameters/expectations/stderr.txt
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 @@ | ||
[31m[1mERROR[0m[39m: | ||
[31m×[0m `app::B<'a>` can't be a singleton because at least one of its lifetime | ||
[31m│[0m parameters isn't `'static`. | ||
[31m│[0m Singletons must be available for as long as the application is running, | ||
[31m│[0m therefore their lifetime must be `'static`. | ||
[31m│[0m | ||
[31m│[0m ╭─[[36;1;4msrc/lib.rs[0m:26:1] | ||
[31m│[0m [2m26[0m │ bp.singleton(f!(self::a)); | ||
[31m│[0m [2m27[0m │ bp.singleton(f!(self::B::new)).clone_if_necessary(); | ||
[31m│[0m · [35;1m ────────┬───────[0m | ||
[31m│[0m · [35;1m╰── The singleton was registered here[0m | ||
[31m│[0m [2m28[0m │ bp.route(GET, "/", f!(self::handler)); | ||
[31m│[0m ╰──── | ||
[31m│[0m [36m help: [0mIf your type holds a reference to data that's owned by another | ||
[31m│[0m singleton component, register its constructor as transient rather | ||
[31m│[0m than singleton. |
30 changes: 30 additions & 0 deletions
30
...endency_injection/lifecycles/singletons_cannot_have_non_static_lifetime_parameters/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,30 @@ | ||
use pavex::blueprint::{router::GET, Blueprint}; | ||
use pavex::f; | ||
use pavex::http::StatusCode; | ||
|
||
pub struct A; | ||
|
||
#[derive(Clone)] | ||
pub struct B<'a>(&'a A); | ||
|
||
impl<'a> B<'a> { | ||
pub fn new(a: &'a A) -> Self { | ||
B(a) | ||
} | ||
} | ||
|
||
pub fn a() -> A { | ||
todo!() | ||
} | ||
|
||
pub fn handler<'a>(_b: B<'a>) -> StatusCode { | ||
todo!() | ||
} | ||
|
||
pub fn blueprint() -> Blueprint { | ||
let mut bp = Blueprint::new(); | ||
bp.singleton(f!(self::a)); | ||
bp.singleton(f!(self::B::new)).clone_if_necessary(); | ||
bp.route(GET, "/", f!(self::handler)); | ||
bp | ||
} |
4 changes: 4 additions & 0 deletions
4
...jection/lifecycles/singletons_cannot_have_non_static_lifetime_parameters/test_config.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,4 @@ | ||
description = """Singletons cannot have non-'static lifetime parameters""" | ||
|
||
[expectations] | ||
codegen = "fail" |
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