From f7ca8c1ac26c3ba1ce0e49d2a29142880bf1020a Mon Sep 17 00:00:00 2001 From: Lindsay Roberts Date: Sun, 20 Nov 2022 11:00:49 +0200 Subject: [PATCH 1/3] Add const to generated enum values() Enum values() functions return static arrays of static strings and could be of compile time use. Make callable in const contexts. --- CHANGELOG.next.toml | 6 ++++++ .../rust/codegen/core/smithy/generators/EnumGenerator.kt | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index fea153453a..35429d8667 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -478,3 +478,9 @@ x-amzn-errortype: com.example.service#InvalidRequestException references = ["smithy-rs#1982"] meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "server" } author = "david-perez" + +[[smithy-rs]] +message = "Make generated enum `values()` functions callable in const contexts." +references = [] +meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client" } +author = "lsr0" diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/EnumGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/EnumGenerator.kt index 0acfe2641d..95a46649ea 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/EnumGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/EnumGenerator.kt @@ -141,7 +141,7 @@ open class EnumGenerator( } docs("Returns all the `&str` representations of the enum members.") - rustBlock("pub fn $Values() -> &'static [&'static str]") { + rustBlock("pub const fn $Values() -> &'static [&'static str]") { withBlock("&[", "]") { val memberList = sortedMembers.joinToString(", ") { it.value.dq() } rust(memberList) @@ -198,7 +198,7 @@ open class EnumGenerator( } rust("/// Returns all the `&str` values of the enum members.") - rustBlock("pub fn $Values() -> &'static [&'static str]") { + rustBlock("pub const fn $Values() -> &'static [&'static str]") { withBlock("&[", "]") { val memberList = sortedMembers.joinToString(", ") { it.value.doubleQuote() } write(memberList) From 0093d630a912f8fbc1801ab0e47931e992843f6f Mon Sep 17 00:00:00 2001 From: Lindsay Roberts Date: Mon, 21 Nov 2022 11:49:37 +0200 Subject: [PATCH 2/3] Add reference to PR in changelog next for const enum values() --- CHANGELOG.next.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index 35429d8667..4f9eff56a6 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -481,6 +481,6 @@ author = "david-perez" [[smithy-rs]] message = "Make generated enum `values()` functions callable in const contexts." -references = [] +references = ["smithy-rs#2011"] meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client" } author = "lsr0" From 5f1b6ec4ef4aa54bce9da0c5c776936e3b9a4645 Mon Sep 17 00:00:00 2001 From: Lindsay Roberts Date: Mon, 21 Nov 2022 16:46:29 +0200 Subject: [PATCH 3/3] Correct changelog next target to all for const enum values() --- CHANGELOG.next.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index 4f9eff56a6..f0a3bf3c70 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -482,5 +482,5 @@ author = "david-perez" [[smithy-rs]] message = "Make generated enum `values()` functions callable in const contexts." references = ["smithy-rs#2011"] -meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client" } +meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "all" } author = "lsr0"