Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newtype FromRequest::Future #2244

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
"RuntimeError" to ServerRuntimeType.runtimeError(runtimeConfig),
"RequestRejection" to ServerRuntimeType.requestRejection(runtimeConfig),
"ResponseRejection" to ServerRuntimeType.responseRejection(runtimeConfig),
"PinProjectLite" to ServerCargoDependency.PinProjectLite.toType(),
"http" to RuntimeType.Http,
)

Expand Down Expand Up @@ -196,8 +197,27 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
}

// Implement `from_request` trait for input types.
val inputFuture = "${inputSymbol.name}Future"
rustTemplate(
"""
// TODO(https://github.com/awslabs/smithy-rs/issues/2238): Remove the `Pin<Box<dyn Future>>` and replace with thin wrapper around `Collect`.
#{PinProjectLite}::pin_project! {
/// A [`Future`](std::future::Future) aggregating the body bytes of a [`Request`] and constructing the
/// [`${inputSymbol.name}`](#{I}) using modelled bindings.
pub struct $inputFuture {
inner: std::pin::Pin<Box<dyn std::future::Future<Output = Result<#{I}, #{RuntimeError}>> + Send>>
}
}

impl std::future::Future for $inputFuture {
type Output = Result<#{I}, #{RuntimeError}>;

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
let this = self.project();
this.inner.as_mut().poll(cx)
}
}

impl<B> #{SmithyHttpServer}::request::FromRequest<#{Marker}, B> for #{I}
where
B: #{SmithyHttpServer}::body::HttpBody + Send,
Expand All @@ -207,7 +227,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
#{RequestRejection} : From<<B as #{SmithyHttpServer}::body::HttpBody>::Error>
{
type Rejection = #{RuntimeError};
type Future = std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self, Self::Rejection>> + Send>>;
type Future = $inputFuture;

fn from_request(request: #{http}::Request<B>) -> Self::Future {
let fut = async move {
Expand All @@ -217,7 +237,9 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
.await
.map_err(Into::into)
};
Box::pin(fut)
$inputFuture {
inner: Box::pin(fut)
}
}
}

Expand Down