Skip to content

Commit 766f7b7

Browse files
fix: Ensure that types from std::sync work as expected when used as inputs/outputs
1 parent 196b575 commit 766f7b7

File tree

5 files changed

+147
-57
lines changed

5 files changed

+147
-57
lines changed

libs/pavexc/src/rustdoc/queries.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl CrateCollection {
467467
// The item might come from a transitive dependency via a re-export
468468
// done by a direct dependency.
469469
// We don't have a bulletproof way of finding the re-exporter name, but we can
470-
// try to infer it (e.g. via the `name` proeprty).
470+
// try to infer it (e.g. via the `name` property).
471471
re_exporter_crate_name: Option<&str>,
472472
) -> Result<(GlobalItemId, &[String]), anyhow::Error> {
473473
let (definition_package_id, path) = {
@@ -988,6 +988,33 @@ fn index_local_types<'a>(
988988
}
989989
} else {
990990
navigation_history.insert(*imported_id);
991+
992+
// We keep track of the source path in our indexes.
993+
// This is useful, in particular, if we don't have
994+
// access to the source module of the imported item.
995+
// This can happen when working with `std`/`alloc`/`core`
996+
// since the JSON output doesn't include private/doc-hidden
997+
// items.
998+
{
999+
let mut normalized_source_path = vec![];
1000+
let source_segments = i.source.split("::");
1001+
for segment in source_segments {
1002+
if segment == "self" {
1003+
normalized_source_path
1004+
.extend(current_path.iter().map(|s| s.to_string()));
1005+
} else if segment == "crate" {
1006+
normalized_source_path.push(current_path[0].to_string())
1007+
} else {
1008+
normalized_source_path.push(segment.to_string());
1009+
}
1010+
}
1011+
// Assume it's private unless we find out otherwise later on
1012+
private_path_index
1013+
.entry(*imported_id)
1014+
.or_default()
1015+
.insert(normalized_source_path);
1016+
}
1017+
9911018
index_local_types(
9921019
krate,
9931020
package_id,

libs/ui_tests/reflection/arc_singletons_are_supported/diagnostics.dot

+26-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
digraph "GET / - 0" {
2-
0 [ label = "3| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a>>) -> pavex::response::Response"]
3-
1 [ label = "2| pavex::middleware::Next::new(crate::route_0::Next0<'a>) -> pavex::middleware::Next<crate::route_0::Next0<'a>>"]
4-
2 [ label = "1| crate::route_0::Next0(&'a alloc::sync::Arc<app_933292bd::Custom>) -> crate::route_0::Next0<'a>"]
5-
4 [ label = "4| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
6-
5 [ label = "0| &alloc::sync::Arc<app_933292bd::Custom>"]
2+
0 [ label = "5| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>) -> pavex::response::Response"]
3+
1 [ label = "4| pavex::middleware::Next::new(crate::route_0::Next0<'a, 'b, 'c>) -> pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>"]
4+
2 [ label = "3| crate::route_0::Next0(&'a alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>, &'b alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>, &'c alloc::sync::Arc<app_933292bd::Custom>) -> crate::route_0::Next0<'a, 'b, 'c>"]
5+
3 [ label = "1| &alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>"]
6+
4 [ label = "2| &alloc::sync::Arc<app_933292bd::Custom>"]
7+
6 [ label = "6| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
8+
7 [ label = "0| &alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>"]
79
1 -> 0 [ ]
810
2 -> 1 [ ]
9-
0 -> 4 [ ]
10-
5 -> 2 [ ]
11+
4 -> 2 [ ]
12+
3 -> 2 [ ]
13+
0 -> 6 [ ]
14+
7 -> 2 [ ]
1115
}
1216

1317
digraph "GET / - 1" {
14-
0 [ label = "1| app_933292bd::handler(&alloc::sync::Arc<app_933292bd::Custom>) -> http::StatusCode"]
15-
2 [ label = "2| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
16-
3 [ label = "0| &alloc::sync::Arc<app_933292bd::Custom>"]
17-
0 -> 2 [ ]
18-
3 -> 0 [ ]
18+
0 [ label = "3| app_933292bd::handler(&alloc::sync::Arc<app_933292bd::Custom>, &alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>, &alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>) -> http::StatusCode"]
19+
1 [ label = "1| &alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>"]
20+
2 [ label = "2| &alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>"]
21+
4 [ label = "4| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
22+
5 [ label = "0| &alloc::sync::Arc<app_933292bd::Custom>"]
23+
2 -> 0 [ ]
24+
1 -> 0 [ ]
25+
0 -> 4 [ ]
26+
5 -> 0 [ ]
1927
}
2028

2129
digraph "* / - 0" {
@@ -39,7 +47,11 @@ digraph "* / - 1" {
3947
}
4048

4149
digraph app_state {
42-
0 [ label = "1| crate::ApplicationState(alloc::sync::Arc<app_933292bd::Custom>) -> crate::ApplicationState"]
43-
1 [ label = "0| app_933292bd::constructor() -> alloc::sync::Arc<app_933292bd::Custom>"]
50+
0 [ label = "3| crate::ApplicationState(alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>, alloc::sync::Arc<app_933292bd::Custom>, alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>) -> crate::ApplicationState"]
51+
1 [ label = "2| app_933292bd::arc_rwlock() -> alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>"]
52+
2 [ label = "1| app_933292bd::arc() -> alloc::sync::Arc<app_933292bd::Custom>"]
53+
3 [ label = "0| app_933292bd::arc_mutex() -> alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>"]
54+
3 -> 0 [ ]
55+
2 -> 0 [ ]
4456
1 -> 0 [ ]
4557
}

libs/ui_tests/reflection/arc_singletons_are_supported/expectations/app.rs

+53-24
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ struct ServerState {
77
application_state: ApplicationState,
88
}
99
pub struct ApplicationState {
10-
s0: alloc::sync::Arc<app::Custom>,
10+
s0: alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
11+
s1: alloc::sync::Arc<app::Custom>,
12+
s2: alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
1113
}
1214
pub async fn build_application_state() -> crate::ApplicationState {
13-
let v0 = app::constructor();
14-
crate::ApplicationState { s0: v0 }
15+
let v0 = app::arc_mutex();
16+
let v1 = app::arc();
17+
let v2 = app::arc_rwlock();
18+
crate::ApplicationState {
19+
s0: v2,
20+
s1: v1,
21+
s2: v0,
22+
}
1523
}
1624
pub fn run(
1725
server_builder: pavex::server::Server,
@@ -56,7 +64,12 @@ async fn route_request(
5664
0u32 => {
5765
match &request_head.method {
5866
&pavex::http::Method::GET => {
59-
route_0::entrypoint(&server_state.application_state.s0).await
67+
route_0::entrypoint(
68+
&server_state.application_state.s0,
69+
&server_state.application_state.s1,
70+
&server_state.application_state.s2,
71+
)
72+
.await
6073
}
6174
_ => {
6275
let allowed_methods: pavex::router::AllowedMethods = pavex::router::MethodAllowList::from_iter([
@@ -71,50 +84,66 @@ async fn route_request(
7184
}
7285
}
7386
pub mod route_0 {
74-
pub async fn entrypoint<'a>(
75-
s_0: &'a alloc::sync::Arc<app::Custom>,
87+
pub async fn entrypoint<'a, 'b, 'c>(
88+
s_0: &'a alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
89+
s_1: &'b alloc::sync::Arc<app::Custom>,
90+
s_2: &'c alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
7691
) -> pavex::response::Response {
77-
let response = wrapping_0(s_0).await;
92+
let response = wrapping_0(s_0, s_1, s_2).await;
7893
response
7994
}
80-
async fn stage_1<'a>(
81-
s_0: &'a alloc::sync::Arc<app::Custom>,
95+
async fn stage_1<'a, 'b, 'c>(
96+
s_0: &'a alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
97+
s_1: &'b alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
98+
s_2: &'c alloc::sync::Arc<app::Custom>,
8299
) -> pavex::response::Response {
83-
let response = handler(s_0).await;
100+
let response = handler(s_0, s_1, s_2).await;
84101
response
85102
}
86103
async fn wrapping_0(
87-
v0: &alloc::sync::Arc<app::Custom>,
104+
v0: &alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
105+
v1: &alloc::sync::Arc<app::Custom>,
106+
v2: &alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
88107
) -> pavex::response::Response {
89-
let v1 = crate::route_0::Next0 {
90-
s_0: v0,
108+
let v3 = crate::route_0::Next0 {
109+
s_0: v2,
110+
s_1: v0,
111+
s_2: v1,
91112
next: stage_1,
92113
};
93-
let v2 = pavex::middleware::Next::new(v1);
94-
let v3 = pavex::middleware::wrap_noop(v2).await;
95-
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v3)
114+
let v4 = pavex::middleware::Next::new(v3);
115+
let v5 = pavex::middleware::wrap_noop(v4).await;
116+
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v5)
96117
}
97118
async fn handler(
98-
v0: &alloc::sync::Arc<app::Custom>,
119+
v0: &alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
120+
v1: &alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
121+
v2: &alloc::sync::Arc<app::Custom>,
99122
) -> pavex::response::Response {
100-
let v1 = app::handler(v0);
101-
<http::StatusCode as pavex::response::IntoResponse>::into_response(v1)
123+
let v3 = app::handler(v2, v0, v1);
124+
<http::StatusCode as pavex::response::IntoResponse>::into_response(v3)
102125
}
103-
struct Next0<'a, T>
126+
struct Next0<'a, 'b, 'c, T>
104127
where
105128
T: std::future::Future<Output = pavex::response::Response>,
106129
{
107-
s_0: &'a alloc::sync::Arc<app::Custom>,
108-
next: fn(&'a alloc::sync::Arc<app::Custom>) -> T,
130+
s_0: &'a alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
131+
s_1: &'b alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
132+
s_2: &'c alloc::sync::Arc<app::Custom>,
133+
next: fn(
134+
&'a alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
135+
&'b alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
136+
&'c alloc::sync::Arc<app::Custom>,
137+
) -> T,
109138
}
110-
impl<'a, T> std::future::IntoFuture for Next0<'a, T>
139+
impl<'a, 'b, 'c, T> std::future::IntoFuture for Next0<'a, 'b, 'c, T>
111140
where
112141
T: std::future::Future<Output = pavex::response::Response>,
113142
{
114143
type Output = pavex::response::Response;
115144
type IntoFuture = T;
116145
fn into_future(self) -> Self::IntoFuture {
117-
(self.next)(self.s_0)
146+
(self.next)(self.s_0, self.s_1, self.s_2)
118147
}
119148
}
120149
}

libs/ui_tests/reflection/arc_singletons_are_supported/expectations/diagnostics.dot

+26-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
digraph "GET / - 0" {
2-
0 [ label = "3| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a>>) -> pavex::response::Response"]
3-
1 [ label = "2| pavex::middleware::Next::new(crate::route_0::Next0<'a>) -> pavex::middleware::Next<crate::route_0::Next0<'a>>"]
4-
2 [ label = "1| crate::route_0::Next0(&'a alloc::sync::Arc<app::Custom>) -> crate::route_0::Next0<'a>"]
5-
4 [ label = "4| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
6-
5 [ label = "0| &alloc::sync::Arc<app::Custom>"]
2+
0 [ label = "5| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>) -> pavex::response::Response"]
3+
1 [ label = "4| pavex::middleware::Next::new(crate::route_0::Next0<'a, 'b, 'c>) -> pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>"]
4+
2 [ label = "3| crate::route_0::Next0(&'a alloc::sync::Arc<std::sync::Mutex<app::Custom>>, &'b alloc::sync::Arc<std::sync::RwLock<app::Custom>>, &'c alloc::sync::Arc<app::Custom>) -> crate::route_0::Next0<'a, 'b, 'c>"]
5+
3 [ label = "1| &alloc::sync::Arc<std::sync::RwLock<app::Custom>>"]
6+
4 [ label = "2| &alloc::sync::Arc<app::Custom>"]
7+
6 [ label = "6| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
8+
7 [ label = "0| &alloc::sync::Arc<std::sync::Mutex<app::Custom>>"]
79
1 -> 0 [ ]
810
2 -> 1 [ ]
9-
0 -> 4 [ ]
10-
5 -> 2 [ ]
11+
4 -> 2 [ ]
12+
3 -> 2 [ ]
13+
0 -> 6 [ ]
14+
7 -> 2 [ ]
1115
}
1216

1317
digraph "GET / - 1" {
14-
0 [ label = "1| app::handler(&alloc::sync::Arc<app::Custom>) -> http::StatusCode"]
15-
2 [ label = "2| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
16-
3 [ label = "0| &alloc::sync::Arc<app::Custom>"]
17-
0 -> 2 [ ]
18-
3 -> 0 [ ]
18+
0 [ label = "3| app::handler(&alloc::sync::Arc<app::Custom>, &alloc::sync::Arc<std::sync::Mutex<app::Custom>>, &alloc::sync::Arc<std::sync::RwLock<app::Custom>>) -> http::StatusCode"]
19+
1 [ label = "1| &alloc::sync::Arc<std::sync::Mutex<app::Custom>>"]
20+
2 [ label = "2| &alloc::sync::Arc<std::sync::RwLock<app::Custom>>"]
21+
4 [ label = "4| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
22+
5 [ label = "0| &alloc::sync::Arc<app::Custom>"]
23+
2 -> 0 [ ]
24+
1 -> 0 [ ]
25+
0 -> 4 [ ]
26+
5 -> 0 [ ]
1927
}
2028

2129
digraph "* / - 0" {
@@ -39,7 +47,11 @@ digraph "* / - 1" {
3947
}
4048

4149
digraph app_state {
42-
0 [ label = "1| crate::ApplicationState(alloc::sync::Arc<app::Custom>) -> crate::ApplicationState"]
43-
1 [ label = "0| app::constructor() -> alloc::sync::Arc<app::Custom>"]
50+
0 [ label = "3| crate::ApplicationState(alloc::sync::Arc<std::sync::RwLock<app::Custom>>, alloc::sync::Arc<app::Custom>, alloc::sync::Arc<std::sync::Mutex<app::Custom>>) -> crate::ApplicationState"]
51+
1 [ label = "2| app::arc_rwlock() -> alloc::sync::Arc<std::sync::RwLock<app::Custom>>"]
52+
2 [ label = "1| app::arc() -> alloc::sync::Arc<app::Custom>"]
53+
3 [ label = "0| app::arc_mutex() -> alloc::sync::Arc<std::sync::Mutex<app::Custom>>"]
54+
3 -> 0 [ ]
55+
2 -> 0 [ ]
4456
1 -> 0 [ ]
4557
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
use std::sync::Arc;
1+
use std::sync::{Arc, Mutex, RwLock};
22

33
use pavex::blueprint::{router::GET, Blueprint};
44
use pavex::f;
55
use pavex::http::StatusCode;
66

77
pub struct Custom;
88

9-
pub fn constructor() -> Arc<Custom> {
9+
pub fn arc() -> Arc<Custom> {
1010
Arc::new(Custom)
1111
}
1212

13-
pub fn handler(_s: &Arc<Custom>) -> StatusCode {
13+
pub fn arc_mutex() -> Arc<Mutex<Custom>> {
14+
Arc::new(Mutex::new(Custom))
15+
}
16+
17+
pub fn arc_rwlock() -> Arc<RwLock<Custom>> {
18+
Arc::new(RwLock::new(Custom))
19+
}
20+
21+
pub fn handler(_s: &Arc<Custom>, _t: &Arc<Mutex<Custom>>, _u: &Arc<RwLock<Custom>>) -> StatusCode {
1422
todo!()
1523
}
1624

1725
pub fn blueprint() -> Blueprint {
1826
let mut bp = Blueprint::new();
19-
bp.singleton(f!(crate::constructor));
27+
bp.singleton(f!(crate::arc));
28+
bp.singleton(f!(crate::arc_mutex));
29+
bp.singleton(f!(crate::arc_rwlock));
2030
bp.route(GET, "/", f!(crate::handler));
2131
bp
2232
}

0 commit comments

Comments
 (0)