From 20ad88862fd82edec0aa642e4262374786a2a6bc Mon Sep 17 00:00:00 2001 From: david-perez Date: Mon, 8 Jul 2024 18:33:51 +0200 Subject: [PATCH] AWS JSON 1.x server request specs can be `&'static str`s (#3741) This is technically a breaking change because we stop implementing `FromIterator<(String, S)>`. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --- .../smithy/rust/codegen/core/smithy/RuntimeType.kt | 3 +++ .../smithy/generators/protocol/ServerProtocol.kt | 4 ++-- rust-runtime/aws-smithy-http-server/Cargo.toml | 2 +- .../src/protocol/aws_json/router.rs | 12 ++++-------- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt index da5d742647..903bc85f86 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt @@ -272,6 +272,9 @@ data class RuntimeType(val path: String, val dependency: RustDependency? = null) val U64 = std.resolve("primitive::u64") val Vec = std.resolve("vec::Vec") + // primitive types + val StaticStr = RuntimeType("&'static str") + // external cargo dependency types val Bytes = CargoDependency.Bytes.toType().resolve("Bytes") val Http = CargoDependency.Http.toType() 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 2fb76bf879..6434c0290c 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 @@ -163,10 +163,10 @@ class ServerAwsJsonProtocol( serviceName: String, requestSpecModule: RuntimeType, ) = writable { - rust("""String::from("$serviceName.$operationName")""") + rust(""""$serviceName.$operationName"""") } - override fun serverRouterRequestSpecType(requestSpecModule: RuntimeType): RuntimeType = RuntimeType.String + override fun serverRouterRequestSpecType(requestSpecModule: RuntimeType): RuntimeType = RuntimeType.StaticStr override fun serverRouterRuntimeConstructor() = when (version) { diff --git a/rust-runtime/aws-smithy-http-server/Cargo.toml b/rust-runtime/aws-smithy-http-server/Cargo.toml index c8b619dcfc..a63a125941 100644 --- a/rust-runtime/aws-smithy-http-server/Cargo.toml +++ b/rust-runtime/aws-smithy-http-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aws-smithy-http-server" -version = "0.63.0" +version = "0.63.1" authors = ["Smithy Rust Server "] edition = "2021" license = "Apache-2.0" diff --git a/rust-runtime/aws-smithy-http-server/src/protocol/aws_json/router.rs b/rust-runtime/aws-smithy-http-server/src/protocol/aws_json/router.rs index 295c670b27..df304d823f 100644 --- a/rust-runtime/aws-smithy-http-server/src/protocol/aws_json/router.rs +++ b/rust-runtime/aws-smithy-http-server/src/protocol/aws_json/router.rs @@ -47,7 +47,7 @@ const ROUTE_CUTOFF: usize = 15; /// [AWS JSON 1.1]: https://smithy.io/2.0/aws/protocols/aws-json-1_1-protocol.html #[derive(Debug, Clone)] pub struct AwsJsonRouter { - routes: TinyMap, + routes: TinyMap<&'static str, S, ROUTE_CUTOFF>, } impl AwsJsonRouter { @@ -106,9 +106,9 @@ where } } -impl FromIterator<(String, S)> for AwsJsonRouter { +impl FromIterator<(&'static str, S)> for AwsJsonRouter { #[inline] - fn from_iter>(iter: T) -> Self { + fn from_iter>(iter: T) -> Self { Self { routes: iter.into_iter().collect(), } @@ -126,11 +126,7 @@ mod tests { #[tokio::test] async fn simple_routing() { let routes = vec![("Service.Operation")]; - let router: AwsJsonRouter<_> = routes - .clone() - .into_iter() - .map(|operation| (operation.to_string(), ())) - .collect(); + let router: AwsJsonRouter<_> = routes.clone().into_iter().map(|operation| (operation, ())).collect(); let mut headers = HeaderMap::new(); headers.insert("x-amz-target", HeaderValue::from_static("Service.Operation"));