diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 01a2cbbd5..6aaaddea9 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -621,7 +621,7 @@ class RustGenerator : public BaseGenerator { static const uint64_t kMaxSparseness = 5; if (range / static_cast(enum_def.size()) < kMaxSparseness) { code_ += "#[allow(non_camel_case_types)]"; - code_ += "const ENUM_NAMES_{{ENUM_NAME_CAPS}}:[&'static str; " + + code_ += "pub const ENUM_NAMES_{{ENUM_NAME_CAPS}}:[&'static str; " + NumToString(range + 1) + "] = ["; auto val = enum_def.Vals().front(); diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index f44c96b51..a05073107 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -219,7 +219,7 @@ pub const ENUM_VALUES_COLOR:[Color; 3] = [ ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_COLOR:[&'static str; 8] = [ +pub const ENUM_NAMES_COLOR:[&'static str; 8] = [ "Red", "Green", "", @@ -289,7 +289,7 @@ pub const ENUM_VALUES_RACE:[Race; 4] = [ ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_RACE:[&'static str; 4] = [ +pub const ENUM_NAMES_RACE:[&'static str; 4] = [ "None", "Human", "Dwarf", @@ -355,7 +355,7 @@ pub const ENUM_VALUES_ANY:[Any; 4] = [ ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_ANY:[&'static str; 4] = [ +pub const ENUM_NAMES_ANY:[&'static str; 4] = [ "NONE", "Monster", "TestSimpleTableWithEnum", @@ -422,7 +422,7 @@ pub const ENUM_VALUES_ANY_UNIQUE_ALIASES:[AnyUniqueAliases; 4] = [ ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_ANY_UNIQUE_ALIASES:[&'static str; 4] = [ +pub const ENUM_NAMES_ANY_UNIQUE_ALIASES:[&'static str; 4] = [ "NONE", "M", "TS", @@ -489,7 +489,7 @@ pub const ENUM_VALUES_ANY_AMBIGUOUS_ALIASES:[AnyAmbiguousAliases; 4] = [ ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_ANY_AMBIGUOUS_ALIASES:[&'static str; 4] = [ +pub const ENUM_NAMES_ANY_AMBIGUOUS_ALIASES:[&'static str; 4] = [ "NONE", "M1", "M2", diff --git a/tests/namespace_test/namespace_test1_generated.rs b/tests/namespace_test/namespace_test1_generated.rs index c4d428c6f..b811e3a0f 100644 --- a/tests/namespace_test/namespace_test1_generated.rs +++ b/tests/namespace_test/namespace_test1_generated.rs @@ -77,7 +77,7 @@ pub const ENUM_VALUES_ENUM_IN_NESTED_NS:[EnumInNestedNS; 3] = [ ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_ENUM_IN_NESTED_NS:[&'static str; 3] = [ +pub const ENUM_NAMES_ENUM_IN_NESTED_NS:[&'static str; 3] = [ "A", "B", "C" diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs index 8fc086e31..b8347694d 100644 --- a/tests/rust_usage_test/tests/integration_test.rs +++ b/tests/rust_usage_test/tests/integration_test.rs @@ -245,6 +245,16 @@ mod generated_constants { my_game::example::Color::Green, my_game::example::Color::Blue, ]); + assert_eq!(my_game::example::ENUM_NAMES_COLOR, [ + "Red", + "Green", + "", + "", + "", + "", + "", + "Blue" + ]); assert_eq!(-1, my_game::example::ENUM_MIN_RACE); assert_eq!(2, my_game::example::ENUM_MAX_RACE); @@ -254,6 +264,12 @@ mod generated_constants { my_game::example::Race::Dwarf, my_game::example::Race::Elf, ]); + assert_eq!(my_game::example::ENUM_NAMES_RACE, [ + "None", + "Human", + "Dwarf", + "Elf" + ]); assert_eq!(0, my_game::example::ENUM_MIN_ANY); assert_eq!(3, my_game::example::ENUM_MAX_ANY); @@ -263,6 +279,12 @@ mod generated_constants { my_game::example::Any::TestSimpleTableWithEnum, my_game::example::Any::MyGame_Example2_Monster, ]); + assert_eq!(my_game::example::ENUM_NAMES_ANY, [ + "NONE", + "Monster", + "TestSimpleTableWithEnum", + "MyGame_Example2_Monster" + ]); assert_eq!(0, my_game::example::ENUM_MIN_ANY_UNIQUE_ALIASES); assert_eq!(3, my_game::example::ENUM_MAX_ANY_UNIQUE_ALIASES); @@ -272,6 +294,12 @@ mod generated_constants { my_game::example::AnyUniqueAliases::TS, my_game::example::AnyUniqueAliases::M2, ]); + assert_eq!(my_game::example::ENUM_NAMES_ANY_UNIQUE_ALIASES, [ + "NONE", + "M", + "TS", + "M2" + ]); assert_eq!(0, my_game::example::ENUM_MIN_ANY_AMBIGUOUS_ALIASES); assert_eq!(3, my_game::example::ENUM_MAX_ANY_AMBIGUOUS_ALIASES); @@ -281,6 +309,12 @@ mod generated_constants { my_game::example::AnyAmbiguousAliases::M2, my_game::example::AnyAmbiguousAliases::M3, ]); + assert_eq!(my_game::example::ENUM_NAMES_ANY_AMBIGUOUS_ALIASES, [ + "NONE", + "M1", + "M2", + "M3" + ]); } }