Skip to content

Commit f7c417c

Browse files
authored
Newtype FromRequest::Future (#2244)
1 parent 41ca31b commit f7c417c

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpBoundProtocolGenerator.kt

+24-2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
136136
"RuntimeError" to ServerRuntimeType.runtimeError(runtimeConfig),
137137
"RequestRejection" to ServerRuntimeType.requestRejection(runtimeConfig),
138138
"ResponseRejection" to ServerRuntimeType.responseRejection(runtimeConfig),
139+
"PinProjectLite" to ServerCargoDependency.PinProjectLite.toType(),
139140
"http" to RuntimeType.Http,
140141
)
141142

@@ -196,8 +197,27 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
196197
}
197198

198199
// Implement `from_request` trait for input types.
200+
val inputFuture = "${inputSymbol.name}Future"
199201
rustTemplate(
200202
"""
203+
// TODO(https://github.com/awslabs/smithy-rs/issues/2238): Remove the `Pin<Box<dyn Future>>` and replace with thin wrapper around `Collect`.
204+
#{PinProjectLite}::pin_project! {
205+
/// A [`Future`](std::future::Future) aggregating the body bytes of a [`Request`] and constructing the
206+
/// [`${inputSymbol.name}`](#{I}) using modelled bindings.
207+
pub struct $inputFuture {
208+
inner: std::pin::Pin<Box<dyn std::future::Future<Output = Result<#{I}, #{RuntimeError}>> + Send>>
209+
}
210+
}
211+
212+
impl std::future::Future for $inputFuture {
213+
type Output = Result<#{I}, #{RuntimeError}>;
214+
215+
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
216+
let this = self.project();
217+
this.inner.as_mut().poll(cx)
218+
}
219+
}
220+
201221
impl<B> #{SmithyHttpServer}::request::FromRequest<#{Marker}, B> for #{I}
202222
where
203223
B: #{SmithyHttpServer}::body::HttpBody + Send,
@@ -207,7 +227,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
207227
#{RequestRejection} : From<<B as #{SmithyHttpServer}::body::HttpBody>::Error>
208228
{
209229
type Rejection = #{RuntimeError};
210-
type Future = std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self, Self::Rejection>> + Send>>;
230+
type Future = $inputFuture;
211231
212232
fn from_request(request: #{http}::Request<B>) -> Self::Future {
213233
let fut = async move {
@@ -217,7 +237,9 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
217237
.await
218238
.map_err(Into::into)
219239
};
220-
Box::pin(fut)
240+
$inputFuture {
241+
inner: Box::pin(fut)
242+
}
221243
}
222244
}
223245

0 commit comments

Comments
 (0)