Skip to content

Commit

Permalink
fix: Ensure that types from std::sync work as expected when used as i…
Browse files Browse the repository at this point in the history
…nputs/outputs
  • Loading branch information
LukeMathWalker committed Nov 9, 2024
1 parent 196b575 commit d8a9c9a
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 57 deletions.
29 changes: 28 additions & 1 deletion libs/pavexc/src/rustdoc/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl CrateCollection {
// The item might come from a transitive dependency via a re-export
// done by a direct dependency.
// We don't have a bulletproof way of finding the re-exporter name, but we can
// try to infer it (e.g. via the `name` proeprty).
// try to infer it (e.g. via the `name` property).
re_exporter_crate_name: Option<&str>,
) -> Result<(GlobalItemId, &[String]), anyhow::Error> {
let (definition_package_id, path) = {
Expand Down Expand Up @@ -988,6 +988,33 @@ fn index_local_types<'a>(
}
} else {
navigation_history.insert(*imported_id);

// We keep track of the source path in our indexes.
// This is useful, in particular, if we don't have
// access to the source module of the imported item.
// This can happen when working with `std`/`alloc`/`core`
// since the JSON output doesn't include private/doc-hidden
// items.
{
let mut normalized_source_path = vec![];
let source_segments = i.source.split("::");
for segment in source_segments {
if segment == "self" {
normalized_source_path
.extend(current_path.iter().map(|s| s.to_string()));
} else if segment == "crate" {
normalized_source_path.push(current_path[0].to_string())
} else {
normalized_source_path.push(segment.to_string());
}
}
// Assume it's private unless we find out otherwise later on
private_path_index
.entry(*imported_id)
.or_default()
.insert(normalized_source_path);
}

index_local_types(
krate,
package_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
digraph "GET / - 0" {
0 [ label = "3| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a>>) -> pavex::response::Response"]
1 [ label = "2| pavex::middleware::Next::new(crate::route_0::Next0<'a>) -> pavex::middleware::Next<crate::route_0::Next0<'a>>"]
2 [ label = "1| crate::route_0::Next0(&'a alloc::sync::Arc<app_933292bd::Custom>) -> crate::route_0::Next0<'a>"]
4 [ label = "4| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
5 [ label = "0| &alloc::sync::Arc<app_933292bd::Custom>"]
0 [ label = "5| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>) -> pavex::response::Response"]
1 [ label = "4| pavex::middleware::Next::new(crate::route_0::Next0<'a, 'b, 'c>) -> pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>"]
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>"]
3 [ label = "1| &alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>"]
4 [ label = "2| &alloc::sync::Arc<app_933292bd::Custom>"]
6 [ label = "6| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
7 [ label = "0| &alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>"]
1 -> 0 [ ]
2 -> 1 [ ]
0 -> 4 [ ]
5 -> 2 [ ]
4 -> 2 [ ]
3 -> 2 [ ]
0 -> 6 [ ]
7 -> 2 [ ]
}

digraph "GET / - 1" {
0 [ label = "1| app_933292bd::handler(&alloc::sync::Arc<app_933292bd::Custom>) -> http::StatusCode"]
2 [ label = "2| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
3 [ label = "0| &alloc::sync::Arc<app_933292bd::Custom>"]
0 -> 2 [ ]
3 -> 0 [ ]
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"]
1 [ label = "1| &alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>"]
2 [ label = "2| &alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>"]
4 [ label = "4| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
5 [ label = "0| &alloc::sync::Arc<app_933292bd::Custom>"]
2 -> 0 [ ]
1 -> 0 [ ]
0 -> 4 [ ]
5 -> 0 [ ]
}

digraph "* / - 0" {
Expand All @@ -39,7 +47,11 @@ digraph "* / - 1" {
}

digraph app_state {
0 [ label = "1| crate::ApplicationState(alloc::sync::Arc<app_933292bd::Custom>) -> crate::ApplicationState"]
1 [ label = "0| app_933292bd::constructor() -> alloc::sync::Arc<app_933292bd::Custom>"]
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"]
1 [ label = "2| app_933292bd::arc_rwlock() -> alloc::sync::Arc<std::sync::RwLock<app_933292bd::Custom>>"]
2 [ label = "1| app_933292bd::arc() -> alloc::sync::Arc<app_933292bd::Custom>"]
3 [ label = "0| app_933292bd::arc_mutex() -> alloc::sync::Arc<std::sync::Mutex<app_933292bd::Custom>>"]
3 -> 0 [ ]
2 -> 0 [ ]
1 -> 0 [ ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ struct ServerState {
application_state: ApplicationState,
}
pub struct ApplicationState {
s0: alloc::sync::Arc<app::Custom>,
s0: alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
s1: alloc::sync::Arc<app::Custom>,
s2: alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
}
pub async fn build_application_state() -> crate::ApplicationState {
let v0 = app::constructor();
crate::ApplicationState { s0: v0 }
let v0 = app::arc_mutex();
let v1 = app::arc();
let v2 = app::arc_rwlock();
crate::ApplicationState {
s0: v2,
s1: v1,
s2: v0,
}
}
pub fn run(
server_builder: pavex::server::Server,
Expand Down Expand Up @@ -56,7 +64,12 @@ async fn route_request(
0u32 => {
match &request_head.method {
&pavex::http::Method::GET => {
route_0::entrypoint(&server_state.application_state.s0).await
route_0::entrypoint(
&server_state.application_state.s0,
&server_state.application_state.s1,
&server_state.application_state.s2,
)
.await
}
_ => {
let allowed_methods: pavex::router::AllowedMethods = pavex::router::MethodAllowList::from_iter([
Expand All @@ -71,50 +84,66 @@ async fn route_request(
}
}
pub mod route_0 {
pub async fn entrypoint<'a>(
s_0: &'a alloc::sync::Arc<app::Custom>,
pub async fn entrypoint<'a, 'b, 'c>(
s_0: &'a alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
s_1: &'b alloc::sync::Arc<app::Custom>,
s_2: &'c alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
) -> pavex::response::Response {
let response = wrapping_0(s_0).await;
let response = wrapping_0(s_0, s_1, s_2).await;
response
}
async fn stage_1<'a>(
s_0: &'a alloc::sync::Arc<app::Custom>,
async fn stage_1<'a, 'b, 'c>(
s_0: &'a alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
s_1: &'b alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
s_2: &'c alloc::sync::Arc<app::Custom>,
) -> pavex::response::Response {
let response = handler(s_0).await;
let response = handler(s_0, s_1, s_2).await;
response
}
async fn wrapping_0(
v0: &alloc::sync::Arc<app::Custom>,
v0: &alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
v1: &alloc::sync::Arc<app::Custom>,
v2: &alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
) -> pavex::response::Response {
let v1 = crate::route_0::Next0 {
s_0: v0,
let v3 = crate::route_0::Next0 {
s_0: v2,
s_1: v0,
s_2: v1,
next: stage_1,
};
let v2 = pavex::middleware::Next::new(v1);
let v3 = pavex::middleware::wrap_noop(v2).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v3)
let v4 = pavex::middleware::Next::new(v3);
let v5 = pavex::middleware::wrap_noop(v4).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v5)
}
async fn handler(
v0: &alloc::sync::Arc<app::Custom>,
v0: &alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
v1: &alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
v2: &alloc::sync::Arc<app::Custom>,
) -> pavex::response::Response {
let v1 = app::handler(v0);
<http::StatusCode as pavex::response::IntoResponse>::into_response(v1)
let v3 = app::handler(v2, v0, v1);
<http::StatusCode as pavex::response::IntoResponse>::into_response(v3)
}
struct Next0<'a, T>
struct Next0<'a, 'b, 'c, T>
where
T: std::future::Future<Output = pavex::response::Response>,
{
s_0: &'a alloc::sync::Arc<app::Custom>,
next: fn(&'a alloc::sync::Arc<app::Custom>) -> T,
s_0: &'a alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
s_1: &'b alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
s_2: &'c alloc::sync::Arc<app::Custom>,
next: fn(
&'a alloc::sync::Arc<std::sync::Mutex<app::Custom>>,
&'b alloc::sync::Arc<std::sync::RwLock<app::Custom>>,
&'c alloc::sync::Arc<app::Custom>,
) -> T,
}
impl<'a, T> std::future::IntoFuture for Next0<'a, T>
impl<'a, 'b, 'c, T> std::future::IntoFuture for Next0<'a, 'b, 'c, T>
where
T: std::future::Future<Output = pavex::response::Response>,
{
type Output = pavex::response::Response;
type IntoFuture = T;
fn into_future(self) -> Self::IntoFuture {
(self.next)(self.s_0)
(self.next)(self.s_0, self.s_1, self.s_2)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
digraph "GET / - 0" {
0 [ label = "3| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a>>) -> pavex::response::Response"]
1 [ label = "2| pavex::middleware::Next::new(crate::route_0::Next0<'a>) -> pavex::middleware::Next<crate::route_0::Next0<'a>>"]
2 [ label = "1| crate::route_0::Next0(&'a alloc::sync::Arc<app::Custom>) -> crate::route_0::Next0<'a>"]
4 [ label = "4| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
5 [ label = "0| &alloc::sync::Arc<app::Custom>"]
0 [ label = "5| pavex::middleware::wrap_noop(pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>) -> pavex::response::Response"]
1 [ label = "4| pavex::middleware::Next::new(crate::route_0::Next0<'a, 'b, 'c>) -> pavex::middleware::Next<crate::route_0::Next0<'a, 'b, 'c>>"]
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>"]
3 [ label = "1| &alloc::sync::Arc<std::sync::RwLock<app::Custom>>"]
4 [ label = "2| &alloc::sync::Arc<app::Custom>"]
6 [ label = "6| <pavex::response::Response as pavex::response::IntoResponse>::into_response(pavex::response::Response) -> pavex::response::Response"]
7 [ label = "0| &alloc::sync::Arc<std::sync::Mutex<app::Custom>>"]
1 -> 0 [ ]
2 -> 1 [ ]
0 -> 4 [ ]
5 -> 2 [ ]
4 -> 2 [ ]
3 -> 2 [ ]
0 -> 6 [ ]
7 -> 2 [ ]
}

digraph "GET / - 1" {
0 [ label = "1| app::handler(&alloc::sync::Arc<app::Custom>) -> http::StatusCode"]
2 [ label = "2| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
3 [ label = "0| &alloc::sync::Arc<app::Custom>"]
0 -> 2 [ ]
3 -> 0 [ ]
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"]
1 [ label = "1| &alloc::sync::Arc<std::sync::Mutex<app::Custom>>"]
2 [ label = "2| &alloc::sync::Arc<std::sync::RwLock<app::Custom>>"]
4 [ label = "4| <http::StatusCode as pavex::response::IntoResponse>::into_response(http::StatusCode) -> pavex::response::Response"]
5 [ label = "0| &alloc::sync::Arc<app::Custom>"]
2 -> 0 [ ]
1 -> 0 [ ]
0 -> 4 [ ]
5 -> 0 [ ]
}

digraph "* / - 0" {
Expand All @@ -39,7 +47,11 @@ digraph "* / - 1" {
}

digraph app_state {
0 [ label = "1| crate::ApplicationState(alloc::sync::Arc<app::Custom>) -> crate::ApplicationState"]
1 [ label = "0| app::constructor() -> alloc::sync::Arc<app::Custom>"]
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"]
1 [ label = "2| app::arc_rwlock() -> alloc::sync::Arc<std::sync::RwLock<app::Custom>>"]
2 [ label = "1| app::arc() -> alloc::sync::Arc<app::Custom>"]
3 [ label = "0| app::arc_mutex() -> alloc::sync::Arc<std::sync::Mutex<app::Custom>>"]
3 -> 0 [ ]
2 -> 0 [ ]
1 -> 0 [ ]
}
18 changes: 14 additions & 4 deletions libs/ui_tests/reflection/arc_singletons_are_supported/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
use std::sync::Arc;
use std::sync::{Arc, Mutex, RwLock};

use pavex::blueprint::{router::GET, Blueprint};
use pavex::f;
use pavex::http::StatusCode;

pub struct Custom;

pub fn constructor() -> Arc<Custom> {
pub fn arc() -> Arc<Custom> {
Arc::new(Custom)
}

pub fn handler(_s: &Arc<Custom>) -> StatusCode {
pub fn arc_mutex() -> Arc<Mutex<Custom>> {
Arc::new(Mutex::new(Custom))
}

pub fn arc_rwlock() -> Arc<RwLock<Custom>> {
Arc::new(RwLock::new(Custom))
}

pub fn handler(_s: &Arc<Custom>, _t: &Arc<Mutex<Custom>>, _u: &Arc<RwLock<Custom>>) -> StatusCode {
todo!()
}

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.singleton(f!(crate::constructor));
bp.singleton(f!(crate::arc));
bp.singleton(f!(crate::arc_mutex));
bp.singleton(f!(crate::arc_rwlock));
bp.route(GET, "/", f!(crate::handler));
bp
}

0 comments on commit d8a9c9a

Please sign in to comment.