Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,7 @@ class Parser : public ParserState {
FLATBUFFERS_CHECKED_ERROR ParseEnum(bool is_union, EnumDef** dest,
const char* filename);
FLATBUFFERS_CHECKED_ERROR ParseNamespace();
FLATBUFFERS_CHECKED_ERROR ParseOption();
FLATBUFFERS_CHECKED_ERROR StartStruct(const std::string& name,
StructDef** dest);
FLATBUFFERS_CHECKED_ERROR StartEnum(const std::string& name, bool is_union,
Expand Down Expand Up @@ -1241,6 +1242,8 @@ class Parser : public ParserState {
StructDef* root_struct_def_;
std::string file_identifier_;
std::string file_extension_;
std::string rust_module_;
std::map<std::string, std::string> rust_modules_by_file_;

std::map<uint64_t, std::string> included_files_;
std::map<std::string, std::set<IncludedFile>> files_included_per_file_;
Expand Down
22 changes: 18 additions & 4 deletions include/flatbuffers/reflection_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,8 @@ struct Schema FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
VT_ROOT_TABLE = 12,
VT_SERVICES = 14,
VT_ADVANCED_FEATURES = 16,
VT_FBS_FILES = 18
VT_FBS_FILES = 18,
VT_RUST_MODULE = 20
};
const ::flatbuffers::Vector<::flatbuffers::Offset<reflection::Object>> *objects() const {
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<reflection::Object>> *>(VT_OBJECTS);
Expand Down Expand Up @@ -1369,6 +1370,9 @@ struct Schema FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
const ::flatbuffers::Vector<::flatbuffers::Offset<reflection::SchemaFile>> *fbs_files() const {
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<reflection::SchemaFile>> *>(VT_FBS_FILES);
}
const ::flatbuffers::String *rust_module() const {
return GetPointer<const ::flatbuffers::String *>(VT_RUST_MODULE);
}
template <bool B = false>
bool Verify(::flatbuffers::VerifierTemplate<B> &verifier) const {
return VerifyTableStart(verifier) &&
Expand All @@ -1391,6 +1395,8 @@ struct Schema FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
VerifyOffset(verifier, VT_FBS_FILES) &&
verifier.VerifyVector(fbs_files()) &&
verifier.VerifyVectorOfTables(fbs_files()) &&
VerifyOffset(verifier, VT_RUST_MODULE) &&
verifier.VerifyString(rust_module()) &&
verifier.EndTable();
}
};
Expand Down Expand Up @@ -1423,6 +1429,9 @@ struct SchemaBuilder {
void add_fbs_files(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<reflection::SchemaFile>>> fbs_files) {
fbb_.AddOffset(Schema::VT_FBS_FILES, fbs_files);
}
void add_rust_module(::flatbuffers::Offset<::flatbuffers::String> rust_module) {
fbb_.AddOffset(Schema::VT_RUST_MODULE, rust_module);
}
explicit SchemaBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
Expand All @@ -1445,9 +1454,11 @@ inline ::flatbuffers::Offset<Schema> CreateSchema(
::flatbuffers::Offset<reflection::Object> root_table = 0,
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<reflection::Service>>> services = 0,
reflection::AdvancedFeatures advanced_features = static_cast<reflection::AdvancedFeatures>(0),
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<reflection::SchemaFile>>> fbs_files = 0) {
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<reflection::SchemaFile>>> fbs_files = 0,
::flatbuffers::Offset<::flatbuffers::String> rust_module = 0) {
SchemaBuilder builder_(_fbb);
builder_.add_advanced_features(advanced_features);
builder_.add_rust_module(rust_module);
builder_.add_fbs_files(fbs_files);
builder_.add_services(services);
builder_.add_root_table(root_table);
Expand All @@ -1467,13 +1478,15 @@ inline ::flatbuffers::Offset<Schema> CreateSchemaDirect(
::flatbuffers::Offset<reflection::Object> root_table = 0,
std::vector<::flatbuffers::Offset<reflection::Service>> *services = nullptr,
reflection::AdvancedFeatures advanced_features = static_cast<reflection::AdvancedFeatures>(0),
std::vector<::flatbuffers::Offset<reflection::SchemaFile>> *fbs_files = nullptr) {
std::vector<::flatbuffers::Offset<reflection::SchemaFile>> *fbs_files = nullptr,
const char *rust_module = nullptr) {
auto objects__ = objects ? _fbb.CreateVectorOfSortedTables<reflection::Object>(objects) : 0;
auto enums__ = enums ? _fbb.CreateVectorOfSortedTables<reflection::Enum>(enums) : 0;
auto file_ident__ = file_ident ? _fbb.CreateString(file_ident) : 0;
auto file_ext__ = file_ext ? _fbb.CreateString(file_ext) : 0;
auto services__ = services ? _fbb.CreateVectorOfSortedTables<reflection::Service>(services) : 0;
auto fbs_files__ = fbs_files ? _fbb.CreateVectorOfSortedTables<reflection::SchemaFile>(fbs_files) : 0;
auto rust_module__ = rust_module ? _fbb.CreateString(rust_module) : 0;
return reflection::CreateSchema(
_fbb,
objects__,
Expand All @@ -1483,7 +1496,8 @@ inline ::flatbuffers::Offset<Schema> CreateSchemaDirect(
root_table,
services__,
advanced_features,
fbs_files__);
fbs_files__,
rust_module__);
}

inline const reflection::Schema *GetSchema(const void *buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public final class Schema extends com.google.flatbuffers.Table {
public com.google.flatbuffers.reflection.SchemaFile fbsFilesByKey(com.google.flatbuffers.reflection.SchemaFile obj, String key) { int o = __offset(18); return o != 0 ? com.google.flatbuffers.reflection.SchemaFile.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public com.google.flatbuffers.reflection.SchemaFile.Vector fbsFilesVector() { return fbsFilesVector(new com.google.flatbuffers.reflection.SchemaFile.Vector()); }
public com.google.flatbuffers.reflection.SchemaFile.Vector fbsFilesVector(com.google.flatbuffers.reflection.SchemaFile.Vector obj) { int o = __offset(18); return o != 0 ? obj.__assign(__vector(o), 4, bb) : null; }
public String rustModule() { int o = __offset(20); return o != 0 ? __string(o + bb_pos) : null; }
public ByteBuffer rustModuleAsByteBuffer() { return __vector_as_bytebuffer(20, 1); }
public ByteBuffer rustModuleInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 20, 1); }

public static int createSchema(FlatBufferBuilder builder,
int objectsOffset,
Expand All @@ -77,9 +80,11 @@ public static int createSchema(FlatBufferBuilder builder,
int rootTableOffset,
int servicesOffset,
long advancedFeatures,
int fbsFilesOffset) {
builder.startTable(8);
int fbsFilesOffset,
int rustModuleOffset) {
builder.startTable(9);
Schema.addAdvancedFeatures(builder, advancedFeatures);
Schema.addRustModule(builder, rustModuleOffset);
Schema.addFbsFiles(builder, fbsFilesOffset);
Schema.addServices(builder, servicesOffset);
Schema.addRootTable(builder, rootTableOffset);
Expand All @@ -90,7 +95,7 @@ public static int createSchema(FlatBufferBuilder builder,
return Schema.endSchema(builder);
}

public static void startSchema(FlatBufferBuilder builder) { builder.startTable(8); }
public static void startSchema(FlatBufferBuilder builder) { builder.startTable(9); }
public static void addObjects(FlatBufferBuilder builder, int objectsOffset) { builder.addOffset(0, objectsOffset, 0); }
public static int createObjectsVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
public static void startObjectsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
Expand All @@ -107,6 +112,7 @@ public static int createSchema(FlatBufferBuilder builder,
public static void addFbsFiles(FlatBufferBuilder builder, int fbsFilesOffset) { builder.addOffset(7, fbsFilesOffset, 0); }
public static int createFbsFilesVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
public static void startFbsFilesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
public static void addRustModule(FlatBufferBuilder builder, int rustModuleOffset) { builder.addOffset(8, rustModuleOffset, 0); }
public static int endSchema(FlatBufferBuilder builder) {
int o = builder.endTable();
builder.required(o, 4); // objects
Expand Down
15 changes: 14 additions & 1 deletion python/flatbuffers/reflection/Schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,15 @@ def FbsFilesIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(18))
return o == 0

# Schema
def RustModule(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(20))
if o != 0:
return self._tab.String(o + self._tab.Pos)
return None

def SchemaStart(builder):
builder.StartObject(8)
builder.StartObject(9)

def Start(builder):
SchemaStart(builder)
Expand Down Expand Up @@ -264,6 +271,12 @@ def SchemaCreateFbsFilesVector(builder, data):
def CreateFbsFilesVector(builder, data):
return SchemaCreateFbsFilesVector(builder, data)

def SchemaAddRustModule(builder, rustModule):
builder.PrependUOffsetTRelativeSlot(8, flatbuffers.number_types.UOffsetTFlags.py_type(rustModule), 0)

def AddRustModule(builder, rustModule):
SchemaAddRustModule(builder, rustModule)

def SchemaEnd(builder):
return builder.EndObject()

Expand Down
3 changes: 2 additions & 1 deletion reflection/reflection.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ table Field {
documentation:[string];
optional:bool = false;
/// Number of padding octets to always add after this field. Structs only.
padding:uint16 = 0;
padding:uint16 = 0;
/// If the field uses 64-bit offsets.
offset64:bool = false;
}
Expand Down Expand Up @@ -148,6 +148,7 @@ table Schema {
/// All the files used in this compilation. Files are relative to where
/// flatc was invoked.
fbs_files:[SchemaFile]; // Sorted.
rust_module:string;
}

root_type Schema;
Expand Down
Binary file modified samples/monster.bfbs
Binary file not shown.
18 changes: 2 additions & 16 deletions samples/rust_generated/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
// Automatically generated by the Flatbuffers compiler. Do not modify.
// @generated
pub mod my_game {
use super::*;
pub mod sample {
use super::*;
mod color_generated;
pub use self::color_generated::*;
mod equipment_generated;
pub use self::equipment_generated::*;
mod vec_3_generated;
pub use self::vec_3_generated::*;
mod monster_generated;
pub use self::monster_generated::*;
mod weapon_generated;
pub use self::weapon_generated::*;
} // sample
} // my_game

pub mod my_game;
4 changes: 4 additions & 0 deletions samples/rust_generated/my_game/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Automatically generated by the Flatbuffers compiler. Do not modify.
// @generated

pub mod sample;
1 change: 0 additions & 1 deletion samples/rust_generated/my_game/sample/color_generated.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
// @generated
extern crate alloc;
use super::*;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_COLOR: i8 = 0;
Expand Down
15 changes: 7 additions & 8 deletions samples/rust_generated/my_game/sample/equipment_generated.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
// @generated
extern crate alloc;
use super::*;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_EQUIPMENT: u8 = 0;
Expand Down Expand Up @@ -105,7 +104,7 @@ pub struct EquipmentUnionTableOffset {}
#[derive(Debug, Clone, PartialEq)]
pub enum EquipmentT {
NONE,
Weapon(alloc::boxed::Box<WeaponT>),
Weapon(alloc::boxed::Box<super::WeaponT>),
}

impl Default for EquipmentT {
Expand All @@ -129,8 +128,8 @@ impl EquipmentT {
}
}

/// If the union variant matches, return the owned WeaponT, setting the union to NONE.
pub fn take_weapon(&mut self) -> Option<alloc::boxed::Box<WeaponT>> {
/// If the union variant matches, return the owned super::WeaponT, setting the union to NONE.
pub fn take_weapon(&mut self) -> Option<alloc::boxed::Box<super::WeaponT>> {
if let Self::Weapon(_) = self {
let v = ::core::mem::replace(self, Self::NONE);
if let Self::Weapon(w) = v {
Expand All @@ -143,13 +142,13 @@ impl EquipmentT {
}
}

/// If the union variant matches, return a reference to the WeaponT.
pub fn as_weapon(&self) -> Option<&WeaponT> {
/// If the union variant matches, return a reference to the super::WeaponT.
pub fn as_weapon(&self) -> Option<&super::WeaponT> {
if let Self::Weapon(v) = self { Some(v.as_ref()) } else { None }
}

/// If the union variant matches, return a mutable reference to the WeaponT.
pub fn as_weapon_mut(&mut self) -> Option<&mut WeaponT> {
/// If the union variant matches, return a mutable reference to the super::WeaponT.
pub fn as_weapon_mut(&mut self) -> Option<&mut super::WeaponT> {
if let Self::Weapon(v) = self { Some(v.as_mut()) } else { None }
}
}
Expand Down
14 changes: 14 additions & 0 deletions samples/rust_generated/my_game/sample/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Automatically generated by the Flatbuffers compiler. Do not modify.
// @generated

mod color_generated;
mod equipment_generated;
mod vec_3_generated;
mod monster_generated;
mod weapon_generated;

pub use self::color_generated::*;
pub use self::equipment_generated::*;
pub use self::vec_3_generated::*;
pub use self::monster_generated::*;
pub use self::weapon_generated::*;
Loading