From d6db31122946c270bb276e86d877b6379e899bdd Mon Sep 17 00:00:00 2001 From: 82marbag <69267416+82marbag@users.noreply.github.com> Date: Fri, 21 Oct 2022 17:43:57 +0200 Subject: [PATCH 1/4] 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 | 17 ++++++++++++----- 3 files changed, 16 insertions(+), 13 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 64f09b7536..29d829ff8d 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 5c55e2c2b3..9042a15fc5 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 5028231e21..3a3cb527e4 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, }; @@ -225,7 +226,7 @@ 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; } impl Upgradable for Operation @@ -255,6 +256,11 @@ where // The signature of the output is correct >>::Service: Service, Response = http::Response>, + + // For `Route::new` for the resulting service + >>::Service: tower::Service, Error = std::convert::Infallible>, + <>::Layer as Layer>::Service>>>::Service: Clone + Send + 'static, + <<>::Layer as Layer>::Service>>>::Service as tower::Service>>::Future: Send + 'static, { type Service = >>::Service; @@ -262,10 +268,10 @@ where /// 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,11 +288,12 @@ pub struct FailOnMissingOperation; impl Upgradable for FailOnMissingOperation where InternalFailureException: IntoResponse

, + P: Send + '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 }) } } From 5defc2a6d89ccfdc03ba9ec360954d72061a631d Mon Sep 17 00:00:00 2001 From: Daniele Ahmed Date: Fri, 21 Oct 2022 18:02:59 +0200 Subject: [PATCH 2/4] Remove superfluous types Signed-off-by: Daniele Ahmed --- .../aws-smithy-http-server/src/operation/upgrade.rs | 6 ------ 1 file changed, 6 deletions(-) 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 3a3cb527e4..c20ed2d25c 100644 --- a/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs +++ b/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs @@ -223,8 +223,6 @@ 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) -> Route; } @@ -262,8 +260,6 @@ where <>::Layer as Layer>::Service>>>::Service: Clone + Send + 'static, <<>::Layer as Layer>::Service>>>::Service as tower::Service>>::Future: Send + 'static, { - type Service = >>::Service; - /// Takes the [`Operation`](Operation), applies [`Plugin`], then applies [`UpgradeLayer`] to /// the modified `S`, then finally applies the modified `L`. /// @@ -290,8 +286,6 @@ where InternalFailureException: IntoResponse

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

; - fn upgrade(self, _plugin: &Pl) -> Route { Route::new(MissingFailure { _protocol: PhantomData }) } From a17c083295e49023583822cae0f43d28fad38b38 Mon Sep 17 00:00:00 2001 From: Daniele Ahmed Date: Mon, 24 Oct 2022 11:32:33 +0200 Subject: [PATCH 3/4] Shorten imported types Signed-off-by: Daniele Ahmed --- rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 c20ed2d25c..0156da850d 100644 --- a/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs +++ b/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs @@ -256,9 +256,9 @@ where Service, Response = http::Response>, // For `Route::new` for the resulting service - >>::Service: tower::Service, Error = std::convert::Infallible>, + >>::Service: Service, Error = Infallible>, <>::Layer as Layer>::Service>>>::Service: Clone + Send + 'static, - <<>::Layer as Layer>::Service>>>::Service as tower::Service>>::Future: Send + 'static, + <<>::Layer as Layer>::Service>>>::Service as Service>>::Future: Send + 'static, { /// Takes the [`Operation`](Operation), applies [`Plugin`], then applies [`UpgradeLayer`] to /// the modified `S`, then finally applies the modified `L`. From b2b9272bb7ef14ff0ff29e0a440dcf5bc4de7447 Mon Sep 17 00:00:00 2001 From: 82marbag <69267416+82marbag@users.noreply.github.com> Date: Tue, 25 Oct 2022 15:17:04 +0200 Subject: [PATCH 4/4] Type alias Signed-off-by: Daniele Ahmed --- .../src/operation/upgrade.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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 0156da850d..ff12e55005 100644 --- a/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs +++ b/rust-runtime/aws-smithy-http-server/src/operation/upgrade.rs @@ -71,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 { @@ -227,6 +224,10 @@ pub trait Upgradable { 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 @@ -257,8 +258,8 @@ where // For `Route::new` for the resulting service >>::Service: Service, Error = Infallible>, - <>::Layer as Layer>::Service>>>::Service: Clone + Send + 'static, - <<>::Layer as Layer>::Service>>>::Service as Service>>::Future: Send + 'static, + 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`. @@ -284,7 +285,7 @@ pub struct FailOnMissingOperation; impl Upgradable for FailOnMissingOperation where InternalFailureException: IntoResponse

, - P: Send + 'static, + P: 'static, { fn upgrade(self, _plugin: &Pl) -> Route { Route::new(MissingFailure { _protocol: PhantomData }) @@ -292,8 +293,9 @@ where } /// 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

{