Skip to content

Commit

Permalink
Fix bug related to generic arguments defined in a foreign crate wrt t…
Browse files Browse the repository at this point in the history
…he typedef definition.
  • Loading branch information
LukeMathWalker committed Jun 9, 2023
1 parent 9518d66 commit a950e54
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/pavex/src/compiler/resolvers.rs
Expand Up @@ -56,7 +56,7 @@ pub(crate) fn resolve_type(
if let GenericArg::Type(provided_arg) = provided_arg {
resolve_type(
provided_arg,
&global_type_id.package_id,
&used_by_package_id,
krate_collection,
&generic_bindings,
)?
Expand Down
@@ -0,0 +1,103 @@
//! Do NOT edit this code.
//! It was automatically generated by Pavex.
//! All manual edits will be lost next time the code is generated.
#[allow(unused_imports)]
use std as alloc;
struct ServerState {
router: pavex_runtime::routing::Router<u32>,
#[allow(dead_code)]
application_state: ApplicationState,
}
pub struct ApplicationState {}
pub async fn build_application_state() -> crate::ApplicationState {
crate::ApplicationState {}
}
pub async fn run(
server_builder: pavex_runtime::hyper::server::Builder<
pavex_runtime::hyper::server::conn::AddrIncoming,
>,
application_state: ApplicationState,
) -> Result<(), pavex_runtime::Error> {
let server_state = std::sync::Arc::new(ServerState {
router: build_router().map_err(pavex_runtime::Error::new)?,
application_state,
});
let make_service = pavex_runtime::hyper::service::make_service_fn(move |_| {
let server_state = server_state.clone();
async move {
Ok::<
_,
pavex_runtime::hyper::Error,
>(
pavex_runtime::hyper::service::service_fn(move |request| {
let server_state = server_state.clone();
async move {
Ok::<
_,
pavex_runtime::hyper::Error,
>(route_request(request, server_state).await)
}
}),
)
}
});
server_builder.serve(make_service).await.map_err(pavex_runtime::Error::new)
}
fn build_router() -> Result<
pavex_runtime::routing::Router<u32>,
pavex_runtime::routing::InsertError,
> {
let mut router = pavex_runtime::routing::Router::new();
router.insert("/home", 0u32)?;
Ok(router)
}
async fn route_request(
request: http::Request<pavex_runtime::hyper::body::Body>,
server_state: std::sync::Arc<ServerState>,
) -> pavex_runtime::response::Response {
#[allow(unused)]
let (request_head, request_body) = request.into_parts();
let request_head: pavex_runtime::request::RequestHead = request_head.into();
let matched_route = match server_state.router.at(&request_head.uri.path()) {
Ok(m) => m,
Err(_) => {
return pavex_runtime::response::Response::builder()
.status(pavex_runtime::http::StatusCode::NOT_FOUND)
.body(pavex_runtime::body::boxed(hyper::body::Body::empty()))
.unwrap();
}
};
let route_id = matched_route.value;
#[allow(unused)]
let url_params: pavex_runtime::extract::route::RawRouteParams<'_, '_> = matched_route
.params
.into();
match route_id {
0u32 => {
match &request_head.method {
&pavex_runtime::http::Method::GET => route_handler_0().await,
_ => {
pavex_runtime::response::Response::builder()
.status(pavex_runtime::http::StatusCode::METHOD_NOT_ALLOWED)
.header(pavex_runtime::http::header::ALLOW, "GET")
.body(pavex_runtime::body::boxed(hyper::body::Body::empty()))
.unwrap()
}
}
}
_ => {
pavex_runtime::response::Response::builder()
.status(pavex_runtime::http::StatusCode::NOT_FOUND)
.body(pavex_runtime::body::boxed(hyper::body::Body::empty()))
.unwrap()
}
}
}
pub async fn route_handler_0() -> http::Response<
http_body::combinators::BoxBody<bytes::Bytes, pavex_runtime::Error>,
> {
let v0 = app::handler();
<http::Response<
app::BodyType,
> as pavex_runtime::response::IntoResponse>::into_response(v0)
}
@@ -0,0 +1,8 @@
digraph "GET /home" {
0 [ label = "app::handler() -> http::Response<app::BodyType>"]
1 [ label = "<http::Response::<app::BodyType> as pavex_runtime::response::IntoResponse>::into_response(http::Response<app::BodyType>) -> http::Response<http_body::combinators::BoxBody<bytes::Bytes, pavex_runtime::Error>>"]
0 -> 1 [ ]
}
digraph app_state {
0 [ label = "crate::ApplicationState() -> crate::ApplicationState"]
}
@@ -0,0 +1,40 @@
use pavex_builder::{f, router::GET, Blueprint};
use pavex_runtime::response::IntoResponse;
use pavex_runtime::response::Response;

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

// A locally-definited type
pub struct BodyType {
pub name: String,
pub age: u8,
}

impl http_body::Body for BodyType {
type Data = bytes::Bytes;
type Error = pavex_runtime::Error;

fn poll_data(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Result<Self::Data, Self::Error>>> {
todo!()
}

fn poll_trailers(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<Option<pavex_runtime::http::HeaderMap>, Self::Error>> {
todo!()
}
}

// The `Response` type comes from `pavex_runtime` but the body
// type is defined in this crate.
pub fn handler() -> Response<BodyType> {
todo!()
}
@@ -0,0 +1,11 @@
description = """
Pavex handles types whose generic parameter are not defined in the same crate
that defines the base type
"""

[expectations]
codegen = "pass"

[dependencies]
http-body = "0.4"
bytes = "1"
@@ -1,4 +1,4 @@
description = "pavex is abled to handle glob re-exports from local modules"
description = "Pavex is able to handle glob re-exports from local modules"

[expectations]
codegen = "pass"
Expand Down

0 comments on commit a950e54

Please sign in to comment.