Skip to content

Commit

Permalink
Add const to generated enum values() (#2011)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
lsr0 and LukeMathWalker authored Nov 22, 2022
1 parent 86fd8f5 commit 0f26ead
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0f26ead

Please sign in to comment.