From 50e85724b46277319db75f3af15938ca8e3a1693 Mon Sep 17 00:00:00 2001 From: 82marbag <69267416+82marbag@users.noreply.github.com> Date: Wed, 26 Oct 2022 05:44:37 -0400 Subject: [PATCH] Route::new in upgrade (#1891) * Route::new in upgrade Move the creation of Routes in Upgradable to have a cleaner sequence of constraints in ServiceBuilder::build Signed-off-by: Daniele Ahmed --- .../generators/ServerServiceGeneratorV2.kt | 8 ++--- .../generators/protocol/ServerProtocol.kt | 4 +-- .../src/operation/upgrade.rs | 33 ++++++++++--------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt index 3f44b9d67..eb44ffab1 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGeneratorV2.kt @@ -152,7 +152,7 @@ class ServerServiceGeneratorV2( private val buildConstraints = operations.zip(builderOps).zip(extensionTypes).map { (first, exts) -> val (operation, type) = first // TODO(https://github.com/awslabs/smithy-rs/issues/1713#issue-1365169734): The `Error = Infallible` is an - // excess requirement to stay at parity with existing builder. + // excess requirement to stay at parity with existing builder. writable { rustTemplate( """ @@ -162,11 +162,7 @@ class ServerServiceGeneratorV2( $exts, B, Pl, - >, - $type::Service: Clone + Send + 'static, - <$type::Service as #{Tower}::Service<#{Http}::Request>>::Future: Send + 'static, - - $type::Service: #{Tower}::Service<#{Http}::Request, Error = std::convert::Infallible> + > """, "Marker" to protocol.markerStruct(), *codegenScope, diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt index 73f3fd1da..2dea2153b 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt @@ -123,7 +123,7 @@ class ServerAwsJsonProtocol( """ ( String::from("$serviceName.$operationName"), - #{SmithyHttpServer}::routing::Route::new(#{OperationValue:W}) + #{OperationValue:W} ), """, "OperationValue" to operationValue, @@ -184,7 +184,7 @@ private fun restRouterConstruction( """ ( #{Key:W}, - #{SmithyHttpServer}::routing::Route::new(#{OperationValue:W}) + #{OperationValue:W} ), """, "Key" to key, diff --git a/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs b/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs index 5028231e2..ff12e5500 100644 --- a/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs +++ b/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs @@ -21,6 +21,7 @@ use crate::{ plugin::Plugin, request::{FromParts, FromRequest}, response::IntoResponse, + routing::Route, runtime_error::InternalFailureException, }; @@ -70,9 +71,6 @@ impl Layer for UpgradeLayer { } } -/// An alias allowing for quick access to [`UpgradeLayer`]s target [`Service`]. -pub type UpgradedService = as Layer>::Service; - /// A [`Service`] responsible for wrapping an operation [`Service`] accepting and returning Smithy /// types, and converting it into a [`Service`] accepting and returning [`http`] types. pub struct Upgrade { @@ -222,12 +220,14 @@ where /// Provides an interface to convert a representation of an operation to a HTTP [`Service`](tower::Service) with /// canonical associated types. pub trait Upgradable { - type Service: Service, Response = http::Response>; - /// Performs an upgrade from a representation of an operation to a HTTP [`Service`](tower::Service). - fn upgrade(self, plugin: &Plugin) -> Self::Service; + fn upgrade(self, plugin: &Plugin) -> Route; } +type UpgradedService = <>::Layer as Layer< + Upgrade>::Service>, +>>::Service; + impl Upgradable for Operation where // `Op` is used to specify the operation shape @@ -255,17 +255,20 @@ where // The signature of the output is correct >>::Service: Service, Response = http::Response>, -{ - type Service = >>::Service; + // For `Route::new` for the resulting service + >>::Service: Service, Error = Infallible>, + UpgradedService: Clone + Send + 'static, + as Service>>::Future: Send + 'static, +{ /// Takes the [`Operation`](Operation), applies [`Plugin`], then applies [`UpgradeLayer`] to /// the modified `S`, then finally applies the modified `L`. /// /// The composition is made explicit in the method constraints and return type. - fn upgrade(self, plugin: &Pl) -> Self::Service { + fn upgrade(self, plugin: &Pl) -> Route { let mapped = plugin.map(self); let layer = Stack::new(UpgradeLayer::new(), mapped.layer); - layer.layer(mapped.inner) + Route::new(layer.layer(mapped.inner)) } } @@ -282,17 +285,17 @@ pub struct FailOnMissingOperation; impl Upgradable for FailOnMissingOperation where InternalFailureException: IntoResponse

, + P: 'static, { - type Service = MissingFailure

; - - fn upgrade(self, _plugin: &Pl) -> Self::Service { - MissingFailure { _protocol: PhantomData } + fn upgrade(self, _plugin: &Pl) -> Route { + Route::new(MissingFailure { _protocol: PhantomData }) } } /// A [`Service`] which always returns an internal failure message and logs an error. +#[derive(Copy)] pub struct MissingFailure

{ - _protocol: PhantomData

, + _protocol: PhantomData, } impl

Clone for MissingFailure

{ -- GitLab