Unverified Commit f7c417c5 authored by Harry Barber's avatar Harry Barber Committed by GitHub
Browse files

Newtype FromRequest::Future (#2244)

parent 41ca31b8
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -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,
    )

@@ -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,
@@ -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 {
@@ -217,7 +237,9 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
                            .await
                            .map_err(Into::into)
                    };
                    Box::pin(fut)
                    $inputFuture {
                        inner: Box::pin(fut)
                    }
                }
            }