diff options
author | Max Burke <max@urbanlogiq.com> | 2019-12-31 10:28:58 -0800 |
---|---|---|
committer | Robert Winslow <rw@users.noreply.github.com> | 2019-12-31 10:28:58 -0800 |
commit | 355dfd48d16f4ec0f5b02c6841cfda2038dabfcb (patch) | |
tree | 082e10092cf261eb23aefab28d5bf7c13637c0ca /src/idl_gen_rust.cpp | |
parent | bcd58a159be154e3211868aea7bf3eec66ab21a5 (diff) | |
download | flatbuffers-355dfd48d16f4ec0f5b02c6841cfda2038dabfcb.tar.gz flatbuffers-355dfd48d16f4ec0f5b02c6841cfda2038dabfcb.tar.bz2 flatbuffers-355dfd48d16f4ec0f5b02c6841cfda2038dabfcb.zip |
[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
Diffstat (limited to 'src/idl_gen_rust.cpp')
-rw-r--r-- | src/idl_gen_rust.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 01a2cbbd..6aaaddea 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<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(); |