From 0f26eade6047b6407fc866bc53c655fbec00a127 Mon Sep 17 00:00:00 2001 From: Lindsay Roberts Date: Tue, 22 Nov 2022 15:25:10 +0200 Subject: [PATCH] Add const to generated enum values() (#2011) * 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. * Add reference to PR in changelog next for const enum values() * Correct changelog next target to all for const enum values() Co-authored-by: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> --- 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..f0a3bf3c70 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 = ["smithy-rs#2011"] +meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "all" } +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)