mirror of
https://github.com/vee1e/flatbuffers.git
synced 2026-09-01 17:57:25 +00:00
[rust] Make enum names public (#5690)
* Bugfix for Rust generation of union fields named with language keywords
Looking at ParseField, it appears that in the case of unions, an extra field with a `UnionTypeFieldSuffix` is added to the type definition, however, if the name of this field is a keyword in the target language, it isn't escaped.
For example, if generating code for rust for a union field named `type`, flatc will generate a (non-keyword escaped) field named `type_type` for this hidden union field, and one (keyword escaped) called `type_` for the actual union contents.
When the union accessors are generated, they refer to this `type_type` field, but they will escape it mistakenly, generating code like this:
```
#[inline]
#[allow(non_snake_case)]
pub fn type__as_int(&self) -> Option<Int<'a>> {
if self.type__type() == Type::Int {
self.type_().map(|u| Int::init_from_table(u))
} else {
None
}
}
```
Which will fail to build because the field is called `self.type_type()`, not `self.type__type()`.
* [Rust] Add crate-relative use statements for FBS includes.
At present if a flatbuffer description includes a reference to a type in
another file, the generated Rust code needs to be hand-modified to add
the appropriate `use` statements.
This assumes that the dependencies are built into the same crate, which
I think is a reasonable assumption?
* Revert "[Rust] Add crate-relative use statements for FBS includes."
This reverts commit d554d79fecf5afd6da6fb993b30b4cd523a5889a.
* Address comments raised in PR
* Update documentation comments per feedback
* Fix typo
* [rust] Make enum variant names public.
* Update generated test files
* Add test for public enum names
This commit is contained in:
parent
bcd58a159b
commit
355dfd48d1
4 changed files with 41 additions and 7 deletions
|
|
@ -621,7 +621,7 @@ class RustGenerator : public BaseGenerator {
|
|||
static const uint64_t kMaxSparseness = 5;
|
||||
if (range / static_cast<uint64_t>(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();
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue