mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 05:22:27 +00:00
[Rust] Ensure unions are referenced with the correct path (#6422)
* Add codegen test for namespaced unions * [Rust] Handle cross-namespace union use * [Rust] Test namespace handling * [Rust] Drop trailing whitespace in codegen * [Rust] Set flags in generate_code.bat to match .sh * [C#] Add additional namespace test file
This commit is contained in:
@@ -24,6 +24,110 @@ inline const flatbuffers::TypeTable *TableInNestedNSTypeTable();
|
||||
|
||||
inline const flatbuffers::TypeTable *StructInNestedNSTypeTable();
|
||||
|
||||
enum UnionInNestedNS : uint8_t {
|
||||
UnionInNestedNS_NONE = 0,
|
||||
UnionInNestedNS_TableInNestedNS = 1,
|
||||
UnionInNestedNS_MIN = UnionInNestedNS_NONE,
|
||||
UnionInNestedNS_MAX = UnionInNestedNS_TableInNestedNS
|
||||
};
|
||||
|
||||
inline const UnionInNestedNS (&EnumValuesUnionInNestedNS())[2] {
|
||||
static const UnionInNestedNS values[] = {
|
||||
UnionInNestedNS_NONE,
|
||||
UnionInNestedNS_TableInNestedNS
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char * const *EnumNamesUnionInNestedNS() {
|
||||
static const char * const names[3] = {
|
||||
"NONE",
|
||||
"TableInNestedNS",
|
||||
nullptr
|
||||
};
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameUnionInNestedNS(UnionInNestedNS e) {
|
||||
if (flatbuffers::IsOutRange(e, UnionInNestedNS_NONE, UnionInNestedNS_TableInNestedNS)) return "";
|
||||
const size_t index = static_cast<size_t>(e);
|
||||
return EnumNamesUnionInNestedNS()[index];
|
||||
}
|
||||
|
||||
template<typename T> struct UnionInNestedNSTraits {
|
||||
static const UnionInNestedNS enum_value = UnionInNestedNS_NONE;
|
||||
};
|
||||
|
||||
template<> struct UnionInNestedNSTraits<NamespaceA::NamespaceB::TableInNestedNS> {
|
||||
static const UnionInNestedNS enum_value = UnionInNestedNS_TableInNestedNS;
|
||||
};
|
||||
|
||||
struct UnionInNestedNSUnion {
|
||||
UnionInNestedNS type;
|
||||
void *value;
|
||||
|
||||
UnionInNestedNSUnion() : type(UnionInNestedNS_NONE), value(nullptr) {}
|
||||
UnionInNestedNSUnion(UnionInNestedNSUnion&& u) FLATBUFFERS_NOEXCEPT :
|
||||
type(UnionInNestedNS_NONE), value(nullptr)
|
||||
{ std::swap(type, u.type); std::swap(value, u.value); }
|
||||
UnionInNestedNSUnion(const UnionInNestedNSUnion &);
|
||||
UnionInNestedNSUnion &operator=(const UnionInNestedNSUnion &u)
|
||||
{ UnionInNestedNSUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; }
|
||||
UnionInNestedNSUnion &operator=(UnionInNestedNSUnion &&u) FLATBUFFERS_NOEXCEPT
|
||||
{ std::swap(type, u.type); std::swap(value, u.value); return *this; }
|
||||
~UnionInNestedNSUnion() { Reset(); }
|
||||
|
||||
void Reset();
|
||||
|
||||
#ifndef FLATBUFFERS_CPP98_STL
|
||||
template <typename T>
|
||||
void Set(T&& val) {
|
||||
using RT = typename std::remove_reference<T>::type;
|
||||
Reset();
|
||||
type = UnionInNestedNSTraits<typename RT::TableType>::enum_value;
|
||||
if (type != UnionInNestedNS_NONE) {
|
||||
value = new RT(std::forward<T>(val));
|
||||
}
|
||||
}
|
||||
#endif // FLATBUFFERS_CPP98_STL
|
||||
|
||||
static void *UnPack(const void *obj, UnionInNestedNS type, const flatbuffers::resolver_function_t *resolver);
|
||||
flatbuffers::Offset<void> Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const;
|
||||
|
||||
NamespaceA::NamespaceB::TableInNestedNST *AsTableInNestedNS() {
|
||||
return type == UnionInNestedNS_TableInNestedNS ?
|
||||
reinterpret_cast<NamespaceA::NamespaceB::TableInNestedNST *>(value) : nullptr;
|
||||
}
|
||||
const NamespaceA::NamespaceB::TableInNestedNST *AsTableInNestedNS() const {
|
||||
return type == UnionInNestedNS_TableInNestedNS ?
|
||||
reinterpret_cast<const NamespaceA::NamespaceB::TableInNestedNST *>(value) : nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
inline bool operator==(const UnionInNestedNSUnion &lhs, const UnionInNestedNSUnion &rhs) {
|
||||
if (lhs.type != rhs.type) return false;
|
||||
switch (lhs.type) {
|
||||
case UnionInNestedNS_NONE: {
|
||||
return true;
|
||||
}
|
||||
case UnionInNestedNS_TableInNestedNS: {
|
||||
return *(reinterpret_cast<const NamespaceA::NamespaceB::TableInNestedNST *>(lhs.value)) ==
|
||||
*(reinterpret_cast<const NamespaceA::NamespaceB::TableInNestedNST *>(rhs.value));
|
||||
}
|
||||
default: {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline bool operator!=(const UnionInNestedNSUnion &lhs, const UnionInNestedNSUnion &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
bool VerifyUnionInNestedNS(flatbuffers::Verifier &verifier, const void *obj, UnionInNestedNS type);
|
||||
bool VerifyUnionInNestedNSVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types);
|
||||
|
||||
enum EnumInNestedNS : int8_t {
|
||||
EnumInNestedNS_A = 0,
|
||||
EnumInNestedNS_B = 1,
|
||||
@@ -203,6 +307,93 @@ inline flatbuffers::Offset<TableInNestedNS> CreateTableInNestedNS(flatbuffers::F
|
||||
_foo);
|
||||
}
|
||||
|
||||
inline bool VerifyUnionInNestedNS(flatbuffers::Verifier &verifier, const void *obj, UnionInNestedNS type) {
|
||||
switch (type) {
|
||||
case UnionInNestedNS_NONE: {
|
||||
return true;
|
||||
}
|
||||
case UnionInNestedNS_TableInNestedNS: {
|
||||
auto ptr = reinterpret_cast<const NamespaceA::NamespaceB::TableInNestedNS *>(obj);
|
||||
return verifier.VerifyTable(ptr);
|
||||
}
|
||||
default: return true;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool VerifyUnionInNestedNSVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector<flatbuffers::Offset<void>> *values, const flatbuffers::Vector<uint8_t> *types) {
|
||||
if (!values || !types) return !values && !types;
|
||||
if (values->size() != types->size()) return false;
|
||||
for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) {
|
||||
if (!VerifyUnionInNestedNS(
|
||||
verifier, values->Get(i), types->GetEnum<UnionInNestedNS>(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void *UnionInNestedNSUnion::UnPack(const void *obj, UnionInNestedNS type, const flatbuffers::resolver_function_t *resolver) {
|
||||
switch (type) {
|
||||
case UnionInNestedNS_TableInNestedNS: {
|
||||
auto ptr = reinterpret_cast<const NamespaceA::NamespaceB::TableInNestedNS *>(obj);
|
||||
return ptr->UnPack(resolver);
|
||||
}
|
||||
default: return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
inline flatbuffers::Offset<void> UnionInNestedNSUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const {
|
||||
switch (type) {
|
||||
case UnionInNestedNS_TableInNestedNS: {
|
||||
auto ptr = reinterpret_cast<const NamespaceA::NamespaceB::TableInNestedNST *>(value);
|
||||
return CreateTableInNestedNS(_fbb, ptr, _rehasher).Union();
|
||||
}
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline UnionInNestedNSUnion::UnionInNestedNSUnion(const UnionInNestedNSUnion &u) : type(u.type), value(nullptr) {
|
||||
switch (type) {
|
||||
case UnionInNestedNS_TableInNestedNS: {
|
||||
value = new NamespaceA::NamespaceB::TableInNestedNST(*reinterpret_cast<NamespaceA::NamespaceB::TableInNestedNST *>(u.value));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inline void UnionInNestedNSUnion::Reset() {
|
||||
switch (type) {
|
||||
case UnionInNestedNS_TableInNestedNS: {
|
||||
auto ptr = reinterpret_cast<NamespaceA::NamespaceB::TableInNestedNST *>(value);
|
||||
delete ptr;
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
value = nullptr;
|
||||
type = UnionInNestedNS_NONE;
|
||||
}
|
||||
|
||||
inline const flatbuffers::TypeTable *UnionInNestedNSTypeTable() {
|
||||
static const flatbuffers::TypeCode type_codes[] = {
|
||||
{ flatbuffers::ET_SEQUENCE, 0, -1 },
|
||||
{ flatbuffers::ET_SEQUENCE, 0, 0 }
|
||||
};
|
||||
static const flatbuffers::TypeFunction type_refs[] = {
|
||||
NamespaceA::NamespaceB::TableInNestedNSTypeTable
|
||||
};
|
||||
static const char * const names[] = {
|
||||
"NONE",
|
||||
"TableInNestedNS"
|
||||
};
|
||||
static const flatbuffers::TypeTable tt = {
|
||||
flatbuffers::ST_UNION, 2, type_codes, type_refs, nullptr, nullptr, names
|
||||
};
|
||||
return &tt;
|
||||
}
|
||||
|
||||
inline const flatbuffers::TypeTable *EnumInNestedNSTypeTable() {
|
||||
static const flatbuffers::TypeCode type_codes[] = {
|
||||
{ flatbuffers::ET_CHAR, 0, 0 },
|
||||
|
||||
Reference in New Issue
Block a user